I'm solving a problem of the sum of all even Fibonacci numbers bellow n. I do first in Julia, because it's seems to be easy to deal with large numbers. When I try rewrite in python, because the answer's submission is not available in Julia, I got another value. Someone can explain to me why I'm having this precision problem?i = 0
n = 400000000000000000
s = 0
while true
f = floor(golden^(i) / sqrt(5) + 1//2)
if f > n
break
end
s += f
i += 3
end
@printf("%d", s)
> 210098070363744768import sys
from math import pow, floor, sqrt
golden = (1 + sqrt(5)) / 2
n = 400000000000000000
s = 0
j = 3
while True:
f = floor(pow(golden, j) / sqrt(5) + 1/2)
j += 3
if f > n:
break
s += f
print(s)
> 210098070363745432
Floats get more coarse as they get larger, just use integers for your problem.
Here ya go
>Pic related
>>57923862
If I do with integer the answer lost its efficiency. The problem is time dependent.
>>57923929
then use numpy
>>57923957
The submission answer environment don't have others libraries. So I can only use default libraries.
>>57924022
Oh... Does the submission enviroment accept with the precision problem?
I know python have a problem with anything more than 15 significant digits. But I thought that only applied to floats.
>>57923922
>>57924022
I just finished my into class and we talked about algorithm
O2n or something?
>>57924051
I passed in 4 of 5 tests cases. If some one knows other language witch don't have this problem I can try.
>>57924108
That does not mean anything. What are the test cases?
>>57924164
That is the problem. I cant access them. But I believe it's the worst case where n = 400000000000000000.
>>57924192
Try a different algorithm then... Maybe list the fibbonaci numbers and sum them as a list?
>>57924280
It's not nice and lose all joy. I want understand the problem if it has a solution. If don't exist alternatives I can try anther way.
In algorithms problems this problem so recurrent than I understand it once at all.
>>57924108
try it with based haskell
Your Julia solution starts with i = 0, and your Python solution which uses j instead of i starts with j = 3
>>57923786
>babby's first project euler
lmao
>>57924107
>O2n or something?
>>57923786
meanwhile my C solution runs 10x faster
>>57923786
>uses floating point types and expects precision
>uses the slow closed formula instead of just iteratively adding
>writes code in shitty interpreted memelangs and expects it to be fast
found your problems OP