0

A simple fibonacci number quiz

I have a number 726, 726 isn't fibo, then sum it with reverse of it self, 726+627, it will keep sum until the number is fibonacci and print out how many times of sum needed until it fibo number. constraint : 1 < n < 1000 n = times of sum needed until it is fibo number.

25th Nov 2017, 9:19 AM
Momo Belia
Momo Belia - avatar
27 Antworten
+ 5
I hope it's okay to interject... the problem is that you're dropping to floats (square root). Python has arbitrary *integer* precision, but *float* precision is 15.8 digits. Further, the answer you get depends on the underlying C library, which varies across operating systems (a separate issue I've been tracking from a discrepancy in Numpy; but again, it's the OS and not Python). I can show the "float" issue most relevantly to SoloLearn by modifying Sayan's code from the "stairs-lambdas" challenge: import math, operator from functools import reduce # desired fib() step (actually step 80) x = 78 c=lambda n,a=1,b=0 : c(n-1, a+b, a) if n>1 else str(a+b) d=lambda n: str(round((1/(5)**0.5)*((0.5*(1+(5)**0.5))**(1+n)))) d1=lambda n: "{}".format(round(1./math.sqrt(5) * pow( ( 1+math.sqrt(5) ) /2, 1+n) )) d2=lambda n: "{}".format(round(reduce(operator.mul, [( 1+math.sqrt(5) ) for _ in range(1+n)], 1 ) / (2**(n+1)*math.sqrt(5)) )) print(c(x)) print(d(x), d1(x), d2(x), sep="\n") OUTPUT: ------------------------------------ 14472334024676221 # correct 14472334024676260 # incorrect 14472334024676260 # incorrect 14472334024676254 # slightly less incorrect (less division) ^ 15.8 digits of precision # accuracy of floats (cast to integers) Compare to Javascript, which loses integer precision at step 80 ... because all numbers in Javascript are floats: https://code.sololearn.com/W0izfx5FISNx/?ref=app
25th Nov 2017, 7:00 PM
Kirk Schafer
Kirk Schafer - avatar
25th Nov 2017, 10:36 AM
sayan chandra
sayan chandra - avatar
+ 1
sayan Chandra solution are very interesting @@, but unfortunately I can't python :>
25th Nov 2017, 1:02 PM
Momo Belia
Momo Belia - avatar
+ 1
@Sayan Maybe, but my Python code gives the same result as C# code
25th Nov 2017, 1:29 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
+ 1
@ For informtion>>>> vadim )) IF EITHER OF 5n^2+4 OR 5n^2-4 OR BOTH ARE PERFECT SQUARE THEN n is a fibonacci... as of here...reverse and sum up... evry number will get to a fib.. so its perfect solution IT HAS A PROOF..TRUST ME.. TOO BIG AND COMPLEX TO POST IN HERE..
25th Nov 2017, 5:27 PM
sayan chandra
sayan chandra - avatar
+ 1
## @ vadim ## if u still want proof i can do it for u in another way... for n in range (1,1000000): a,b=(5*n**2-4),(5*n**2+4) if a**0.5==int(a**0.5) or b**0.5==int(b**0.5): print(n) ## paste it and run... ## YOU WILL SEE IT WILL ONLY pRINT THE FIBO NUMBERS...LIMIT UPTO 1000K
25th Nov 2017, 5:46 PM
sayan chandra
sayan chandra - avatar
+ 1
@Sayan. I trust. Means, I was wrong. But then why your program gives number 972845626549368 which isn't Fibonacci's number? The program beautiful and the idea beautiful, it seems, everything has to be right. Or it is my generator not all numbers of Fibonacci shows?
25th Nov 2017, 5:50 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
+ 1
oohoo i mistook 1 thing its my typo error... if either of the case is perfect square...not both... ok @ vadim...
25th Nov 2017, 6:03 PM
sayan chandra
sayan chandra - avatar
25th Nov 2017, 12:50 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
0
@Sayan, you made very interest solution. But my direct algorithm do not found solution for 726. Why ?
25th Nov 2017, 1:01 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
0
@vadim tell me the process that u r doing... i mean the working method of ur code..
25th Nov 2017, 1:03 PM
sayan chandra
sayan chandra - avatar
0
@Sayan. You see answer with my solutions under your answer with two solutions.
25th Nov 2017, 1:15 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
0
thats why im telling you... just tell me..how ur code works... tell me the thought behind it that you implemented...
25th Nov 2017, 1:30 PM
sayan chandra
sayan chandra - avatar
0
@Sayan. nextFibo = next(fibonacci) n = 0 while n < 1000: while nextFibo < number: nextFibo = next(fibonacci) #print("F", nextFibo) if nextFibo == number: print("n={} number={}". format(n, number)) break while nextFibo > number: number += reverseInt(number) n += 1 else: print("Not found") https://code.sololearn.com/cHKXQ62qrhkO/?ref=app
25th Nov 2017, 1:48 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
0
@Sayan. This code shows that the number given by your code isn't Fibonacci's number. https://code.sololearn.com/czO2kMmkLSBY
25th Nov 2017, 3:23 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
0
@Sayan It seems to me that I have understood your code. You consider that the number is Fibonacci's number if the formula of receiving number of Fibonacci from previous gives for this number an exact square in one of two results. It seems to me that this condition is incorrect. More precisely, you confuse necessary and sufficient. Performance of this condition is necessary in order that the number was Fibonacci's number, but it isn't enough.
25th Nov 2017, 5:14 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
0
@Sayan Yes, your test work normally. But I made in a different way: I compared the next numbers of Fibonacci determined by my and your method. There is a result: 1 1 2 2 3 3 5 5 8 8 13 13 21 21 34 34 55 55 89 89 144 144 233 233 377 377 610 610 987 987 1597 1597 2584 2584 4181 4181 6765 6765 10946 10946 17711 17711 28657 28657 46368 46368 75025 75025 121393 121393 196418 196418 317811 317811 514229 514229 832040 832040 1346269 1346269 2178309 2178309 3524578 3524578 5702887 5702887 9227465 9227465 14930352 14930352 24157817 24157817 39088169 39088169 63245986 63245986 102334155 70711162 165580141 94868979 267914296 102334155 433494437 126491972 701408733 133957148 That is the discrepancy began with number 70711162 which is obviously not the amount two previous, but is recognized as your program for Fibonacci number. Perhaps, a problem in accuracy arithmeticians of Python ?
25th Nov 2017, 6:25 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
0
@Sayan This is my test code: def fiboSayan(): a = 0 while True: while not (lambda p,q: p==int(p) or q==int(q)) ((5*a**2+4)**0.5, (5*a**2-4)**0.5): a += 1 # print("=== a=", a) yield a a += 1 def fibo(): f1 = 0 yield f1 f2 = 1 yield f2 while True: f1 += f2 yield f1 f2 += f1 yield f2 myfibo = fibo() Sayan = fiboSayan() next(myfibo) next(myfibo) next(Sayan) for i in range(50): print (next(myfibo), next(Sayan))
25th Nov 2017, 6:28 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
0
@Sayan See this test: >>> a = 70711162 >>> print((lambda p,q: (p==int(p) and q!=int(q)) or (q==int(q) and p!=int(p))) ( (5*a**2+4)**0.5, (5*a**2-4)**0.5)) True >>> print ((5*a**2+4)**0.5, (5*a**2-4)**0.5) 158114965.0 158114964.99999997 >>>
25th Nov 2017, 6:41 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
0
@Sayan Yes, a problem in Python arithmetics accuracy!
25th Nov 2017, 6:42 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar