Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def mul( ab1, ab2):
- """(a1 + b1*sqrt(3)) * (a2 + b2*sqrt(3)) """
- a1, b1 = ab1
- a2, b2 = ab2
- return (a1*a2 + 3*b1*b2, a1*b2 + a2*b1)
- def square(ab):
- return mul(ab, ab)
- def sqrti( n ):
- """integer part of the square root, for long arithmetics"""
- x = 1
- while True:
- x1 = (x + n//x)//2
- x12 = x1*x1
- if x12 <= n and x12 + 2*x1+1 > n:
- return x1
- x = x1
- def intpart( ab ):
- #integer part of a+b*sqrt(3)
- # is a + int(sqrt(b*b*3))
- a, b = ab
- return a + sqrti(b*b*3)
- def power( ab, n ):
- if n == 0: return (1,0)
- if n == 1: return ab
- ab_n2 = square(power(ab, n // 2))
- if n % 2: return mul(ab_n2, ab)
- else: return ab_n2
- def smul( ab, k):
- a,b=ab
- return (a*k, b*k)
- def subtract_int(ab, n):
- a,b=ab
- return (a-n,b)
- def decimal_digits( ab, n ):
- x0 = intpart(ab)
- ab = subtract_int(ab, x0)
- digits = []
- while len(digits)!=n:
- ab = smul(ab, 10)
- digit = intpart(ab)
- digits.append(digit)
- ab = subtract_int(ab, digit)
- return x0, digits
- N = 2015
- num_digits = 10
- print ("Calculating (1+sqrt(3))**{N}".format(N=N))
- x0, digits = decimal_digits( power( (1,1), N), num_digits )
- print ("Integer part:")
- print (x0)
- print ("Digits after deciaml point")
- print ("".join(map(str,digits)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement