[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Losting precision

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 20
Thread images: 5

File: Fibonacci-leonardo-pisa.jpg (223KB, 1260x840px) Image search: [Google]
Fibonacci-leonardo-pisa.jpg
223KB, 1260x840px
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)

> 210098070363744768

import 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.
>>
File: trump-golden-ratio-fibonacci.jpg (58KB, 600x371px) Image search: [Google]
trump-golden-ratio-fibonacci.jpg
58KB, 600x371px
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.
>>
File: napknnf8iil5b6fpkjqt.jpg (812KB, 1500x634px) Image search: [Google]
napknnf8iil5b6fpkjqt.jpg
812KB, 1500x634px
>>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.
>>
File: haskell-logo.jpg (25KB, 431x172px) Image search: [Google]
haskell-logo.jpg
25KB, 431x172px
>>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
>>
File: 1312324763893.png (120KB, 387x340px) Image search: [Google]
1312324763893.png
120KB, 387x340px
>>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
Thread posts: 20
Thread images: 5


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.