Code: Select all
DECLARE SUB fibo ALIAS "fibo" LIB "gmp"
PRINT LEN(fibo(4784969)),"\n"
1000000
real 0m4.775s
user 0m4.363s
sys 0m0.084s
pi@raspberrypi:~/sbrpi $
Code: Select all
DECLARE SUB fibo ALIAS "fibo" LIB "gmp"
PRINT LEN(fibo(4784969)),"\n"
Code: Select all
(define (fibo n)
(cond
[(equal? n 0) 0 ]
[(equal? n 1) 1 ]
[(equal? n 2) 1 ]
[else
(define k (quotient n 2))
(define a (fibo k))
(define b (fibo (- k 1)))
(if (even? n)
(* a (+ (* 2 b) a))
(let ([c (* (+ (* 2 a) b) (- (* 2 a) b))])
(if (= (modulo n 4) 1)
(+ c 2)
(- c 2)
)
)
)
]
)
)
Hey! I've been doing that too. Did the runtime improve?Heater wrote: ↑Sun Apr 21, 2019 4:41 amCool.
You are raising the fibo(4784969) challenge to a whole new meta level. Not just calculate fibo(4784969) in the language of your choice but first build a language/compiler/runtime with which to calculate fibo(4784969).
Does your extension actually produce a value that can be used in ScriptBasic? For example
a = fibo(12)
b = fibo(10000)
c = b / a
print c, "\n"
Meanwhile I shaved a few lines off my Scheme fibo and made it almost readable:Code: Select all
(define (fibo n) (cond [(equal? n 0) 0 ] [(equal? n 1) 1 ] [(equal? n 2) 1 ] [else (define k (quotient n 2)) (define a (fibo k)) (define b (fibo (- k 1))) (if (even? n) (* a (+ (* 2 b) a)) (let ([c (* (+ (* 2 a) b) (- (* 2 a) b))]) (if (= (modulo n 4) 1) (+ c 2) (- c 2) ) ) ) ] ) )
Code: Select all
DECLARE SUB fibo ALIAS "fibo" LIB "gmp"
PRINT fibo(24) + fibo(24),"\n"
PRINT FORMAT("%.0f",fibo(46)),"\n"
PRINT FORMAT("%.0f",fibo(46) / 16),"\n"
PRINT FORMAT("%.0f",1836311903 / 16),"\n"
Sorry. I woke up at an ungodly hour, could not sleep, so started tinkering.Hey! I've been doing that too.
I was expecting it to get worse. Thinking that "define" gives us a function to call whereas "let" gives a variable to reference. Turns out to be about the same:Did the runtime improve?
Code: Select all
$ time racket fibo.scheme
10727395641800477229364813596...
...0699706378405156269
real 0m1.631s
user 0m1.031s
sys 0m0.516s
It seems I might have ended up with something less readable. Namely,Heater wrote: ↑Sun Apr 21, 2019 5:10 amejolson,Sorry. I woke up at an ungodly hour, could not sleep, so started tinkering.Hey! I've been doing that too.I was expecting it to get worse. Thinking that "define" gives us a function to call whereas "let" gives a variable to reference. Turns out to be about the same:Did the runtime improve?Code: Select all
$ time racket fibo.scheme 10727395641800477229364813596... ...0699706378405156269 real 0m1.631s user 0m1.031s sys 0m0.516s
Code: Select all
#!/usr/bin/racket
#lang racket
(define (fibowork n)
(cond [(equal? n 0) '(0 1)
][else
(let ([y (fibowork (quotient n 2))])
(let ([a (car y)] [b (cadr y)])
(cond [(even? n)
(let ([t1 (- (+ b b) a)])
(cond [(equal? (modulo n 4) 0)
(list (* a t1) (sub1 (* b t1)))
][else
(list (* a t1) (add1 (* b t1)))
]))
][else
(let ([t1 (+ (+ a a) b)])
(cond [(equal? (modulo n 4) 1)
(list (add1 (* a t1)) (* b t1))
][else
(list (sub1 (* a t1)) (* b t1))
]))
])
))
])
)
(define (fiboEJO n)
(cond [(< n 2) n
][else
(let ([y (fibowork (quotient (- n 1) 2))])
(let ([a (car y)] [b (cadr y)])
(cond [(even? n)
(let ([t1 (+ (+ a a) b)])
(* b t1))
][else
(let ([t1 (- (+ b b) a)])
(cond [(equal? (modulo n 4) 1)
(sub1 (* b t1))
][else
(add1 (* b t1))
]))
])
))
])
)
(fiboEJO 4784969)
Code: Select all
$ time ./scfiboheat | tail -c32
4856539211500699706378405156269
real 0m1.635s
user 0m1.525s
sys 0m0.106s
$ time ./scfiboejo | tail -c32
4856539211500699706378405156269
real 0m1.465s
user 0m1.348s
sys 0m0.111s
"obligatory"? This is perhaps the first time I have seen anyone post REXX code anywhere!As an old OS/2 user, here is the obligatory REXX version for the fibo(4784969) challenge:
Code: Select all
$ time regina fibo.rexx
107273956418004772293648135962250043219072211732...
...211500699706378405156269
real 129m8.833s
user 128m52.891s
sys 0m0.156s
Style? I ain't got no style. I have never read enough Scheme to know what Scheme style might be. I have not read much Scheme because it's so unintelligible.When people code in Scheme with a basic lack of style, why avoid Basic?
Code: Select all
#!/usr/bin/racket
#lang racket
(define memo (make-hash))
(define (memoize n f)
(hash-set! memo n f)
f
)
(define (memoized n)
(dict-has-key? memo n)
)
(define (memoref n)
(hash-ref memo n)
)
(hash-set! memo 0 0)
(hash-set! memo 1 1)
(define (fiboCalc n)
(define k (quotient n 2))
(define a (fibo k))
(define b (fibo (- k 1)))
(if (even? n)
(memoize n (* a (+ (* 2 b) a)))
(let ([c (* (+ (* 2 a) b) (- (* 2 a) b))])
(if (= (modulo n 4) 1)
(memoize n (+ c 2))
(memoize n (- c 2))
)
)
)
)
(define (fibo n)
(if (memoized n)
(memoref n)
(fiboCalc n)
)
)
(fibo 4784969)
Code: Select all
$ time ./fibo.racket > /dev/null
real 0m1.427s
user 0m0.828s
sys 0m0.609s
$ time ./fiboejo.racket > /dev/null
real 0m1.395s
user 0m0.891s
sys 0m0.500s
I wouldn't be worried about extensions tailored to a specific language and no use to anyone else provided they were not a mandatory part of the ISA. To me that's little different to adding extensions which are only of use to those in a specific application domain and of no use outside that.
Code: Select all
#!/usr/bin/racket
#lang racket
(define memo (make-hash))
(define (memoize n f)
(hash-set! memo n f)
f)
(define (memoized n)
(dict-has-key? memo n))
(define (memoref n)
(hash-ref memo n))
(hash-set! memo 0 0)
(hash-set! memo 1 1)
(define (fiboCalc n)
(define k (quotient n 2))
(define a (fibo k))
(define b (fibo (- k 1)))
(if (even? n)
(memoize n (* a (+ (* 2 b) a)))
(let ([c (* (+ (* 2 a) b) (- (* 2 a) b))])
(if (= (modulo n 4) 1)
(memoize n (+ c 2))
(memoize n (- c 2))))))
(define (fibo n)
(if (memoized n)
(memoref n)
(fiboCalc n)))
(fibo 4784969)
Hooray! Basically, I like your new Scheme. The parenthesis look less like worms when grouped together. It's amazing that Basic, Scheme and Python were each designed for teaching.Heater wrote: ↑Sun Apr 21, 2019 1:06 pmStyle?
From the Racket Style Guide : https://docs.racket-lang.org/style
6.1 Where to Put Parentheses
Racket isn’t C. Put all closing parentheses on one line, the last line of your code.
Oh dear.How is one supposed to work like that without having seizures?Code: Select all
#!/usr/bin/racket #lang racket (define memo (make-hash)) (define (memoize n f) (hash-set! memo n f) f) (define (memoized n) (dict-has-key? memo n)) (define (memoref n) (hash-ref memo n)) (hash-set! memo 0 0) (hash-set! memo 1 1) (define (fiboCalc n) (define k (quotient n 2)) (define a (fibo k)) (define b (fibo (- k 1))) (if (even? n) (memoize n (* a (+ (* 2 b) a))) (let ([c (* (+ (* 2 a) b) (- (* 2 a) b))]) (if (= (modulo n 4) 1) (memoize n (+ c 2)) (memoize n (- c 2)))))) (define (fibo n) (if (memoized n) (memoref n) (fiboCalc n))) (fibo 4784969)
I'll see if I can get the Tiny Scheme extension module I wrote for ScriptBasic compiled for the RPi.Now, to keep up with the meta challenge, who will code a Basic in Scheme or alternately Scheme in Basic?
Why thank you. I guess I learned something whilst nodding off in front of Brian Harvey's lectures on youtube.Hooray! Basically, I like your new Scheme
This I don't understand. My gut has the totally opposite reaction.The parenthesis look less like worms when grouped together
Yes it is. Why would one inflict such torture on ones children?It's amazing that Basic, Scheme and Python were each designed for teaching.
I have to take some time to evaluate that circular meta challenge. (Get itNow, to keep up with the meta challenge, who will code a Basic in Scheme or alternately Scheme in Basic?
I think the R means it's reduced; otherwise, it should have been called the EISC-MMXIX. Do you think the EISC-MMXX could add a Fibonacci instruction?
I'd be interested how you were lead from downloading a complete package from squeak.org to grabbing anything from the outdated and usused squeakvm.org. If there is a bad description we need to fix it.Heater wrote: ↑Thu Apr 18, 2019 3:56 amTim,
So I downloaded from squeak.org. It's a bit worrying that their latest source code is from 2012 !!!
From here: http://squeakvm.org/unix/
It works perfectly well for me on Raspbian, and Ubuntu 18 running on an AWS thing, and Mac OS, I even held my nose and fired up Windows 10 in my VMWare stuff and tested it there; works fine except that for some reason Windows doesn't talk to stdio. Since I don't do the vm development (or anything else if at all possible) on Windows I can't suggest any reason for that other than, well, Windows.The big surprise is that it seems to be impossible to turn 10 lines of Smalltalk into a running program and get a result without an IDE. Except possibly for GNU Smalltalk which seems to be broken.
And yet on the aforementioned VMWare/Windows test I note that it does indeed quit if you click on the window close button. And it even nicely asks if you mean it, like a well behaved system should.Yes it does. Except none of that shows up or if it does it does not respond after Squeak has crashed. Which has done pretty quickly on every run so far. Requiring it be killed from the Task Manager.And yet it has a very typical looking menu bar at the top, with a fairly common logo item at the left top corner, that provides a menu which allows saving and exiting.
You get a whole stack of toolboxes by having an OS. To run the C source you have to use a vast array of complex tools that occupy mega(or even gigag)bytes of disk space. Just because those tools don't open windows doesn't mean they aren't there. In the case of Squeak Smallkalk it happens to default to opening a window in the normal case. It is certainly possible to change the system image to not do that and for some purposes people have done just that. It's just not interesting enough to enough people for much effort to have been put into it. If you can make a case for it that enough people would be interested in it enough to spend the time working on it, give it a go.No, I'm trying to run 10 lines of code on a computer. I don't want a tool box, I just want to run that code from the source I have.
Am I losing my mind or are you?I'd be interested how you were lead from downloading a complete package from squeak.org to grabbing anything from the outdated and usused squeakvm.org.
It fails with the error I reported previously.sudo ./Squeak5.2-18229-64bit-All-in-One.app/Contents/Linux-x86_64/bin/squeak ./Squeak5.2-18229-64bit-All-in-One.app/Contents/Resources/Squeak5.2-18229-64bit.image ./bigFib.cs
Yes please.If there is a bad description we need to fix it.
Good question.Why not just use the Pi, Squeaks on there in the Desktop bedroom by default.
No. Just no.You could even add it to Scratch ? Or try do it all in Scratch?
Code: Select all
DECLARE SUB InitNew ALIAS "TS_init_new" LIB "ts"
DECLARE SUB Deinit ALIAS "TS_deinit" LIB "ts"
DECLARE SUB LoadStr ALIAS "TS_load_string" LIB "ts"
sc = InitNew()
LoadStr(sc, "(load \"init.scm\")")
mandel = """
(newline)
(display "Ascii Mandelbrot TinyScheme") (newline)
(display "---------------------------") (newline)
(newline)
(define sq
(lambda (x) (* x x)))
(define (1+ x) (+ x 1))
(define (1- x) (- x 1))
(define level
(lambda (i x y rc ic it orb)
(if (or (= i it) (> orb 4)) i
(level (1+ i) (+ rc (- (sq x) (sq y))) (+ ic (* 2 x y)) rc ic it (+ (sq x) (sq y))))))
(define sq
(lambda (x) (* x x)))
(define (1+ x) (+ x 1))
(define (1- x) (- x 1))
(define level
(lambda (i x y rc ic it orb)
(if (or (= i it) (> orb 4)) i
(level (1+ i) (+ rc (- (sq x) (sq y))) (+ ic (* 2 x y)) rc ic it (+ (sq x) (sq y))))))
(define mlevel
(lambda (L)
(level 0 (cadr L) (car L) (cadr L) (car L) 11 0)))
(define (main)
(let ((cnt 0) (lvl 0) (xo -1.7) (yo -2.3) (dz 0.1) )
(do ((i 0 (1+ i)))
((= i 30))
(do ((j 0 (1+ j)))
((= 30 j))
(set! lvl (mlevel (list (+ xo (* i dz)) (+ yo (* j dz)) )))
(if (< lvl 10)
(begin (display lvl) (display " "))
(display lvl))
(set! cnt (1+ cnt))
(when (= 30 cnt)
(set! cnt 0)
(newline))
))))
(main)
(quit)
"""
PRINT LoadStr(sc, mandel),"\n"
Deinit sc
Code: Select all
jrs@jrs-laptop:~/sb/examples/test$ time scriba mandelisp.sb
Ascii Mandelbrot TinyScheme
---------------------------
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2
1 1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 4 4 4 115 4 4 3 3 2 2 2
1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 4 4 4 5 7 9 114 4 3 3 2 2
1 1 1 1 1 1 2 3 3 3 3 3 3 3 3 4 4 4 4 5 6 9 118 5 4 4 3 3 3
1 1 1 1 1 2 3 3 3 3 3 3 3 3 4 4 4 4 5 6 8 1111116 5 5 4 3 3
1 1 1 1 1 2 3 3 3 3 3 3 3 4 4 4 5 7 8 8 101111119 6 6 5 4 3
1 1 1 1 2 3 3 3 3 3 3 3 4 4 5 5 6 11111111111111111111114 3
1 1 1 1 2 3 3 3 3 3 4 5 5 5 5 6 8 111111111111111111117 5 3
1 1 1 1 3 3 3 3 4 5 7 7 7 7 7 7 11111111111111111111119 5 4
1 1 1 1 3 4 4 4 5 5 7 111111119 1111111111111111111111116 4
1 1 1 1 4 4 4 5 5 6 8 11111111111111111111111111111111115 4
1 1 1 1 4 4 6 6 7 1111111111111111111111111111111111118 5 4
1 1 1 1111111111111111111111111111111111111111111111117 5 4
1 1 1 1 4 4 6 6 7 1111111111111111111111111111111111118 5 4
1 1 1 1 4 4 4 5 5 6 8 11111111111111111111111111111111115 4
1 1 1 1 3 4 4 4 5 5 7 111111119 1111111111111111111111116 4
1 1 1 1 3 3 3 3 4 5 7 7 7 7 7 7 11111111111111111111119 5 4
1 1 1 1 2 3 3 3 3 3 4 5 5 5 5 6 8 111111111111111111117 5 3
1 1 1 1 2 3 3 3 3 3 3 3 4 4 5 5 6 11111111111111111111114 3
1 1 1 1 1 2 3 3 3 3 3 3 3 4 4 4 5 7 8 8 101111119 6 6 5 4 3
1 1 1 1 1 2 3 3 3 3 3 3 3 3 4 4 4 4 5 6 8 1111116 5 5 4 3 3
1 1 1 1 1 1 2 3 3 3 3 3 3 3 3 4 4 4 4 5 6 9 118 5 4 4 3 3 3
1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 4 4 4 5 7 9 114 4 3 3 2 2
1 1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 4 4 4 115 4 4 3 3 2 2 2
1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2
real 0m0.755s
user 0m0.753s
sys 0m0.001s
jrs@jrs-laptop:~/sb/examples/test$
Code: Select all
DECLARE SUB InitNew ALIAS "TS_init_new" LIB "ts"
DECLARE SUB Deinit ALIAS "TS_deinit" LIB "ts"
DECLARE SUB LoadStr ALIAS "TS_load_string" LIB "ts"
sc = InitNew()
LoadStr(sc, "(load \"init.scm\")")
fibolisp = """
(define fibonacci (lambda (n)
(if (< n 2)
n
(+ (fibonacci (- n 1))
(fibonacci (- n 2)))))
)
(display (fibonacci 24))
"""
PRINT LoadStr(sc, fibolisp)
Deinit sc
Code: Select all
...Squeak5.2-18229-64bit-201810190412-Windows>Squeak.exe Squeak5.2-18229-64bit.image bigFib.cs
AIR@AllBASIC.info wrote: GO implementation:OutputCode: Select all
package main import ( "fmt" "math/big" ) func Mul(x, y *big.Int) *big.Int { return big.NewInt(0).Mul(x, y) } func Sub(x, y *big.Int) *big.Int { return big.NewInt(0).Sub(x, y) } func Add(x, y *big.Int) *big.Int { return big.NewInt(0).Add(x, y) } func fib(n int) (*big.Int, *big.Int) { if n == 0 { return big.NewInt(0), big.NewInt(1) } a, b := fib(n / 2) c := Mul(a, Sub(Mul(b, big.NewInt(2)), a)) d := Add(Mul(a, a), Mul(b, b)) if n%2 == 0 { return c, d } else { return d, Add(c, d) } } func main() { a, _ := fib(4784969) fmt.Println(a) }
Code: Select all
Without printing the result real 0m0.352s user 0m0.342s sys 0m0.013s[/tt] With print [tt]real 0m2.632s user 0m2.597s sys 0m0.021s Test System: system_profiler SPHardwareDataType Hardware: Hardware Overview: Model Name: Mac mini Model Identifier: Macmini6,2 Processor Name: Intel Core i7 Processor Speed: 2.3 GHz Number of Processors: 1 Total Number of Cores: 4 L2 Cache (per Core): 256 KB L3 Cache: 6 MB Memory: 16 GB
I would greatly enjoy seeing that scratchy orange cat open it's mouth and utter the famous million-digit Fibonacci number. Given the TIOBE popularity of Scratch, I expect there are many who would be able to write (or click, drag and drop) the code. However, since Scratch apparently can't do file input or output, I'm not sure who would check that all the digits are correct.
Slight modifications of the GNU Basic Calculator Fibonacci code yielded a program compatible with the version of Basic Calculator included with Research Unix Release 7 as distributed by Charles Haley and Dennis Richie in 1979. The modified code looks like
Code: Select all
define f(n){
if(n==0){
a=0; b=1;
return(a);
}
y=f(n/2);
if(n%2==0){
t=2*b-a; a=a*t; b=b*t;
if(n%4==0) {
b=b-1;
return(a);
}
b=b+1;
return(a);
}
t=2*a+b; a=a*t; b=b*t;
if(n%4==1){
a=a+1;
return(a);
}
a=a-1;
return(a);
}
f(7499);
quit;
Code: Select all
$ time bc fibo.bc >fibo.txt
real 30.0
user 23.0
sys 3.0
$ cat fibo.txt
706039879937678439008043140315376240713352339053705987810495062278638\
8718825722561078363937111808052845829043934364899297524579596771135537\
4801002080270853698968059366022272185817616026077096023349398894847983\
9714294221838897832623108356114939220536986382598957620755114616454599\
1161518248812602059748659792822539047900440615827674362997814592886203\
6233871777454827819617640377892562042118765168677553893278624710989272\
0750453526082811016759670376082246298871700521370426055551726269306739\
0672579278642451139772159098237484766060153417966500228867893493613281\
8993979198118179855263939840784481752316021163437326695353825928661768\
3002190127657350138849014296026957342968901034215368495591044620609723\
6075006861770107503447313897585687337597851533416564601034740128818628\
8645591663416538717475614601835764250489591524740065243768671081547198\
2672495157781105852040979589027195320750103738958083706562749130366493\
9319008074326639606500449899956261137209763009997639133968161102379352\
1005265212193190096689493605090171903058222057356370475825968443610545\
3657010557565875895812678161531478689730791188599192215196196394651362\
1905753392384260111071739837465056829372023129875545405507382500246527\
4343153922152993044195779337044156381692291477614825519892105365222191\
0649719893063976464871428717392844399965747793496261915059129980848401\
5221733892947047898044789624388717645430081789847014505889073245674298\
2274890606022749963374408459031409736132841733310512973055666695851121\
5784820984339191265679662767763463393454227116481221490807304074849241\
9868112329633702241675255001
Code: Select all
DECLARE SUB InitNew ALIAS "TS_init_new" LIB "ts"
DECLARE SUB Deinit ALIAS "TS_deinit" LIB "ts"
DECLARE SUB LoadStr ALIAS "TS_load_string" LIB "ts"
sc = InitNew()
fibolisp = """
(define fibonacci (lambda (n)
(if (< n 2)
n
(+ (fibonacci (- n 1))
(fibonacci (- n 2)))))
)
"""
ok = LoadStr(sc, fibolisp)
FOR i = 1 TO 24
results = LoadStr(sc, "(display (fibonacci " & STR(i) & " ))\n")
PRINT results,"\n"
NEXT
Deinit sc