If you need some teasers for TS -- quickly written : the reward of the King (story goes when the King asked the inventor of the game of chess about his price ,he answered to start with one grain and double it every next field (rice / corn .. etc ). The King answered, is that all ?! -- however some calculations learn the amount is immense .....
2 things in the code :
the named let -- how to do "pseudo" iterations (the use name "loop" is just another name -- run , do-it etc.. also will do)
how to mark the number is inexact : use 1.0 i.o. 1 (otherwise it will run out of capable numbers)
Script BASIC and the TinyScheme extension module are 64 bit in my example. Rob's 32 bit overflow hack he used on his PC code was removed.
' King's Reward
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\")")
reward = """
(define (grains x)
(let loop ((i 1) (j 1))
(display " field ") (display i)
(display " number of grains ")(display j) (newline)
(when (< i x) (loop (+ i 1) (* 2 j) ))))
(define (main)
(display "The reward of the King") (newline)
(display "----------------------") (newline)
(newline)
(grains 64))
(main)
"""
PRINT LoadStr(sc, reward)
Deinit sc
Outputjrs@laptop:~/sb/sb22/sblisp/Rob$ time scriba king.sb
The reward of the King
----------------------
field 1 number of grains 1
field 2 number of grains 2
field 3 number of grains 4
field 4 number of grains 8
field 5 number of grains 16
field 6 number of grains 32
field 7 number of grains 64
field 8 number of grains 128
field 9 number of grains 256
field 10 number of grains 512
field 11 number of grains 1024
field 12 number of grains 2048
field 13 number of grains 4096
field 14 number of grains 8192
field 15 number of grains 16384
field 16 number of grains 32768
field 17 number of grains 65536
field 18 number of grains 131072
field 19 number of grains 262144
field 20 number of grains 524288
field 21 number of grains 1048576
field 22 number of grains 2097152
field 23 number of grains 4194304
field 24 number of grains 8388608
field 25 number of grains 16777216
field 26 number of grains 33554432
field 27 number of grains 67108864
field 28 number of grains 134217728
field 29 number of grains 268435456
field 30 number of grains 536870912
field 31 number of grains 1073741824
field 32 number of grains 2147483648
field 33 number of grains 4294967296
field 34 number of grains 8589934592
field 35 number of grains 17179869184
field 36 number of grains 34359738368
field 37 number of grains 68719476736
field 38 number of grains 137438953472
field 39 number of grains 274877906944
field 40 number of grains 549755813888
field 41 number of grains 1099511627776
field 42 number of grains 2199023255552
field 43 number of grains 4398046511104
field 44 number of grains 8796093022208
field 45 number of grains 17592186044416
field 46 number of grains 35184372088832
field 47 number of grains 70368744177664
field 48 number of grains 140737488355328
field 49 number of grains 281474976710656
field 50 number of grains 562949953421312
field 51 number of grains 1125899906842624
field 52 number of grains 2251799813685248
field 53 number of grains 4503599627370496
field 54 number of grains 9007199254740992
field 55 number of grains 18014398509481984
field 56 number of grains 36028797018963968
field 57 number of grains 72057594037927936
field 58 number of grains 144115188075855872
field 59 number of grains 288230376151711744
field 60 number of grains 576460752303423488
field 61 number of grains 1152921504606846976
field 62 number of grains 2305843009213693952
field 63 number of grains 4611686018427387904
field 64 number of grains -9223372036854775808
real 0m0.042s
user 0m0.040s
sys 0m0.000s
jrs@laptop:~/sb/sb22/sblisp/Rob$