Code: Select all
$ cat fibo.js
const bint = require('./bint.js')
bint.onRuntimeInitialized = function() {
let zero = new bint.Bint("0")
let one = new bint.Bint("1")
let two = new bint.Bint("2")
let memo = [zero, one, one]
function fibo (n) {
if (memo[n]) {
return memo[n]
}
let k = (n / 2) | 0
let fk = fibo(k)
let fk1 = fibo(k + 1)
if ((n % 2) == 0) {
return memo[n] = fk.mul((fk1.mul(two)).sub(fk))
}
return memo[n] = (fk.mul(fk)).add(fk1.mul(fk1))
}
let n = 4784969
let f = fibo(n)
console.log(f.toString())
}
Code: Select all
$ time node fibo.js | head -c 32 ; time node fibo.js | tail -c 32
10727395641800477229364813596225
real 0m3.789s
user 0m3.688s
sys 0m0.313s
4856539211500699706378405156269
real 0m3.781s
user 0m3.703s
sys 0m0.344s
$
How to make a fast Fibonacci calculator seems a common problem.
I still don't get it.I'll try again.
NO BIGINT USE just platform level integers and doubles. I've done it in a 1.7 million element array but that is what I'm trying to avoid.
Code: Select all
t = time.time()
res = fibo(4784969)
print(time.time()-t)
t = time.time()
restr = str(res)
print(time.time()-t)
Code: Select all
pi@raspberrypi4:~ $ python3 fiboch.py
10.617712497711182
305.86755871772766
pi@raspberrypi4:~ $ python fiboch.py
13.2166340351
306.506959915
@raspberrypi4:~ $ pypy fiboch.py
6.28427696228
76.403824091
Code: Select all
pi@raspberrypi4:~ $ python3 fibo_phi.py
863.088921546936
0.012897014617919922
Code: Select all
$ time python2 fibo.py | tail -c 100
6875379936330930391815964864885353407167474856539211500699706378405156269
Run time = 0.799810886383
real 0m17.758s
user 0m17.641s
sys 0m0.094s
$ time python3 fibo.py | tail -c 100
5379936330930391815964864885353407167474856539211500699706378405156269
Run time = 0.719452999997884
real 0m17.728s
user 0m17.641s
sys 0m0.078s
$ time pypy fibo.py | tail -c 100
6875379936330930391815964864885353407167474856539211500699706378405156269
Run time = 0.412526845932
real 0m4.935s
user 0m4.703s
sys 0m0.234s
Some results for PyPy were included in the dramatic Fibonacci comedy here. Its big-number implementation is the fastest for much of the range, however, the asymptotically superior Jython wins in the end.Heater wrote: ↑Sun Jun 09, 2019 5:50 amgkreidl,
Interesting. Never heard of pypy.
On my PC at least nearly doubles the calculation speed and displaying the result is about three times faster. Using the code straight out of the github repo:Code: Select all
$ time python2 fibo.py | tail -c 100 6875379936330930391815964864885353407167474856539211500699706378405156269 Run time = 0.799810886383 real 0m17.758s user 0m17.641s sys 0m0.094s $ time python3 fibo.py | tail -c 100 5379936330930391815964864885353407167474856539211500699706378405156269 Run time = 0.719452999997884 real 0m17.728s user 0m17.641s sys 0m0.078s $ time pypy fibo.py | tail -c 100 6875379936330930391815964864885353407167474856539211500699706378405156269 Run time = 0.412526845932 real 0m4.935s user 0m4.703s sys 0m0.234s
Code: Select all
const bint = require('./Bint.js')
Code: Select all
#include <wherever/emscripten/bind.h>
...
using namespace emscripten;
...
// Binding code
EMSCRIPTEN_BINDINGS(my_class_example) {
class_<Bint>("Bint")
.constructor<std::string>()
.function("add", &Bint::operator+)
.function("sub", &Bint::operator-)
.function("mul", &Bint::operator*)
.function("toString", &Bint::toString)
;
Code: Select all
inline std::string toString() {
std::stringstream os;
os << *this;
return os.str();
}
Code: Select all
$ emcc -std=c++17 -Wall -O3 --bind -s TOTAL_MEMORY=16MB -o Bint.js Bint.cpp
Although compiling with -O0 does give a readable, non-compressed, output.var Module=typeof Module!=="undefined"?Module:{};var moduleOverrides={};var key;for(key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}Module["arguments"]=[];Module["thisProgram"]="./this.program";Module["quit"]=function(status,toThrow){throw toThrow};Module["preRun"]=[];Module["postRun"]=[];var ENVIRONMENT_IS_WEB=false;v.....
Code: Select all
bint.onRuntimeInitialized = function() {
// Do stuff with Bints
}
Code: Select all
$ time node fibo.js | tail -c 100
76875379936330930391815964864885353407167474856539211500699706378405156269
Think:3197ms
Print:140ms
real 0m3.713s
user 0m3.656s
sys 0m0.281s
Ah but you see, I'm speaking to you from the future...I cant see where BigInt is defined in the ECMA 262 (2018) document?
Actually my C++ big integers, bint, were derived from my first Karatsuba version in Javascript: https://github.com/ZiCog/fibo_4784969/b ... ratsuba.jsIf its not there then I guess you will have to use your own handwritten "bint" library, like C/C++ had to.
I had better check the C2x draft
That's great isn't it? Meets all the criteria for the challenge.Heater wrote: ↑Sun Jun 09, 2019 1:41 pmActually my C++ big integers, bint, were derived from my first Karatsuba version in Javascript: https://github.com/ZiCog/fibo_4784969/b ... ratsuba.js
It appears you wrote a version of Karatsuba multiplication in JavaScript, translated it to C++ and then translated to back to JavaScript with the result that it now runs in three seconds rather than three minutes. That's 60 times faster. By my calculations if you translated it back and forth one more time, it would run in onlyHeater wrote: ↑Sun Jun 09, 2019 1:41 pmjahboater,Ah but you see, I'm speaking to you from the future...I cant see where BigInt is defined in the ECMA 262 (2018) document?
When you catch up with where I am, about 2020 in Earthling years, BigInt will be in the ECMAScript standard. For you it is still a only a proposal in the standardization process, but as it is already implemented in Chrome, FireFox, Edge, Node.js, Babel, it makes it to final approval.
I took the liberty of checking in the BigInt JS fibo version ahead of time:)
See this historical article re: BigInt standardization here: https://golb.hplar.ch/2018/09/javascript-bigint.htmlActually my C++ big integers, bint, were derived from my first Karatsuba version in Javascript: https://github.com/ZiCog/fibo_4784969/b ... ratsuba.jsIf its not there then I guess you will have to use your own handwritten "bint" library, like C/C++ had to.
That is a tad slower at three and a half minutes![]()
Yes indeed. That is why I'm speaking to you from the future. Too much recursion creates an excessive amount of information in too small a space and time. Information is energy, energy is mass, eventually collapsing into a black hole. Sucking in the computer and anything around and spitting it out the other end of a worm hole.It appears you wrote a version of Karatsuba multiplication in JavaScript, translated it to C++ and then translated to back to JavaScript with the result that it now runs in three seconds rather than three minutes. That's 60 times faster. By my calculations if you translated it back and forth one more time, it would run in only ... 3 / 60 = 0.05 seconds.
Some years ago I did exactly that with a compiler written in C++ for the Spin Language. Spin is the language of the Parallax Inc Propeller micro-controller. Ended up with a full Spin IDE in the browser, syntax highlighting and all! https://github.com/ZiCog/openspin.jsSince FreeBasic has a C backend, it should be possible to take the C output corresponding to classic.bas and feed it into emcc to obtain yet another version in JavaScript. For lots of laughs the same could be done with the lcc compiler.
I suspect there is room for many more Fibonacci stories in this thread as well as on the GitHub.ScriptBasic wrote: ↑Sun Jun 09, 2019 5:19 pmI don't know where your Fibonacci direction is going but you have two opposite stories going.
Neither do I. Who would have guessed that everything that has happened here would have happened as a consequence of an off hand challenge thrown down in forum thread?I don't know where your Fibonacci direction is going...
I do? Quite possibly... let's see......but you have two opposite stories going...
No. I have never said such a thing.You say the challenge is about achieving one million digits not using language features that support BIGINT...
I do? How?...yet you break your own rules with every submission.
Get out of here with that unfounded and insulting nonsense.You then treat others not participating in your madness as clueless idiots.
The only official rules on this forum were stated here and say
The future of the world is at stake.liz wrote:The rules are simple. Be good to each other.
This means be kind, be civil, don't spam.
The good news is that I was able to reach your website. The bad news is that the digital apocalypse has not yet been avoided.
Sometimes nothing further happens; sometimes the big number appears after a few minutes. If I try to scroll to see the number, it disappears leaving me with a completely black screen. I couldn't find the performance metrics. Fortunately closing the webpage returns me safely to the Raspberry Pi forum.The 4784969th Fibonacci Number is: Calculator:Preparing... (0/1)
I've just loaded the page on my phone (Android 9, Exynos 9810 SoC, default Chrome), there wasn't any preparing message at the start, just a blank page until suddenly the result was displayed (after about 19 seconds). Although reloading the page (I didn't time it the first time) did show the preparing message. The result page shows my hatred for one thing in Android, 99% of the time the scroll bar is a purely visual indicator and can't be used to select the area you want to go to.ejolson wrote: ↑Sun Jun 09, 2019 11:52 pmOn my mobile phone (Android 7.1.1 with the default Chrome browser) I obtain the textSometimes nothing further happens; sometimes the big number appears after a few minutes. If I try to scroll to see the number, it disappears leaving me with a completely black screen. I couldn't find the performance metrics. Fortunately closing the webpage returns me safely to the Raspberry Pi forum.The 4784969th Fibonacci Number is: Calculator:Preparing... (0/1)
OK. It's time for me to 'fess up. There is a reason why what I present now is not a valid big fibo challenge solution...I see no problem adding the new JavaScript code to the GitHub repository...
I have already started a GMP extension module which only has one function which is fibo. If you have time maybe you can add the missing functions.For example, an extension to Script Basic that allows one to store big integers in strings and perform the five arithmetic operations addition, subtraction, multiplication, division and remainder on those strings using GMP would be fantastic.
I would guess that has something to do with ...
You appear to acknowledge that entry is disqualified from the challenge but appear to be suggesting it should be included if others cannot see why it should be disqualified.