def test_number(self): src = "123" col = simplify(src) self.assertEqual(col, 123) src = "-123" col = simplify(src) self.assertEqual(col, -123) src = "1/2" col = simplify(src) self.assertEqual(col, fraction(1, 2)) src = "-1/2" col = simplify(src) self.assertEqual(col, fraction(-1, 2)) src = "1.5" col = simplify(src) self.assertEqual(col, 1.5) src = ".5" col = simplify(src) self.assertEqual(col, 0.5) src = "1." col = simplify(src) self.assertEqual(col, 1.0) src = "-1.5" col = simplify(src) self.assertEqual(col, -1.5) src = "-.5" col = simplify(src) self.assertEqual(col, -0.5) src = "-1." col = simplify(src) self.assertEqual(col, -1.0) src = "8+1j" col = simplify(src) self.assertEqual(col, complex("8+1j")) src = "3+i" col = simplify(src) self.assertEqual(col, complex("3+j")) src = "-1.1+2j" col = simplify(src) self.assertEqual(col, complex("-1.1+2j"))
def test_number(self): src = "123" stmt, env = compile_expr(src) self.assertEqual(stmt(), 123) src = "-123" stmt, env = compile_expr(src) self.assertEqual(stmt(), -123) src = "1/2" stmt, env = compile_expr(src) self.assertEqual(stmt(), fraction(1, 2)) src = "-1/2" stmt, env = compile_expr(src) self.assertEqual(stmt(), fraction(-1, 2)) src = "1.5" stmt, env = compile_expr(src) self.assertEqual(stmt(), 1.5) src = ".5" stmt, env = compile_expr(src) self.assertEqual(stmt(), 0.5) src = "1." stmt, env = compile_expr(src) self.assertEqual(stmt(), 1.0) src = "-1.5" stmt, env = compile_expr(src) self.assertEqual(stmt(), -1.5) src = "-1." stmt, env = compile_expr(src) self.assertEqual(stmt(), -1.0) src = "8+1j" stmt, env = compile_expr(src) self.assertEqual(stmt(), complex("8+1j")) src = "3+i" stmt, env = compile_expr(src) self.assertEqual(stmt(), complex("3+j")) src = "-1.1+2j" stmt, env = compile_expr(src) self.assertEqual(stmt(), complex("-1.1+2j"))
def main(): product = 1 for denominator in range(10+1, 100): for numerator in range(10, denominator): if is_digit_cancelling_fraction(numerator, denominator): product *= fraction(numerator, denominator) print product.denominator
def is_digit_cancelling_fraction(num, denom): str_num, str_denom = str(num), str(denom) frac = None try: if str_num[0] == str_denom[0] != '0': frac = fraction(int(str_num[1]), int(str_denom[1])) elif str_num[0] == str_denom[1] != '0': frac = fraction(int(str_num[1]), int(str_denom[0])) elif str_num[1] == str_denom[0] != '0': frac = fraction(int(str_num[0]), int(str_denom[1])) elif str_num[1] == str_denom[1] != '0': frac = fraction(int(str_num[0]), int(str_denom[0])) except ZeroDivisionError: pass if frac == fraction(num, denom): return True return False
def cal_faction(sequence, extend): sequence += extend length = len(extend) nround = length-1 frac = fraction(sequence[length-1], 1) for i in range(nround): frac = one_round(sequence[length-2-i], frac) return frac
def main(): round = 1000 count = 0 num = fraction(3,2) for i in range(1,round): num = sr_deduction(num) if is_longer(num): count += 1 print count
def _as_fraction(s): # this will raise a type error if s cannot be parsed into a # fraction s = fraction(s) # the fraction type cannot be stored in a const pool, so we have # to turn it into a source form of (fraction NUMERATOR # DENOMINATOR) instead. We take the integer values from the # parsing attempt above, so that we end up with integers in the # const pool rather than the literal string, which should make for # faster instantiation of the fraction (parsing once at compile # rather than parsing over and over every time we evaluate this # code) return cons(_symbol_fraction, s.numerator, s.denominator, nil)
def test_fraction(self): self.setup(Fraction, default=fraction('8/1'), allow_none=False) self.commons(FractionVariable) self.assertEqual(self.obj.prop1, fraction('8/1')) self.assertRaises(NoneError, setattr, self.obj, 'prop1', None) self.obj.prop2 = None self.assertEqual(self.obj.prop2, None) self.obj.prop1 = fraction('1.20') self.assertEqual(self.obj.prop1, fraction('6/5')) self.obj.prop1 = fraction('1.40') self.assertEqual(self.obj.prop1, fraction('7/5')) self.assertRaises(TypeError, setattr, self.obj, 'prop1', 1)
def simplify(self, positions): return fraction(self.token)
def main(): e = gen_e_constant(100) result = fraction(e[len(e)-1], 1) for iter in range(len(e)-2, -1, -1): result = one_round(e[iter], result) print sum([int(k) for k in str(result.numerator)])
def generate_continued_fraction(): result = 1 while True: result = fraction(1 + 1 / (1 + result)) yield result
print('x1 : {0}'.format(x_1)) print('x2 : {0}'.format(x_2)) if __name__ == "__main__": a = (input('enter a :')) b = (input('enter b :')) c = (input('enter c :')) roots = (float(a),float(b),float(c)) # fraction calculator from fractions import fraction def add(a,b): print('result of addition:{0}'.format(a+b)) if __name__ = '__main__': a = fraction(input('enter the first fraction')) b = fraction(input('enter the second fraction')) op = input('operation to perform - add,subtract,division,multiply: ') if op == 'add': add(a,b) # even odd vending machine def even_odd_vend(num): if (num%2) == 0: print('even') else: print('odd') count = 1 while count <=9: num+=2 print(num)
from time import clock from fractions import Fraction as fraction start = clock() i = fraction(1, 2) count = 0 answer = 0 for x in range(1000): i = fraction(1, 2 + i) a, b = str(1 + i).split('/') if len(a) > len(b): answer += 1 end = clock() print answer print end - start