def factorial_oob_test(): from factorial import factorial, FactorialOutOfBoundError for i in [-1, -10, 100, 70, 21]: try: factorial(i) assert False, "I accepted values out of bound [0,20]" except FactorialOutOfBoundError: pass
def factorial_invalid_test(): from factorial import factorial for i in ["a", "&", "9999999", None]: try: factorial(i) assert False, "I accepted values out of bound [0,20]" except TypeError: pass
def calculate(n, r): try: permutation = factorial(n) / factorial(n - r) combination = factorial(n) / (factorial(r) * factorial(n - r)) # This error comes from factioral() except ValueError: permutation = 0 combination = 0 # Return the number of permutations and combinations in a list return [permutation, combination]
def main(): print("\t\t\t10th degree of Maclaurin series, cos(x)") x = float(input("Enter value of x (in Radians): ")) answer = 1; i = 1; j = 2 while(i < 10 and j <= 10): if (i % 2 == 0): answer += (x ** j) / factorial(j) elif (i % 2 != 0): answer -= (x ** j) /factorial(j) i += 1; j += 2 print(answer)
def pascal(n): triangle = [] currentRow=[] for i in range(1,n): for j in range(1,n): val = (factorial(i)) / ((factorial(j)) * factorial((i-j))) if val !=0: currentRow.append(val) triangle.append(currentRow) currentRow =[1] for row in triangle: print("{: >3} {: >3} {: >3}".format(*row))
def main(): try: number = int(raw_input('Entre com um número inteiro: ')) print 'factorial({}) = {}'.format(number, factorial(number)) except ValueError: print 'Somente números inteiros são aceitos!' main()
def test_factorial(self): factorial_cases = [(1, 1), (0, 1), (5, 2*3*4*5), (30, 265252859812191058636308480000000)] for n, fact_n in factorial_cases: self.assertEqual(factorial(n), fact_n)
def test_factorial(): factorial_cases = [(1, 1), (0, 1), (5, 2*3*4*5), (30, 265252859812191058636308480000000)] for n, fact_n in factorial_cases: assert factorial(n) == fact_n
def main(): print("\t\t\t10th degree of Maclaurin Series, e^x") x = float(input("Enter value of x: ")) answer = 1; i = 1 while(i <= 10): answer += (x ** i) / factorial(i) i += 1 print(answer)
def factorial_find_boundary_test(): from factorial import factorial n = 1 (ret,prev) = (1,0) while True: prev = ret n = n + 1 ret = factorial(n) assert ret > prev, "Integer size limit reached: n: %d" % n
def test_factorial(self): self.assertEqual(factorial.factorial(0), 1) self.assertEqual(factorial.factorial(1), 1) self.assertEqual(factorial.factorial(2), 2) self.assertEqual(factorial.factorial(3), 6) self.assertEqual(factorial.factorial(4), 24) self.assertEqual(factorial.factorial(5), 120)
def euler74(n) : global d #print "euler74(%d)" % n if d.has_key(n) : return d[n] else : new_key = sum(factorial.factorial(ord(c) - ord('0')) for c in str(n)) #print new_key d[n] = 0 result = euler74(new_key) + 1 d[n] = result return result
def checkprime(n, primeFlag): if n in (1,2,3): primeFlag=True else: #check if the number is even, if even set primeFlag to be false if n % 2 ==0: primeFlag=False else: #now number is no 1,2,3 and is odd. #calling the fatorial program and check if the factorial comprises of the number alone z=factorial(n) if z==[n]: #if true, set primeFlag to be true primeFlag=True return primeFlag
def test_constant_factorial(self): self.assertEqual(2, factorial(2), msg='2 factorial should be 2')
low = size - 2 high = size - 1 while seq[low] >= seq[high]: if low == high - 1: low -= 1 high = size - 1 if low < 0: break else: high -= 1 if low < 0: raise Exception("End of Permutation") # swap seq[low] and seq[high] if low >= 0 seq[low], seq[high] = seq[high], seq[low] # reverse the low+1 to size-1 seq[low + 1 : size] = seq[size - 1 : low : -1] return seq if __name__ == '__main__': seq = [1, 5, 3, 4, 2] #print nextPermutation(seq) from factorial import factorial for i in range(factorial(len(seq))): try: print nextPermutation(seq) except: print("End of permutations") break else: print(seq)
def test_factorial(): assert factorial(3) == 1 * 2 * 3
def test_factorial(self): self.assertEqual(factorial(5), 120) self.assertEqual(factorial(7), 5040)
def test_result_is_int(self): result = factorial(1) self.assertIs(type(result), type(1))
def test_factorial_0(): assert factorial(0) == 1
def main(): result = factorial(4) print(result)
def test_fails_on_three_args(self): with self.assertRaises(TypeError): factorial(1, 2, 3)
def test_factorial_zero(): result = factorial(0) assert result == 1
# # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # # import factorial import kehrwert import sortierteListe zahl = 1 while zahl > 0: zahl = int(input("Geben Sie eine ganze Zahl ein: ")) if zahl > 0: print("Fakultät: ", factorial.factorial(zahl)) print("Kehrwert: ", kehrwert.kehr(zahl)) # Test mit der Klasse sortierteListe l = sortierteListe.SortierteListe([6, 4, 3]) print(l) l.append(2) print(l) l.extend([67, 0, -56]) print(l) l += [100, 5] print(l) l *= 2 print(l)
def test_factorial(): result = factorial(5) assert result == 120
def test_illegal_characters(): with pytest.raises(ValueError): factorial(10.5)
def test_negative_factorial(): with pytest.raises(ValueError): factorial(-1)
def testDecimalInput(self): self.assertEqual(factorial.factorial(2.3453), -1, msg="Factorial should return false for a decimal number")
def nCr(n, r): b = (nPr(n, r)) / factorial(r) return b
def testNegativeFactorial(self): self.assertEqual(factorial.factorial(-9), -1, msg="Factorial of a negative number should return false")
def test_factorial_1(self): result = factorial(1) self.assertEqual(result, 1)
def test_one_for_same(self): result = factorial(3) self.assertEqual(result, 6)
# try: # from factorial import factorial # except ImportError: # print("Error received") from factorial import factorial a, b = list(map(int, input("Enter space separated integers:").split(" "))) print("Factorial of both numbers:\n", "a:", factorial(a), "b:", factorial(b))
def test_factorial_10(self): result = factorial(10) self.assertEqual(result, 3628800)
def test_factorial_6(self): result = factorial(6) self.assertEqual(result, 720)
def test_factorial_8(self): result = factorial(8) self.assertEqual(result, 40320)
def test_simple_factorial(self): self.assertEqual(120, factorial(5), msg='5 factorial should be 120')
high = size - 1 while seq[low] >= seq[high]: if low == high - 1: low -= 1 high = size - 1 if low < 0: break else: high -= 1 if low < 0: raise Exception("End of Permutation") # swap seq[low] and seq[high] if low >= 0 seq[low], seq[high] = seq[high], seq[low] # reverse the low+1 to size-1 seq[low + 1:size] = seq[size - 1:low:-1] return seq if __name__ == '__main__': seq = [1, 5, 3, 4, 2] #print nextPermutation(seq) from factorial import factorial for i in range(factorial(len(seq))): try: print nextPermutation(seq) except: print("End of permutations") break else: print(seq)
def digfac(x): summ = 0 while x % 10 != 0: summ += factorial.factorial(x % 10) x /= 10 return summ
def test_factorial_7(self): result = factorial(7) self.assertEqual(result, 5040)
def test_factorial(self): self.assertEqual(factorial(1), 1)
def test_factorial_5(self): result = factorial(5) self.assertEqual(result, 120)
def test_factorial(result, expected): _f = factorial.factorial(result) assert _f == expected
def test_hard_factorial(self): self.assertEqual(3628800, factorial(10), msg='10 factorial should be 3628800')
""" # finds the permutation @index def find(index, num): num = str(num) if index-1 <= factorial.factorial(index-1) and index-1 >= 1: return lPn(num)[index-1] return "DID NOT FIND" """ done = False while done == False: num = "0123456789" start = int(input("Start (inclusive): ")) end = int(input("End (exclusive): ")) print("List of permutations from index " + str(start) + " to " + str(end - 1) + " with the number " + num[0:end]) for i in range(start, end): print(lPn(num[0:i])) response = input("Again? ") if response[0].lower() == "n": done = True print(factorial.factorial(int(input("Find Factorial of: ")))) #print(find("0123", 2)) print(find("0123456789", 1000000))
def test_factorial_known_values(): for n, val in good_results.items(): yield assert_equal, factorial(n), val
print(y, 'is prime')''' if __name__ == "__main__": d={1:lambda a,b:print('\t\tADDITION:',a+b),2:lambda a,b:print('\t\tSUBTRACTION:',a-b),3:lambda a,b:print('\t\tMULTIPLICATION:',a*b),\ 4:lambda a,b:print('\t\tDIVISION:',a/b),5:lambda a,b:print('\t\tFLOOR DIVISION:',a//b),\ 6:lambda a,b:print('\t\tMODULUS:',a%b),} while True: print("!-!-!WELCOME TO PyCalc!-!-!\n") a = numberValidator() b = numberValidator() while True: try: c = int( input( "\n1.ADDITION\n2.SUBTRACTION\n3.MULTIPLICATION\n4.DIVISION\n\ 5.FLOOR DIVISION\n6.MODULUS\n7.PRIMER\n8.FACTORIAL\n9.RESTART PYCALC\n0.EXIT\n\t\tENTER YOUR CHOICE: " )) if c == 0 or c == 9: break if c == 7 or c == 8: for num in [a, b]: if c == 7: primer(num) else: factorial(num) continue ans = d[c](a, b) except: print('invalid choice.try again.') continue if c == 0: break input('Thank You for using "PyCalc!" ')
def testAlphaticalInput(self): self.assertEqual(factorial.factorial('ieiee'), -1, msg="Factorial should return false for alphatical input")
def interfaz_factorial(number): try: result = int(number) return factorial(result) except: return 'error'
def testPositiveFactorial(self): self.assertEqual(factorial.factorial(5), 120, msg="5 factorial should return 120") self.assertEqual(factorial.factorial(4), 24, msg="4 factorial should return 24")
'''There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 235, 245, and 345 In combinatorics, we use the notation, 5C3 = 10. In general, nCr = n! r!(n−r)! ,where r ≤ n, n! = n×(n−1)×...×3×2×1, and 0! = 1. It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066. How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are greater than one-million?''' import factorial n=0 for i in range (23,101): for j in range (i) : C=factorial.factorial(i)/(factorial.factorial(j)*factorial.factorial(i-j)) if C>1000000: n+=(i-j)-j+1 break print n
def test_factorial_4(self): result = factorial(4) self.assertEqual(result, 24)
def test_fac_1(): assert factorial.factorial(1) == 1
def test_fails_on_negative(self): with self.assertRaises(ValueError): factorial(-1)
def test_fac_2(): assert factorial.factorial(2) == 2
def test_result_is_float(self): result = factorial(1.0) self.assertIs(type(result), type(1.0))
def test_fac_4(): assert factorial.factorial(4) == 24
def test_zero(self): result = factorial(0) self.assertEqual(result, 1)
def test_factorial_9(self): result = factorial(9) self.assertEqual(result, 362880)
def test_fails_on_no_args(self): with self.assertRaises(TypeError): factorial()
def test_factorial_2(self): result = factorial(2) self.assertEqual(result, 2)