| read two numbers from one input string, add and display
| total
[] $s | read string from console
getitem $s x | get first item
getitem $s y | get second item
+ x y | add items
y [] | display on console
| using the Haversine formula as a subroutine with
| parameters and return value
| when subroutine precedes first executable
| instruction, start LABEL must be used
lat1 : 0.0
lon1 : 0.0
lat2 : 0.0
lon2 : 0.0
dx : 0.
dy : 0.
dz : 0.
kms : 0.
k : 0.
haversine param lat1 lon1 lat2 lon2
{degtorad(lon2 - lon1)} lon1
{degtorad lat1} lat1
{degtorad lat2} lat2
{sin lat1 - sin lat2} dz
{cos lon1 * cos lat1 - cos lat2} dx
{sin lon1 * cos lat1} dy
{arcsin(sqrt(dx^2 + dy^2 + dz^2)/2) * 12745.6} kms
return kms
start
call haversine 36.12 -86.67 33.94 -118.4
result k
"'Haversine distance: ' k ' kilometers'" []
| Find all Munchausen numbers between 1 and 5000
x : 10 1
(2^2) x.2
(3^3) x.3
(4^4) x.4
(5^5) x.5
(6^6) x.6
(7^7) x.7
(8^8) x.8
(9^9) x.9
1 i
if i <= 5000
~ i $i | convert binary to string
#$i j | length to j
y | set y to 0
if j > 0
$i.j $j 1 | move digit j to string j
~ $j n | convert j string to binary
+ x.n y | add value x at n to y
- j | dec j
goif
endif
if i = y
i [] | output to console
endif
+ i
goif
endif
| the prog (T1) will cause 99 progs to run in SSX
symsyn 'R T1' | command to Symsyn to run T1
loop
delay 100 | sleep for 100 milliseconds
go loop
| the square root of the sum of the squares
x : 2 0 | array (vector) x ( 2 members each a value of 0 )
3 x.0 | first member = 3
4 x.1 | second member = 4
vmul x x x | multiply each member of array x by same in x, store in x
vsum x y | add all the members of array x place total in word y
sqrt y z | square root of y in z (rounded) = 5
| DES encryption
key : x'0e329232ea6d0d73'
data : x'8787878787878787'
edes key data | encrypt data with key
data $s | move data to string
unpackhex $s $s | unpack
$s [] | output result - 0000000000000000
| quick sort
x : 23 : 15 : 99 : 146 : 3 : 66 : 71 : 5 : 23 : 73 : 19
quicksort param l r
l i
r j
((l+r) shr 1) k
x.k pivot
repeat
if pivot > x.i
+ i
goif
endif
if pivot < x.j
- j
goif
endif
if i <= j
swap x.i x.j
- j
+ i
endif
if i <= j
go repeat
endif
if l < j
save l r i j
call quicksort l j
restore l r i j
endif
if i < r
save l r i j
call quicksort i r
restore l r i j
endif
return
start
' original values : ' $r
call showvalues
call quicksort 0 10
' sorted values : ' $r
call showvalues
stop
showvalues
$s
i
if i <= 10
"$s ' ' x.i ' '" $s
+ i
goif
endif
" $r $s " []
return
| binary search
a : 1 : 2 : 27 : 44 : 46 : 57 : 77 : 154 : 212
binary_search param item index size
index saveindex
index L
(index + size - 1) H
if L <= H
((L + H) shr 1) M
if base.M > item
- 1 M H
else
if base.M < item
+ 1 M L
else
- saveindex M
return M
endif
endif
goif
endif
return -1
start
call binary_search 77 @a #a
result R
"'result = ' R" []
| bubble sort
x : 23 : 15 : 99 : 146 : 3 : 66 : 71 : 5 : 23 : 73 : 19
bubble_sort param index size
+ index size limit
lp
changes
- limit
index i
if i < limit
+ 1 i ip1
if base.i > base.ip1
swap base.i base.ip1
+ changes
endif
+ i
goif
endif
if changes > 0
go lp
endif
return
start
' original values : ' $r
call showvalues
call bubble_sort @x #x
' sorted values : ' $r
call showvalues
stop
showvalues
$s
i
if i < #x
"$s ' ' x.i ' '" $s
+ i
goif
endif
" $r $s " []
return
| Population Count from Rosettacode
| Pop Count 3^i
i
if i < 30
(3^i) x
popcount x 63 x
~ x $r
+ $r $s
+ ' ' $s
+ i
goif
endif
"' Pop Count 3^i : ' $s " []
| Evil Numbers
i
cnt
if cnt < 30
popcount i 7 x
x:0:1 y
if y <> 1
+ cnt
~ i $r
+ $r $e
+ ' ' $e
endif
+ i
goif
endif
"' Evil Numbers : ' $e " []
| Odious Numbers
i
cnt
if cnt < 30
popcount i 7 x
x:0:1 y
if y = 1
+ cnt
~ i $r
+ $r $o
+ ' ' $o
endif
+ i
goif
endif
"' Odious Numbers : ' $o " []
| Three Word Location - convert latitude and longitude to three words
lat : 28.3852
lon : -81.5638
| build word array W00000 ... W28125
i
if i <= 28125
~ i $r
#$r szr
'W00000' $t
(6-szr) szr
szr #$t
+ $r $t
+ $t $wordarray
+ i
goif
endif
| make latitude and longitude positive integers
{lat * 10000 + 900000} ilat
{lon * 10000 + 1800000} ilon
| build 43 bit integer containing latitude (21 bits) and longitude (22 bits)
ilat latlon
shl latlon 22
+ ilon latlon
| isolate most significant 15 bits for word 1 index
| next 14 bits for word 2 index
| next 14 bits for word 3 index
latlon:42:15 w1
latlon:27:14 w2
latlon:13:14 w3
| fetch each word from word array
(w1*6+1) w1
$wordarray.w1 $w1 6
(w2*6+1) w2
$wordarray.w2 $w2 6
(w3*6+1) w3
$wordarray.w3 $w3 6
| display words
"$w1 ' ' $w2 ' ' $w3" []
| reverse the procedure
| look up each word
call bsearch 0 28125 $w1
result w1index
call bsearch 0 28125 $w2
result w2index
call bsearch 0 28125 $w3
result w3index
| build the latlon integer from the word indexes
w1index latlon
shl latlon 14
+ w2index latlon
shl latlon 14
+ w3index latlon
| isolate the latitude and longitude
latlon:21:22 ilon
latlon:42:21 ilat
| convert back to floating point values
{(ilon - 1800000) / 10000} lon
{(ilat - 900000) / 10000} lat
| display values
"'latitude = ' lat ' longitude = ' lon" []
stop
bsearch
param L H $word
if L <= H
((L + H) shr 1) M
(M*6+1) I
$wordarray.I $w 6
if $w > $word
- 1 M H
else
if $w < $word
+ 1 M L
else
return M
endif
endif
goif
endif
return -1
|Trabb Pardo–Knuth algorithm
a : 11 0
i
if i LE 10
[] $s
~ $s w
w a.i
+ i
goif
endif
10 i
if i GE 0
call f
if x GT 400
'too large' $s
else
~ x $s
endif
~ i $r
+ ' ' $r
+ $r $s.1
$s []
- i
goif
endif
stop
f
a.i t
* t t x
* x t x
* 5 x
abs t
sqrt t y
+ y x
return
| pernicious numbers
primes : 0b0010100000100000100010100010000010100000100010100010100010101100
| the first 25 pernicious numbers
$T | clear string
num_pn | set to zero
2 n | start at 2
5 hi_bit
if num_pn LT 25
call popcount | count ones
if primes bit pop_cnt | if pop_cnt bit of bit vector primes is one
+ num_pn | inc number of pernicious numbers
~ n $S | convert to decimal string
+ ' ' $S | pad a space
+ $S $T | add to string $T
endif
+ pop_cnt | next number (odd) has one more bit than previous (even)
+ n | next number
if primes bit pop_cnt
+ num_pn
~ n $S
+ ' ' $S
+ $S $T
endif
+ n
goif | go back to if
endif
$T [] | display numbers
| pernicious numbers in range 888888877 .. 888888888
$T | clear string
num_pn | set to zero
888888876 n | start at 888888876
29 hi_bit
if n LE 888888888
call popcount | count ones
if primes bit pop_cnt | if pop_cnt bit of bit vector primes is one
+ num_pn | inc number of pernicious numbers
~ n $S | convert to decimal string
+ ' ' $S | pad a space
+ $S $T | add to string $T
endif
+ pop_cnt | next number (odd) has one more bit than previous (even)
+ n | next number
if primes bit pop_cnt
+ num_pn
~ n $S
+ ' ' $S
+ $S $T
endif
+ n
goif | go back to if
endif
$T [] | display numbers
stop
popcount | count ones in bit field
pop_cnt | pop_cnt to zero
1 bit_num | only count even numbers so skip bit 0
if bit_num LE hi_bit
if n bit bit_num
+ pop_cnt
endif
+ bit_num
goif
endif
return
OUTPUT
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 37
888888877 888888878 888888880 888888883 888888885 888888886 888888889