Data update
This commit is contained in:
parent
796d366b97
commit
35bcdeebf8
504 changed files with 7045 additions and 610 deletions
12
Task/100-doors/Informix-4GL/100-doors-2.4gl
Normal file
12
Task/100-doors/Informix-4GL/100-doors-2.4gl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
(var doors (times 100 false))
|
||||
|
||||
(for i (range 1 101)
|
||||
i2 (range (dec i) 100 i)
|
||||
(var! doors (set-at [i2] (! (i2 doors))))
|
||||
(continue))
|
||||
|
||||
(-> (xmap vec doors)
|
||||
(filter 1)
|
||||
(map (comp 0 inc))
|
||||
(join ", ")
|
||||
@(str "open doors: "))
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
func ok v t[] .
|
||||
for h in t[]
|
||||
if v = h
|
||||
return 0
|
||||
.
|
||||
.
|
||||
return 1
|
||||
.
|
||||
proc four lo hi uni show . .
|
||||
#
|
||||
subr bf
|
||||
for f = lo to hi
|
||||
if uni = 0 or ok f [ a c d g e ] = 1
|
||||
b = e + f - c
|
||||
if b >= lo and b <= hi and (uni = 0 or ok b [ a c d g e f ] = 1)
|
||||
solutions += 1
|
||||
if show = 1
|
||||
for h in [ a b c d e f g ]
|
||||
write h & " "
|
||||
.
|
||||
print ""
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
subr ge
|
||||
for e = lo to hi
|
||||
if uni = 0 or ok e [ a c d ] = 1
|
||||
g = d + e
|
||||
if g >= lo and g <= hi and (uni = 0 or ok g [ a c d e ] = 1)
|
||||
bf
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
subr acd
|
||||
for c = lo to hi
|
||||
for d = lo to hi
|
||||
if uni = 0 or c <> d
|
||||
a = c + d
|
||||
if a >= lo and a <= hi and (uni = 0 or c <> 0 and d <> 0)
|
||||
ge
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
print "low:" & lo & " hi:" & hi & " unique:" & uni
|
||||
acd
|
||||
print solutions & " solutions"
|
||||
print ""
|
||||
.
|
||||
four 1 7 1 1
|
||||
four 3 9 1 1
|
||||
four 0 9 0 0
|
||||
23
Task/ABC-problem/EasyLang/abc-problem.easy
Normal file
23
Task/ABC-problem/EasyLang/abc-problem.easy
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
b$[][] = [ [ "B" "O" ] [ "X" "K" ] [ "D" "Q" ] [ "C" "P" ] [ "N" "A" ] [ "G" "T" ] [ "R" "E" ] [ "T" "G" ] [ "Q" "D" ] [ "F" "S" ] [ "J" "W" ] [ "H" "U" ] [ "V" "I" ] [ "A" "N" ] [ "O" "B" ] [ "E" "R" ] [ "F" "S" ] [ "L" "Y" ] [ "P" "C" ] [ "Z" "M" ] ]
|
||||
len b[] len b$[][]
|
||||
global w$[] cnt .
|
||||
#
|
||||
proc backtr wi . .
|
||||
if wi > len w$[]
|
||||
cnt += 1
|
||||
return
|
||||
.
|
||||
for i = 1 to len b$[][]
|
||||
if b[i] = 0 and (b$[i][1] = w$[wi] or b$[i][2] = w$[wi])
|
||||
b[i] = 1
|
||||
backtr wi + 1
|
||||
b[i] = 0
|
||||
.
|
||||
.
|
||||
.
|
||||
for s$ in [ "A" "BARK" "BOOK" "TREAT" "COMMON" "SQUAD" "CONFUSE" ]
|
||||
w$[] = strchars s$
|
||||
cnt = 0
|
||||
backtr 1
|
||||
print s$ & " can be spelled in " & cnt & " ways"
|
||||
.
|
||||
|
|
@ -1,8 +1,11 @@
|
|||
(var find-idx #(when (let found (find % %1)) (idx %1 found)))
|
||||
(function in-block? c (when (let block-idx (find-idx (substr? (upper-case c)) rem-blocks)) (var! rem-blocks drop block-idx)))
|
||||
(function in-block? c
|
||||
(when (let block-idx (find-idx (substr? (upper-case c)) rem-blocks))
|
||||
(var! rem-blocks drop block-idx)))
|
||||
|
||||
(function can-make-word word
|
||||
(var rem-blocks ["BO" "XK" "DQ" "CP" "NA" "GT" "RE" "TG" "QD" "FS" "JW" "HU" "VI" "AN" "OB" "ER" "FS" "LY" "PC" "ZM"])
|
||||
(.. and (map in-block? word)))
|
||||
|
||||
(join ", " (map #(str % " => " (can-make-word %)) ["A" "bark" "Book" "TREAT" "Common" "squaD" "CoNFuSe"])) ; Notice case insensitivity
|
||||
(-> ["A" "bark" "Book" "TREAT" "Common" "squaD" "CoNFuSe"] ; Notice case insensitivity
|
||||
(map #(str % " => " (can-make-word %)))
|
||||
(join ", "))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,66 @@
|
|||
[ dup 0 peek swap
|
||||
[] 0 rot 0 join
|
||||
witheach
|
||||
[ abs tuck +
|
||||
rot join swap ]
|
||||
drop
|
||||
[] swap witheach
|
||||
[ rot negate
|
||||
dup dip unrot
|
||||
* join ]
|
||||
nip ] is nextcoeffs ( [ --> [ )
|
||||
|
||||
[ ' [ 1 ] swap
|
||||
times nextcoeffs ] is coeffs ( n --> [ )
|
||||
|
||||
[ dup 2 < iff
|
||||
[ drop false ]
|
||||
done
|
||||
true swap
|
||||
dup coeffs
|
||||
behead drop
|
||||
-1 split drop
|
||||
witheach
|
||||
[ over mod
|
||||
0 != if
|
||||
[ dip not
|
||||
conclude ] ]
|
||||
drop ] is prime ( n --> b )
|
||||
|
||||
[ behead
|
||||
0 < iff
|
||||
[ say "-" ]
|
||||
else sp
|
||||
dup size
|
||||
dup 0 = iff
|
||||
[ 1 echo 2drop ]
|
||||
done
|
||||
say "x"
|
||||
dup 1 = iff
|
||||
[ 2drop
|
||||
say " + 1" ]
|
||||
done
|
||||
say "^"
|
||||
echo
|
||||
witheach
|
||||
[ dup 0 < iff
|
||||
[ say " - " ]
|
||||
else
|
||||
[ say " + " ]
|
||||
abs echo
|
||||
i 0 = if done
|
||||
say "x"
|
||||
i 1 = if done
|
||||
say "^"
|
||||
i echo ] ] is echocoeffs ( [ --> )
|
||||
|
||||
8 times
|
||||
[ i^ echo
|
||||
say ": "
|
||||
i^ coeffs
|
||||
echocoeffs
|
||||
cr ]
|
||||
cr
|
||||
50 times
|
||||
[ i^ prime if
|
||||
[ i^ echo sp ] ]
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
BEGIN # model Abelian sandpiles #
|
||||
# returns TRUE if the sandpile s is stable, FALSE otherwise #
|
||||
OP STABLE = ( [,]INT s )BOOL:
|
||||
BEGIN
|
||||
BOOL result := TRUE;
|
||||
FOR i FROM 1 LWB s TO 1 UPB s WHILE result DO
|
||||
FOR j FROM 2 LWB s TO 2 UPB s WHILE result := s[ i, j ] < 4 DO SKIP OD
|
||||
OD;
|
||||
result
|
||||
END # STABLE # ;
|
||||
# returns the sandpile s after avalanches #
|
||||
OP AVALANCHE = ( [,]INT s )[,]INT:
|
||||
BEGIN
|
||||
[ 1 : 1 UPB s, 1 : 2 UPB s ]INT result := s[ AT 1, AT 1 ];
|
||||
WHILE BOOL had avalanche := FALSE;
|
||||
FOR i TO 1 UPB s DO
|
||||
FOR j TO 2 UPB s DO
|
||||
IF result[ i, j ] >= 4 THEN
|
||||
# unstable pile #
|
||||
had avalanche := TRUE;
|
||||
result[ i, j ] -:= 4;
|
||||
IF i > 1 THEN result[ i - 1, j ] +:= 1 FI;
|
||||
IF i < 1 UPB s THEN result[ i + 1, j ] +:= 1 FI;
|
||||
IF j > 1 THEN result[ i, j - 1 ] +:= 1 FI;
|
||||
IF j < 2 UPB s THEN result[ i, j + 1 ] +:= 1 FI
|
||||
FI
|
||||
OD
|
||||
OD;
|
||||
had avalanche
|
||||
DO SKIP OD;
|
||||
result
|
||||
END # AVALANCHE # ;
|
||||
# returns the maximum element of s #
|
||||
OP MAX = ( [,]INT s )INT:
|
||||
BEGIN
|
||||
INT result := s[ 1 LWB s, 2 LWB s ];
|
||||
FOR i FROM 1 LWB s TO 2 UPB s DO
|
||||
FOR j FROM 2 LWB s TO 2 UPB s DO
|
||||
IF s[ i, j ] > result THEN result := s[ i, j ] FI
|
||||
OD
|
||||
OD;
|
||||
result
|
||||
END # MAX # ;
|
||||
# prints the sandpile s #
|
||||
PROC show sandpile = ( STRING title, [,]INT s )VOID:
|
||||
BEGIN
|
||||
print( ( title, newline ) );
|
||||
IF 1 UPB s >= 1 LWB s AND 2 UPB s >= 2 LWB s THEN
|
||||
# non-empty sandpile #
|
||||
INT width := 1; # find tthe width needed for each element #
|
||||
INT v := MAX s;
|
||||
WHILE v > 9 DO
|
||||
v OVERAB 10;
|
||||
width +:= 1
|
||||
OD;
|
||||
FOR i TO 1 UPB s DO
|
||||
FOR j TO 2 UPB s DO
|
||||
print( ( " ", whole( s[ i, j ], - width ) ) )
|
||||
OD;
|
||||
print( ( newline ) )
|
||||
OD
|
||||
FI
|
||||
END # show sandpile # ;
|
||||
# printys a sandpile before and after the avalanches #
|
||||
PROC show sandpile before and after = ( [,]INT s )VOID:
|
||||
BEGIN
|
||||
[ 1 LWB s : 1 UPB s, 2 LWB s : 2 UPB s ]INT t := s;
|
||||
show sandpile( "before: ", t );
|
||||
WHILE NOT STABLE t DO
|
||||
t := AVALANCHE t
|
||||
OD;
|
||||
show sandpile( "after: ", t );
|
||||
print( ( newline ) )
|
||||
END # show sandpile before and after # ;
|
||||
# task test case #
|
||||
[,]INT s1 = ( ( 0, 0, 0, 0, 0 )
|
||||
, ( 0, 0, 0, 0, 0 )
|
||||
, ( 0, 0, 16, 0, 0 )
|
||||
, ( 0, 0, 0, 0, 0 )
|
||||
, ( 0, 0, 0, 0, 0 )
|
||||
);
|
||||
show sandpile before and after( s1 );
|
||||
# test case from 11l, C, etc. #
|
||||
[ 1 : 10, 1 : 10 ]INT s2;
|
||||
FOR i FROM 1 LWB s2 TO 1 UPB s2 DO
|
||||
FOR j FROM 2 LWB s2 TO 2 UPB s2 DO
|
||||
s2[ i, j ] := 0
|
||||
OD
|
||||
OD;
|
||||
s2[ 6, 6 ] := 64;
|
||||
show sandpile before and after( s2 )
|
||||
END
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
func sumprop num .
|
||||
if num < 2
|
||||
return 0
|
||||
.
|
||||
i = 2
|
||||
sum = 1
|
||||
root = sqrt num
|
||||
while i < root
|
||||
if num mod i = 0
|
||||
sum += i + num / i
|
||||
.
|
||||
i += 1
|
||||
.
|
||||
if num mod root = 0
|
||||
sum += root
|
||||
.
|
||||
return sum
|
||||
.
|
||||
for j = 1 to 20000
|
||||
sump = sumprop j
|
||||
if sump < j
|
||||
deficient += 1
|
||||
elif sump = j
|
||||
perfect += 1
|
||||
else
|
||||
abundant += 1
|
||||
.
|
||||
.
|
||||
print "Perfect: " & perfect
|
||||
print "Abundant: " & abundant
|
||||
print "Deficient: " & deficient
|
||||
35
Task/Abundant-odd-numbers/EasyLang/abundant-odd-numbers.easy
Normal file
35
Task/Abundant-odd-numbers/EasyLang/abundant-odd-numbers.easy
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
fastfunc sumdivs n .
|
||||
sum = 1
|
||||
i = 3
|
||||
while i <= sqrt n
|
||||
if n mod i = 0
|
||||
sum += i
|
||||
j = n / i
|
||||
if i <> j
|
||||
sum += j
|
||||
.
|
||||
.
|
||||
i += 2
|
||||
.
|
||||
return sum
|
||||
.
|
||||
n = 1
|
||||
numfmt 0 6
|
||||
while cnt < 1000
|
||||
sum = sumdivs n
|
||||
if sum > n
|
||||
cnt += 1
|
||||
if cnt <= 25 or cnt = 1000
|
||||
print cnt & " n: " & n & " sum: " & sum
|
||||
.
|
||||
.
|
||||
n += 2
|
||||
.
|
||||
print ""
|
||||
n = 1000000001
|
||||
repeat
|
||||
sum = sumdivs n
|
||||
until sum > n
|
||||
n += 2
|
||||
.
|
||||
print "1st > 1B: " & n & " sum: " & sum
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
( ( accumulator
|
||||
=
|
||||
.
|
||||
' ( add sum object addFunction
|
||||
. ( addFunction
|
||||
= A B
|
||||
. !arg:(?A.?B)
|
||||
& ( !A:#
|
||||
& !B:#
|
||||
& "If both values are recognized as integer or fractional values, just use '+'."
|
||||
& !A+!B
|
||||
| "Otherwise, create an object for adding two C doubles and let that run."
|
||||
& ( new
|
||||
$ (UFP,'(.$($A)+$($B)))
|
||||
. go
|
||||
)
|
||||
$
|
||||
)
|
||||
)
|
||||
& ( object
|
||||
= add
|
||||
= addFunction$($arg.!arg)
|
||||
)
|
||||
& !(object.add):?sum
|
||||
& 'addFunction$($($sum).!arg)
|
||||
: (=?(object.add))
|
||||
& !sum
|
||||
)
|
||||
)
|
||||
& accumulator$1:(=?x)
|
||||
& x$5
|
||||
& accumulator$1:(=?y)
|
||||
& y$"5.0"
|
||||
& out$(x$23/10)
|
||||
& out$(y$"2.3")
|
||||
)
|
||||
55
Task/Align-columns/EasyLang/align-columns.easy
Normal file
55
Task/Align-columns/EasyLang/align-columns.easy
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
global width inp$[] .
|
||||
proc read . .
|
||||
repeat
|
||||
inp$ = input
|
||||
until inp$ = ""
|
||||
inp$[] &= inp$
|
||||
ar$[] = strsplit inp$ "$"
|
||||
for s$ in ar$[]
|
||||
width = higher width len s$
|
||||
.
|
||||
.
|
||||
.
|
||||
call read
|
||||
#
|
||||
proc out mode . .
|
||||
for inp$ in inp$[]
|
||||
ar$[] = strsplit inp$ "$"
|
||||
for s$ in ar$[]
|
||||
spc = width - len s$ + 1
|
||||
if mode = 1
|
||||
write s$
|
||||
for i to spc
|
||||
write " "
|
||||
.
|
||||
elif mode = 2
|
||||
for i to spc
|
||||
write " "
|
||||
.
|
||||
write s$
|
||||
elif mode = 3
|
||||
for i to spc div 2
|
||||
write " "
|
||||
.
|
||||
write s$
|
||||
for i to spc - spc div 2
|
||||
write " "
|
||||
.
|
||||
.
|
||||
.
|
||||
print ""
|
||||
.
|
||||
.
|
||||
call out 1
|
||||
print ""
|
||||
call out 2
|
||||
print ""
|
||||
call out 3
|
||||
#
|
||||
input_data
|
||||
Given$a$text$file$of$many$lines,$where$fields$within$a$line$
|
||||
are$delineated$by$a$single$'dollar'$character,$write$a$program
|
||||
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
|
||||
column$are$separated$by$at$least$one$space.
|
||||
Further,$allow$for$each$word$in$a$column$to$be$either$left$
|
||||
justified,$right$justified,$or$center$justified$within$its$column.
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
fastfunc sumprop num .
|
||||
if num = 1
|
||||
return 0
|
||||
.
|
||||
sum = 1
|
||||
root = sqrt num
|
||||
i = 2
|
||||
while i < root
|
||||
if num mod i = 0
|
||||
sum += i + num / i
|
||||
.
|
||||
i += 1
|
||||
.
|
||||
if num mod root = 0
|
||||
sum += root
|
||||
.
|
||||
return sum
|
||||
.
|
||||
func$ tostr ar[] .
|
||||
for v in ar[]
|
||||
s$ &= " " & v
|
||||
.
|
||||
return s$
|
||||
.
|
||||
func$ class k .
|
||||
oldk = k
|
||||
newk = sumprop oldk
|
||||
oldk = newk
|
||||
seq[] &= newk
|
||||
if newk = 0
|
||||
return "terminating " & tostr seq[]
|
||||
.
|
||||
if newk = k
|
||||
return "perfect " & tostr seq[]
|
||||
.
|
||||
newk = sumprop oldk
|
||||
oldk = newk
|
||||
seq[] &= newk
|
||||
if newk = 0
|
||||
return "terminating " & tostr seq[]
|
||||
.
|
||||
if newk = k
|
||||
return "amicable " & tostr seq[]
|
||||
.
|
||||
for t = 4 to 16
|
||||
newk = sumprop oldk
|
||||
seq[] &= newk
|
||||
if newk = 0
|
||||
return "terminating " & tostr seq[]
|
||||
.
|
||||
if newk = k
|
||||
return "sociable (period " & t - 1 & ") " & tostr seq[]
|
||||
.
|
||||
if newk = oldk
|
||||
return "aspiring " & tostr seq[]
|
||||
.
|
||||
for i to len seq[] - 1
|
||||
if newk = seq[i]
|
||||
return "cyclic (at " & newk & ") " & tostr seq[]
|
||||
.
|
||||
.
|
||||
if newk > 140737488355328
|
||||
return "non-terminating (term > 140737488355328) " & tostr seq[]
|
||||
.
|
||||
oldk = newk
|
||||
.
|
||||
return "non-terminating (after 16 terms) " & tostr seq[]
|
||||
.
|
||||
print "Number classification sequence"
|
||||
for j = 1 to 12
|
||||
print j & " " & class j
|
||||
.
|
||||
for j in [ 28 496 220 1184 12496 1264460 790 909 562 1064 1488 15355717786080 ]
|
||||
print j & " " & class j
|
||||
.
|
||||
30
Task/Almost-prime/EasyLang/almost-prime.easy
Normal file
30
Task/Almost-prime/EasyLang/almost-prime.easy
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
func kprime n k .
|
||||
i = 2
|
||||
while i <= n
|
||||
while n mod i = 0
|
||||
if f = k
|
||||
return 0
|
||||
.
|
||||
f += 1
|
||||
n /= i
|
||||
.
|
||||
i += 1
|
||||
.
|
||||
if f = k
|
||||
return 1
|
||||
.
|
||||
return 0
|
||||
.
|
||||
for k = 1 to 5
|
||||
write "k=" & k & " : "
|
||||
i = 2
|
||||
c = 0
|
||||
while c < 10
|
||||
if kprime i k = 1
|
||||
write i & " "
|
||||
c += 1
|
||||
.
|
||||
i += 1
|
||||
.
|
||||
print ""
|
||||
.
|
||||
|
|
@ -27,7 +27,7 @@ public sealed AmicablePairs
|
|||
{
|
||||
int sum := divsums[i];
|
||||
if(i < sum && sum <= divsums.Length && divsums[sum] == i) {
|
||||
yield:new Tuple<int, int>(i, sum);
|
||||
$yield new Tuple<int, int>(i, sum);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
func angdiff a b .
|
||||
r = (b - a) mod 360
|
||||
if r < -180
|
||||
r += 360
|
||||
elif r >= 180
|
||||
r -= 360
|
||||
.
|
||||
return r
|
||||
.
|
||||
proc pd a b . .
|
||||
print b & " " & a & " -> " & angdiff a b
|
||||
.
|
||||
pd 20 45
|
||||
pd -45 45
|
||||
pd -85 90
|
||||
pd -95 90
|
||||
pd -45 125
|
||||
pd -45 145
|
||||
pd 29.4803 -88.6381
|
||||
pd -78.3251 -159.036
|
||||
pd -70099.74233810938 29840.67437876723
|
||||
pd -165313.6666297357 33693.9894517456
|
||||
pd 1174.8380510598456 -154146.66490124757
|
||||
pd 60175.77306795546 42213.07192354373
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
func angconv ang f$ t$ .
|
||||
sgn = sign ang
|
||||
ang = abs ang
|
||||
if f$ = "degree"
|
||||
turn = ang / 360 mod 1
|
||||
elif f$ = "gradian"
|
||||
turn = ang / 400 mod 1
|
||||
elif f$ = "mil"
|
||||
turn = ang / 6400 mod 1
|
||||
elif f$ = "radian"
|
||||
turn = ang / (2 * pi) mod 1
|
||||
.
|
||||
if t$ = "degree"
|
||||
ang = turn * 360
|
||||
elif t$ = "gradian"
|
||||
ang = turn * 400
|
||||
elif t$ = "mil"
|
||||
ang = turn * 6400
|
||||
elif t$ = "radian"
|
||||
ang = turn * 2 * pi
|
||||
.
|
||||
return ang * sgn
|
||||
.
|
||||
func$ fmt s$ .
|
||||
return substr " " 1 (9 - len s$) & s$ & " "
|
||||
.
|
||||
#
|
||||
scales$[] = [ "degree" "gradian" "mil" "radian" ]
|
||||
values[] = [ -2 -1 0 1 2 6.2831853 16 57.2957795 359 399 6399 1000000 ]
|
||||
numfmt 3 10
|
||||
for f$ in scales$[]
|
||||
write fmt f$
|
||||
for t$ in scales$[]
|
||||
write fmt t$
|
||||
.
|
||||
print ""
|
||||
print " ------------------------------------------------"
|
||||
for v in values[]
|
||||
write v
|
||||
for t$ in scales$[]
|
||||
write angconv v f$ t$
|
||||
.
|
||||
print ""
|
||||
.
|
||||
print ""
|
||||
.
|
||||
27
Task/Anti-primes/EasyLang/anti-primes.easy
Normal file
27
Task/Anti-primes/EasyLang/anti-primes.easy
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
func divcnt v .
|
||||
n = v
|
||||
tot = 1
|
||||
p = 2
|
||||
while p <= sqrt n
|
||||
cnt = 1
|
||||
while n mod p = 0
|
||||
cnt += 1
|
||||
n = n div p
|
||||
.
|
||||
p += 1
|
||||
tot *= cnt
|
||||
.
|
||||
if n > 1
|
||||
tot *= 2
|
||||
.
|
||||
return tot
|
||||
.
|
||||
while count < 20
|
||||
n += 1
|
||||
divs = divcnt n
|
||||
if divs > max
|
||||
print n
|
||||
max = divs
|
||||
count += 1
|
||||
.
|
||||
.
|
||||
31
Task/Anti-primes/Grain/anti-primes.grain
Normal file
31
Task/Anti-primes/Grain/anti-primes.grain
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import File from "sys/file"
|
||||
let mut maxDiv = 0
|
||||
let mut count = 0
|
||||
let numaprimes = 20
|
||||
let countDivisors = n => {
|
||||
if (n < 1) {
|
||||
1
|
||||
} else {
|
||||
let mut count = 2
|
||||
let mut target = n / 2
|
||||
for (let mut i = 1; i <= target; i += 1) {
|
||||
if (n % i == 0) {
|
||||
count += 1
|
||||
} else {
|
||||
void
|
||||
}
|
||||
}
|
||||
count
|
||||
}
|
||||
}
|
||||
print("\nThe first 20 anti-primes are: ")
|
||||
let mut d = 0
|
||||
for (let mut j = 1; count < numaprimes; j += 1) {
|
||||
d = countDivisors(j)
|
||||
if (d > maxDiv) {
|
||||
File.fdWrite(File.stdout, Pervasives.toString(j))
|
||||
File.fdWrite(File.stdout, " ")
|
||||
maxDiv = d
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,8 @@
|
|||
linewidth 0.2
|
||||
linewidth 0.4
|
||||
x = 50
|
||||
y = 50
|
||||
move x y
|
||||
while r < 50
|
||||
line r * cos t + x r * sin t + y
|
||||
r += 0.1
|
||||
t += 6
|
||||
r += 0.05
|
||||
t += 3
|
||||
.
|
||||
|
|
|
|||
21
Task/Arithmetic-derivative/ALGOL-W/arithmetic-derivative.alg
Normal file
21
Task/Arithmetic-derivative/ALGOL-W/arithmetic-derivative.alg
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
begin
|
||||
integer procedure lagarias ( integer value n ) ; % Lagarias arithmetic derivative %
|
||||
if n < 0
|
||||
then -lagarias (-n)
|
||||
else if n = 0 or n = 1
|
||||
then 0
|
||||
else begin
|
||||
integer f, q;
|
||||
integer procedure smallPf ( integer value j, k ) ; % Smallest prime factor %
|
||||
if j rem k = 0 then k else smallPf (j, k + 1);
|
||||
f := smallPf (n, 2); q := n div f;
|
||||
if q = 1
|
||||
then 1
|
||||
else q * lagarias (f) + f * lagarias (q)
|
||||
end lagarias ;
|
||||
|
||||
for n := -99 until 100 do begin
|
||||
writeon( i_w := 6, s_w := 0, " ", lagarias (n) );
|
||||
if n rem 10 = 0 then write()
|
||||
end for_n
|
||||
end.
|
||||
25
Task/Arithmetic-derivative/Lua/arithmetic-derivative.lua
Normal file
25
Task/Arithmetic-derivative/Lua/arithmetic-derivative.lua
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
do local function lagarias (n) -- Lagarias arithmetic derivative
|
||||
if n < 0
|
||||
then return -lagarias (-n)
|
||||
elseif n == 0 or n == 1
|
||||
then return 0
|
||||
else local function smallPf (j, k) -- Smallest prime factor
|
||||
if j % k == 0 then return k else return smallPf (j, k + 1) end
|
||||
end
|
||||
local f = smallPf (n, 2) local q = math.floor (n / f)
|
||||
if q == 1
|
||||
then return 1
|
||||
else return q * lagarias (f) + f * lagarias (q)
|
||||
end
|
||||
end
|
||||
end
|
||||
for n = -99,100
|
||||
do io.write (string.format("%6d", lagarias (n)))
|
||||
if n % 10 == 0 then io.write ("\n") end
|
||||
end
|
||||
io.write ("\n")
|
||||
for n = 1,17 -- 18, 19 and 20 would overflow
|
||||
do local m = 10 ^ n
|
||||
io.write ("D(", string.format ("%d", m), ") / 7 = ", math.floor (lagarias (m) / 7), "\n")
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
lagarias = function (n) // Lagarias arithmetic derivative
|
||||
if n < 0 then
|
||||
return -lagarias (-n)
|
||||
else if n == 0 or n == 1 then
|
||||
return 0
|
||||
else
|
||||
smallPf = function (j, k) // Smallest prime factor
|
||||
if j % k == 0 then
|
||||
return k
|
||||
else
|
||||
return smallPf (j, k + 1)
|
||||
end if
|
||||
end function
|
||||
f = smallPf (n, 2)
|
||||
q = floor (n / f)
|
||||
if q == 1 then
|
||||
return 1
|
||||
else
|
||||
return q * lagarias (f) + f * lagarias (q)
|
||||
end if
|
||||
end if
|
||||
end function
|
||||
fmt6 = function (n) // return a 6 character string representation of n
|
||||
s = str( n )
|
||||
if s.len > 5 then
|
||||
return s
|
||||
else
|
||||
return ( " " * ( 6 - s.len ) ) + s
|
||||
end if
|
||||
end function
|
||||
ad = ""
|
||||
for n in range( -99, 100 )
|
||||
ad = ad + " " + fmt6( lagarias (n) )
|
||||
if n % 10 == 0 then
|
||||
print( ad )
|
||||
ad = ""
|
||||
end if
|
||||
end for
|
||||
print()
|
||||
for n in range( 1, 17 ) // 18, 19 and 20 would overflow ????? TODO: check
|
||||
m = 10 ^ n
|
||||
print( "D(" + str(m) + ") / 7 = " + str( floor (lagarias (m) / 7) ) )
|
||||
end for
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
an = 1
|
||||
bn = sqrt 0.5
|
||||
tn = 0.25
|
||||
pn = 1
|
||||
while pn <= 5
|
||||
prevAn = an
|
||||
an = (bn + an) / 2
|
||||
bn = sqrt (bn * prevAn)
|
||||
prevAn -= an
|
||||
tn -= (pn * prevAn * prevAn)
|
||||
pn *= 2
|
||||
.
|
||||
mypi = (an + bn) * (an + bn) / (tn * 4)
|
||||
numfmt 15 0
|
||||
print mypi
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
func agm a g .
|
||||
repeat
|
||||
a0 = a
|
||||
a = (a0 + g) / 2
|
||||
g = sqrt (a0 * g)
|
||||
until abs (a0 - a) < abs (a) * 1e-15
|
||||
.
|
||||
return a
|
||||
.
|
||||
numfmt 16 0
|
||||
print agm 1 sqrt 0.5
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
sub agm {
|
||||
($^z, {(.re+.im)/2 + (.re*.im).sqrt*1i} ... * ≅ *)
|
||||
.tail.re
|
||||
}
|
||||
|
||||
say agm 1 + 1i/2.sqrt
|
||||
48
Task/Arithmetic-numbers/Draco/arithmetic-numbers.draco
Normal file
48
Task/Arithmetic-numbers/Draco/arithmetic-numbers.draco
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
word MAX = 13000;
|
||||
|
||||
[MAX+1]word divisorSum;
|
||||
[MAX+1]byte divisorCount;
|
||||
|
||||
proc calculateDivisorSums() void:
|
||||
word num, div;
|
||||
for div from 1 by 1 upto MAX do
|
||||
for num from div by div upto MAX do
|
||||
divisorSum[num] := divisorSum[num] + div;
|
||||
divisorCount[num] := divisorCount[num] + 1
|
||||
od
|
||||
od
|
||||
corp
|
||||
|
||||
proc arithmetic(word n) bool:
|
||||
divisorSum[n] % divisorCount[n] = 0
|
||||
corp
|
||||
|
||||
proc composite(word n) bool:
|
||||
n > 1 and divisorSum[n] /= n+1
|
||||
corp
|
||||
|
||||
proc main() void:
|
||||
word num, nthArithm, composites;
|
||||
calculateDivisorSums();
|
||||
|
||||
writeln("First 100 arithmetic numbers:");
|
||||
|
||||
num := 0;
|
||||
composites := 0;
|
||||
for nthArithm from 1 upto 10000 do
|
||||
while num := num+1; not arithmetic(num) do od;
|
||||
if composite(num) then composites := composites + 1 fi;
|
||||
|
||||
if nthArithm <= 100 then
|
||||
write(num:5);
|
||||
if nthArithm % 10 = 0 then writeln() fi
|
||||
fi;
|
||||
|
||||
if nthArithm = 1000 or nthArithm = 10000 then
|
||||
writeln();
|
||||
writeln("The ",nthArithm,"th arithmetic number is ",num,".");
|
||||
writeln("Of the first ",nthArithm," arithmetic numbers, ",
|
||||
composites," are composite.")
|
||||
fi
|
||||
od
|
||||
corp
|
||||
25
Task/Ascending-primes/EasyLang/ascending-primes.easy
Normal file
25
Task/Ascending-primes/EasyLang/ascending-primes.easy
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
func isprim num .
|
||||
if num < 2
|
||||
return 0
|
||||
.
|
||||
i = 2
|
||||
while i <= sqrt num
|
||||
if num mod i = 0
|
||||
return 0
|
||||
.
|
||||
i += 1
|
||||
.
|
||||
return 1
|
||||
.
|
||||
proc nextasc n . .
|
||||
if isprim n = 1
|
||||
write n & " "
|
||||
.
|
||||
if n > 123456789
|
||||
return
|
||||
.
|
||||
for d = n mod 10 + 1 to 9
|
||||
nextasc n * 10 + d
|
||||
.
|
||||
.
|
||||
nextasc 0
|
||||
32
Task/Attractive-numbers/EasyLang/attractive-numbers.easy
Normal file
32
Task/Attractive-numbers/EasyLang/attractive-numbers.easy
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
func isprim num .
|
||||
if num < 2
|
||||
return 0
|
||||
.
|
||||
i = 2
|
||||
while i <= sqrt num
|
||||
if num mod i = 0
|
||||
return 0
|
||||
.
|
||||
i += 1
|
||||
.
|
||||
return 1
|
||||
.
|
||||
func count n .
|
||||
f = 2
|
||||
repeat
|
||||
if n mod f = 0
|
||||
cnt += 1
|
||||
n /= f
|
||||
else
|
||||
f += 1
|
||||
.
|
||||
until n = 1
|
||||
.
|
||||
return cnt
|
||||
.
|
||||
for i = 2 to 120
|
||||
n = count i
|
||||
if isprim n = 1
|
||||
write i & " "
|
||||
.
|
||||
.
|
||||
10
Task/Averages-Mean-angle/EasyLang/averages-mean-angle.easy
Normal file
10
Task/Averages-Mean-angle/EasyLang/averages-mean-angle.easy
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
func mean ang[] .
|
||||
for ang in ang[]
|
||||
x += cos ang
|
||||
y += sin ang
|
||||
.
|
||||
return atan2 (y / len ang[]) (x / len ang[])
|
||||
.
|
||||
print mean [ 350 10 ]
|
||||
print mean [ 90 180 270 360 ]
|
||||
print mean [ 10 20 30 ]
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
func tm2deg t$ .
|
||||
t[] = number strsplit t$ ":"
|
||||
return 360 * t[1] / 24.0 + 360 * t[2] / (24 * 60.0) + 360 * t[3] / (24 * 3600.0)
|
||||
.
|
||||
func$ deg2tm deg .
|
||||
len t[] 3
|
||||
h = floor (24 * 60 * 60 * deg / 360)
|
||||
t[3] = h mod 60
|
||||
h = h div 60
|
||||
t[2] = h mod 60
|
||||
t[1] = h div 60
|
||||
for h in t[]
|
||||
if h < 10
|
||||
s$ &= 0
|
||||
.
|
||||
s$ &= h
|
||||
s$ &= ":"
|
||||
.
|
||||
return substr s$ 1 8
|
||||
.
|
||||
func mean ang[] .
|
||||
for ang in ang[]
|
||||
x += cos ang
|
||||
y += sin ang
|
||||
.
|
||||
return atan2 (y / len ang[]) (x / len ang[])
|
||||
.
|
||||
in$ = "23:00:17 23:40:20 00:12:45 00:17:19"
|
||||
for s$ in strsplit in$ " "
|
||||
ar[] &= tm2deg s$
|
||||
.
|
||||
print deg2tm (360 + mean ar[])
|
||||
|
|
@ -1,25 +1,47 @@
|
|||
(median=
|
||||
begin decimals end int list med med1 med2 num number
|
||||
. 0:?list
|
||||
& whl
|
||||
' ( @( !arg
|
||||
: ?
|
||||
((%@:~" ":~",") ?:?number)
|
||||
((" "|",") ?arg|:?arg)
|
||||
)
|
||||
& @( !number
|
||||
: ( #?int "." [?begin #?decimals [?end
|
||||
& !int+!decimals*10^(!begin+-1*!end):?num
|
||||
| ?num
|
||||
)
|
||||
)
|
||||
& (!num.)+!list:?list
|
||||
( ( median
|
||||
= begin decimals end int list
|
||||
, med med1 med2 num number
|
||||
. ( convertToRational
|
||||
=
|
||||
. new$(UFP,'(.$arg:?V))
|
||||
: ?ufp
|
||||
& (ufp..go)$
|
||||
& (ufp..export)$(Q.V)
|
||||
)
|
||||
& 0:?list
|
||||
& whl
|
||||
' ( !arg:%?number ?arg
|
||||
& convertToRational$!number:?rationalnumber
|
||||
& (!rationalnumber.)+!list:?list
|
||||
)
|
||||
& !list:?+[?end
|
||||
& ( !end*1/2:~/
|
||||
& !list
|
||||
: ?
|
||||
+ [!(=1/2*!end+-1)
|
||||
+ (?med1.?)
|
||||
+ (?med2.?)
|
||||
+ ?
|
||||
& !med1*1/2+!med2*1/2:?med
|
||||
| !list:?+[(div$(1/2*!end,1))+(?med.)+?
|
||||
)
|
||||
& (new$(UFP,'(.$med)).go)$
|
||||
)
|
||||
& out$(median$("4.1" 4 "1.2" "6.235" "7868.33"))
|
||||
& out
|
||||
$ ( median
|
||||
$ ( "4.4"
|
||||
"2.3"
|
||||
"-1.7"
|
||||
"7.5"
|
||||
"6.6"
|
||||
"0.0"
|
||||
"1.9"
|
||||
"8.2"
|
||||
"9.3"
|
||||
"4.5"
|
||||
)
|
||||
& !list:?+[?end
|
||||
& ( !end*1/2:~/
|
||||
& !list:?+[!(=1/2*!end+-1)+(?med1.)+(?med2.)+?
|
||||
& !med1*1/2+!med2*1/2:?med
|
||||
| !list:?+[(div$(1/2*!end,1))+(?med.)+?
|
||||
)
|
||||
& !med
|
||||
& out$(median$(1 5 3 2 4))
|
||||
& out$(median$(1 5 3 6 4 2))
|
||||
);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
func rms v[] .
|
||||
for v in v[]
|
||||
sum += v * v
|
||||
.
|
||||
return sqrt (sum / len v[])
|
||||
.
|
||||
v[] = [ 1 2 3 4 5 6 7 8 9 10 ]
|
||||
print rms v[]
|
||||
14
Task/Bell-numbers/EasyLang/bell-numbers.easy
Normal file
14
Task/Bell-numbers/EasyLang/bell-numbers.easy
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
func bell n .
|
||||
len list[] n
|
||||
list[1] = 1
|
||||
for i = 2 to n
|
||||
for j = 1 to i - 2
|
||||
list[i - j - 1] += list[i - j]
|
||||
.
|
||||
list[i] = list[1] + list[i - 1]
|
||||
.
|
||||
return list[n]
|
||||
.
|
||||
for i = 1 to 15
|
||||
print bell i
|
||||
.
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
len d[] 26
|
||||
pos = 1
|
||||
numfmt 0 4
|
||||
repeat
|
||||
s$ = input
|
||||
until s$ = ""
|
||||
for c$ in strchars s$
|
||||
if pos mod 40 = 1
|
||||
write pos & ":"
|
||||
.
|
||||
if pos mod 4 = 1
|
||||
write " "
|
||||
.
|
||||
write c$
|
||||
if pos mod 40 = 0
|
||||
print ""
|
||||
.
|
||||
pos += 1
|
||||
c = strcode c$
|
||||
d[c - 64] += 1
|
||||
.
|
||||
.
|
||||
print ""
|
||||
for i in [ 1 3 7 20 ]
|
||||
write strchar (64 + i) & ": "
|
||||
print d[i]
|
||||
.
|
||||
print "Total: " & d[1] + d[3] + d[7] + d[20]
|
||||
input_data
|
||||
CGTAAAAAATTACAACGTCCTTTGGCTATCTCTTAAACTCCTGCTAAATG
|
||||
CTCGTGCTTTCCAATTATGTAAGCGTTCCGAGACGGGGTGGTCGATTCTG
|
||||
AGGACAAAGGTCAAGATGGAGCGCATCGAACGCAATAAGGATCATTTGAT
|
||||
GGGACGTTTCGTCGACAAAGTCTTGTTTCGAGAGTAACGGCTACCGTCTT
|
||||
CGATTCTGCTTATAACACTATGTTCTTATGAAATGGATGTTCTGAGTTGG
|
||||
TCAGTCCCAATGTGCGGGGTTTCTTTTAGTACGTCGGGAGTGGTATTATA
|
||||
TTTAATTTTTCTATATAGCGATCTGTATTTAAGCAATTCATTTAGGTTAT
|
||||
CGCCGCGATGCTCGGTTCGGACCGCCAAGCATCTGGCTCCACTGCTAGTG
|
||||
TCCTAAATTTGAATGGCAAACACAAATAAGATTTAGCAATTCGTGTAGAC
|
||||
GACCGGGGACTTGCATGATGGGAGCAGCTTTGTTAAACTACGAACGTAAT
|
||||
38
Task/Blum-integer/EasyLang/blum-integer.easy
Normal file
38
Task/Blum-integer/EasyLang/blum-integer.easy
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
fastfunc semiprim n .
|
||||
d = 3
|
||||
while d * d <= n
|
||||
while n mod d = 0
|
||||
if c = 2
|
||||
return 0
|
||||
.
|
||||
n /= d
|
||||
c += 1
|
||||
.
|
||||
d += 2
|
||||
.
|
||||
if c = 1
|
||||
return n
|
||||
.
|
||||
.
|
||||
print "The first 50 Blum integers:"
|
||||
n = 3
|
||||
numfmt 0 4
|
||||
repeat
|
||||
prim1 = semiprim n
|
||||
if prim1 <> 0
|
||||
if prim1 mod 4 = 3
|
||||
prim2 = n div prim1
|
||||
if prim2 <> prim1 and prim2 mod 4 = 3
|
||||
c += 1
|
||||
if c <= 50
|
||||
write n
|
||||
if c mod 10 = 0 ; print "" ; .
|
||||
.
|
||||
.
|
||||
.
|
||||
.
|
||||
until c >= 26828
|
||||
n += 2
|
||||
.
|
||||
print ""
|
||||
print "The 26828th Blum integer is: " & n
|
||||
48
Task/Blum-integer/Quackery/blum-integer.quackery
Normal file
48
Task/Blum-integer/Quackery/blum-integer.quackery
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
600000 eratosthenes
|
||||
|
||||
[ dup sqrt
|
||||
tuck dup * = ] is exactsqrt ( n --> n b )
|
||||
|
||||
[ dup isprime iff
|
||||
[ drop false ] done
|
||||
dup 4 mod 1 != iff
|
||||
[ drop false ] done
|
||||
dup exactsqrt iff
|
||||
[ 2drop false ] done
|
||||
temp put
|
||||
3 from
|
||||
[ 4 incr
|
||||
index temp share > iff
|
||||
[ drop false end ]
|
||||
done
|
||||
index isprime not if done
|
||||
dup index /mod 0 != iff
|
||||
drop done
|
||||
isprime not if done
|
||||
drop index end ]
|
||||
temp release ] is blum ( n --> n )
|
||||
|
||||
[ dup blum
|
||||
over echo
|
||||
say " = "
|
||||
dup echo
|
||||
say " * "
|
||||
/ echo cr ] is echoblum ( n --> )
|
||||
|
||||
say "The First 50 Blum integers:"
|
||||
cr cr
|
||||
[] 1 from
|
||||
[ 4 incr
|
||||
index blum if
|
||||
[ index join ]
|
||||
dup size 50 = if end ]
|
||||
witheach echoblum
|
||||
cr
|
||||
say "The 26828th Blum integer:"
|
||||
cr cr
|
||||
0 1 from
|
||||
[ 4 incr
|
||||
index blum if 1+
|
||||
dup 26828 = if
|
||||
[ drop index end ] ]
|
||||
echoblum
|
||||
49
Task/Brazilian-numbers/Quackery/brazilian-numbers.quackery
Normal file
49
Task/Brazilian-numbers/Quackery/brazilian-numbers.quackery
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
[ dup base put
|
||||
/mod temp put
|
||||
true swap
|
||||
[ dup 0 > while
|
||||
base share /mod
|
||||
temp share != iff
|
||||
[ dip not ]
|
||||
done
|
||||
again ]
|
||||
drop
|
||||
temp release
|
||||
base release ] is allsame ( n n --> b )
|
||||
|
||||
[ false swap
|
||||
dup 3 - times
|
||||
[ dup i 2 +
|
||||
allsame iff
|
||||
[ dip not
|
||||
conclude ]
|
||||
done ]
|
||||
drop ] is brazilian ( n --> b )
|
||||
|
||||
say "First 20 Brazilian numbers:" cr
|
||||
[] 0
|
||||
[ dup brazilian if
|
||||
[ dup dip join ]
|
||||
1+
|
||||
over size 20 = until ]
|
||||
drop echo
|
||||
cr
|
||||
cr
|
||||
say "First 20 odd Brazilian numbers:" cr
|
||||
[] 1
|
||||
[ dup brazilian if
|
||||
[ dup dip join ]
|
||||
2 +
|
||||
over size 20 = until ]
|
||||
drop echo
|
||||
cr
|
||||
cr
|
||||
say "First 20 prime Brazilian numbers:" cr
|
||||
[] 1
|
||||
[ dup isprime not iff
|
||||
[ 2 + ] again
|
||||
dup brazilian if
|
||||
[ dup dip join ]
|
||||
2 +
|
||||
over size 20 = until ]
|
||||
drop echo
|
||||
107
Task/Bulls-and-cows/FutureBasic/bulls-and-cows.basic
Normal file
107
Task/Bulls-and-cows/FutureBasic/bulls-and-cows.basic
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
include "NSLog.incl"
|
||||
str15 guess, goal
|
||||
short x, y
|
||||
cgRect wndrect
|
||||
|
||||
begin enum 1
|
||||
_window
|
||||
_bullLabel
|
||||
_cowLabel
|
||||
_horzLine
|
||||
_vertLine
|
||||
_newGameBtn
|
||||
_alert = 101
|
||||
end enum
|
||||
|
||||
void local fn showStr( string as str15 )
|
||||
short r
|
||||
x = 20
|
||||
for r = 1 to string[0]
|
||||
print %(x,y)chr$( string[r] );
|
||||
x += 39
|
||||
next
|
||||
end fn
|
||||
|
||||
void local fn NewGame
|
||||
str15 ch
|
||||
goal = "" : guess = "" :y = 20
|
||||
window _window,,wndRect
|
||||
text ,,fn colorRed
|
||||
cls
|
||||
fn showStr( "????" )
|
||||
|
||||
do
|
||||
ch = chr$(rnd(9) or _"0")
|
||||
if instr$(0, goal, ch) == 0 then goal += ch
|
||||
until goal[0] == 4
|
||||
|
||||
nslog(@"%u",val&(goal)) //unrem for testing
|
||||
y += 48
|
||||
end fn
|
||||
|
||||
local fn SetWindowFrame
|
||||
CGRect r = fn WindowContentRect( _window )
|
||||
r.size.height += 32
|
||||
r.origin.y -= 32
|
||||
window _window,,r
|
||||
if ( r.origin.y < 150 )
|
||||
alert _alert,, @"Too many guesses!",, @"Give up", YES
|
||||
fn newGame
|
||||
end if
|
||||
end fn
|
||||
|
||||
local fn play( ch as str15 )
|
||||
short r, bulls = 0, cows = 0
|
||||
if instr$(0, guess, ch) then exit fn
|
||||
guess += ch
|
||||
text,,fn colorDarkGray
|
||||
fn showStr( guess )
|
||||
if guess[0] < 4 then exit fn
|
||||
|
||||
for r = 1 to 4
|
||||
if goal[r] == guess[r] then bulls++ : continue
|
||||
if instr$(0, goal, chr$(guess[r]) ) then cows++
|
||||
next
|
||||
|
||||
select
|
||||
case bulls == 4
|
||||
text ,,fn colorRed
|
||||
print %(x + 31, y)("W I N!")
|
||||
y = 20 : fn showStr( goal )
|
||||
case else : print %(x + 35, y)bulls;" "; cows
|
||||
y += 32 : guess = ""
|
||||
end select
|
||||
fn SetWindowFrame
|
||||
end fn
|
||||
|
||||
|
||||
void local fn BuildWindow
|
||||
subclass window _window, @"Bulls and cows", (0,0,311,114), NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
|
||||
wndrect = = fn WindowContentRect( _window )
|
||||
textlabel _bullLabel, @"🐂", (198,59,38,40)
|
||||
textlabel _cowLabel, @"🐄", (255,59,38,40)
|
||||
ControlSetFontWithName( _bullLabel, NULL, 30 )
|
||||
ControlSetFontWithName( _cowLabel, NULL, 30 )
|
||||
box _horzLine,, (12,50,287,5), NSBoxSeparator
|
||||
box _vertLine,, (180,12,5,90), NSBoxSeparator
|
||||
ViewSetAutoresizingMask( _vertLine, NSViewHeightSizable )
|
||||
button _newGameBtn,,, @"New Game", (198,13,100,32)
|
||||
ViewSetAutoresizingMask( _newGameBtn, NSViewMaxYMargin )
|
||||
text @"menlo bold",24,,fn ColorWindowBackground
|
||||
end fn
|
||||
|
||||
void local fn DoDialog( evt as long, tag as long )
|
||||
select ( evt )
|
||||
case _windowKeyDown //: stop
|
||||
short ch = intval( fn EventCharacters )
|
||||
if ch then fn play( chr$( ch or _"0" ) ):DialogEventSetBool(YES)
|
||||
case _btnClick : fn NewGame
|
||||
case _windowWillClose : end
|
||||
end select
|
||||
end fn
|
||||
|
||||
on dialog fn DoDialog
|
||||
fn buildWindow
|
||||
fn newGame
|
||||
|
||||
HandleEvents
|
||||
|
|
@ -1,19 +1,19 @@
|
|||
: ceasar ( c n -- c )
|
||||
: caesar ( c n -- c )
|
||||
over 32 or [char] a -
|
||||
dup 0 26 within if
|
||||
over + 25 > if 26 - then +
|
||||
else 2drop then ;
|
||||
|
||||
: ceasar-string ( n str len -- )
|
||||
over + swap do i c@ over ceasar i c! loop drop ;
|
||||
: caesar-string ( n str len -- )
|
||||
over + swap do i c@ over caesar i c! loop drop ;
|
||||
|
||||
: ceasar-inverse ( n -- 'n ) 26 swap - 26 mod ;
|
||||
: caesar-inverse ( n -- 'n ) 26 swap - 26 mod ;
|
||||
|
||||
2variable test
|
||||
s" The five boxing wizards jump quickly!" test 2!
|
||||
|
||||
3 test 2@ ceasar-string
|
||||
3 test 2@ caesar-string
|
||||
test 2@ cr type
|
||||
|
||||
3 ceasar-inverse test 2@ ceasar-string
|
||||
3 caesar-inverse test 2@ caesar-string
|
||||
test 2@ cr type
|
||||
|
|
|
|||
12
Task/Caesar-cipher/Icon/caesar-cipher-2.icon
Normal file
12
Task/Caesar-cipher/Icon/caesar-cipher-2.icon
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
(function caeser by of
|
||||
(let alphabets (proj (map char-code) (range 65 91) (range 97 123))
|
||||
shifted (.. vec (map (rotate by) alphabets))
|
||||
table (kv-dict (flatten alphabets) (flatten shifted)))
|
||||
(... str (map #(or (table %) %) of)))
|
||||
|
||||
(let original "The Quick Brown Fox Jumps Over The Lazy Dog."
|
||||
encrypted (caeser -1 original)
|
||||
decrypted (caeser 1 encrypted))
|
||||
(str "Original: " original "
|
||||
Encrypted: " encrypted "
|
||||
Decrypted: " decrypted)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(var funcs (for x (range 11) #(* x x)))
|
||||
|
||||
[(0 funcs) ((3 funcs)) ((4 funcs))]
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
( ( C
|
||||
( C
|
||||
= n k coef
|
||||
. !arg:(?n,?k)
|
||||
& (!n+-1*!k:<!k:?k|)
|
||||
|
|
@ -11,6 +11,35 @@
|
|||
)
|
||||
& !coef
|
||||
)
|
||||
& ( compileBinomialFunctionThatDoesFloatingPointCalculations
|
||||
=
|
||||
. new
|
||||
$ ( UFP
|
||||
,
|
||||
' ( (s.n) (s.k)
|
||||
. "**************************************************************
|
||||
*** Notice the difference between the following four lines ***
|
||||
*** of code and the much shorter (!n+-1*!k:<!k:?k|) in ***
|
||||
*** function C above. UFP grammar is simpler than usual ***
|
||||
*** Bracmat grammar. UFP code is therefore less terse. ***
|
||||
**************************************************************"
|
||||
& !n+-1*!k:?n-k
|
||||
& ( !n-k:<!k&!n-k:?k
|
||||
|
|
||||
)
|
||||
& 1:?coef
|
||||
& whl
|
||||
' ( !k:>0
|
||||
& !coef*!n*!k^-1:?coef
|
||||
& !k+-1:?k
|
||||
& !n+-1:?n
|
||||
)
|
||||
& !coef
|
||||
)
|
||||
)
|
||||
)
|
||||
& compileBinomialFunctionThatDoesFloatingPointCalculations$
|
||||
: ?binom
|
||||
& ( P
|
||||
= n k result
|
||||
. !arg:(?n,?k)
|
||||
|
|
@ -23,6 +52,25 @@
|
|||
)
|
||||
& !result
|
||||
)
|
||||
& ( compilePermutationFunctionThatDoesFloatingPointCalculations
|
||||
=
|
||||
. new
|
||||
$ ( UFP
|
||||
,
|
||||
' ( (s.n) (s.k)
|
||||
. !n+-1*!k:?k
|
||||
& 1:?result
|
||||
& whl
|
||||
' ( !n:>!k
|
||||
& !n*!result:?result
|
||||
& !n+-1:?n
|
||||
)
|
||||
& !result
|
||||
)
|
||||
)
|
||||
)
|
||||
& compilePermutationFunctionThatDoesFloatingPointCalculations$
|
||||
: ?permu
|
||||
& 0:?i
|
||||
& whl
|
||||
' ( 1+!i:~>12:?i
|
||||
|
|
@ -33,7 +81,8 @@
|
|||
& whl
|
||||
' ( 10+!i:~>60:?i
|
||||
& div$(!i.3):?k
|
||||
& out$(!i C !k "=" C$(!i,!k))
|
||||
& out$(!i Cn !k "= " C$(!i,!k))
|
||||
& out$(!i Cf !k "=" (binom..go)$(!i,!k))
|
||||
)
|
||||
& ( displayBig
|
||||
=
|
||||
|
|
@ -45,12 +94,26 @@
|
|||
& whl
|
||||
' ( !is:%?i ?is
|
||||
& div$(!i.3):?k
|
||||
& out$(str$(!i " P " !k " = " displayBig$(P$(!i,!k))))
|
||||
& out
|
||||
$ ( str
|
||||
$ (!i " Pn " !k " = " displayBig$(P$(!i,!k)))
|
||||
)
|
||||
& out
|
||||
$ ( str
|
||||
$ (!i " Pf " !k " = " (permu..go)$(!i,!k))
|
||||
)
|
||||
)
|
||||
& 0:?i
|
||||
& whl
|
||||
' ( 100+!i:~>1000:?i
|
||||
& div$(!i.3):?k
|
||||
& out$(str$(!i " C " !k " = " displayBig$(C$(!i,!k))))
|
||||
& out
|
||||
$ ( str
|
||||
$ (!i " Cn " !k " = " displayBig$(C$(!i,!k)))
|
||||
)
|
||||
& out
|
||||
$ ( str
|
||||
$ (!i " Cf " !k " = " (binom..go)$(!i,!k))
|
||||
)
|
||||
)
|
||||
);
|
||||
& all done;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
const std = @import("std");
|
||||
const num = f64;
|
||||
|
||||
pub fn perm(n: num, k: num) num {
|
||||
var result: num = 1;
|
||||
var i: num = 0;
|
||||
while (i < k) : (i += 1) {
|
||||
result *= n - i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn comb(n: num, k: num) num {
|
||||
return perm(n, k) / perm(k, k);
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var stdout = std.io.getStdOut().writer();
|
||||
|
||||
const p: num = 12;
|
||||
const c: num = 60;
|
||||
var j: num = 1;
|
||||
var k: num = 10;
|
||||
|
||||
while (j < p) : (j += 1) {
|
||||
try stdout.print("P({d},{d}) = {d}\n", .{ p, j, @floor(perm(p, j)) });
|
||||
}
|
||||
|
||||
while (k < c) : (k += 10) {
|
||||
try stdout.print("C({d},{d}) = {d}\n", .{ c, k, @floor(comb(c, k)) });
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
(defun samples (k items)
|
||||
(cond
|
||||
((zerop k) '(()))
|
||||
((null items) '())
|
||||
(t (append
|
||||
(mapc '(lambda (c) (cons (car items) c))
|
||||
(samples (sub1 k) items))
|
||||
(samples k (cdr items))))))
|
||||
|
||||
(defun append (a b)
|
||||
(cond ((null a) b)
|
||||
(t (cons (car a) (append (cdr a) b)))))
|
||||
|
||||
(defun length (list (len . 0))
|
||||
(map '(lambda (e) (setq len (add1 len)))
|
||||
list)
|
||||
len)
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
-->
|
||||
<span style="color: #000080;font-style:italic;">-- This is a comment
|
||||
(phixonline)-->
|
||||
<span style="color: #000080;font-style:italic;">-- this is a comment.
|
||||
// this is also a comment. </span>
|
||||
<!--
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
proc test s$[] . .
|
||||
ident = 1
|
||||
ascend = 1
|
||||
for i = 2 to len s$[]
|
||||
h = strcmp s$[i] s$[i - 1]
|
||||
if h <> 0
|
||||
ident = 0
|
||||
.
|
||||
if h <= 0
|
||||
ascend = 0
|
||||
.
|
||||
.
|
||||
print s$[]
|
||||
if ident = 1
|
||||
print "all equal"
|
||||
.
|
||||
if ascend = 1
|
||||
print "ascending"
|
||||
.
|
||||
print ""
|
||||
.
|
||||
test [ "AA" "BB" "CC" ]
|
||||
test [ "AA" "AA" "AA" ]
|
||||
test [ "AA" "CC" "BB" ]
|
||||
test [ "AA" "ACB" "BB" "CC" ]
|
||||
test [ "single_element" ]
|
||||
|
|
@ -1,48 +1,75 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
"use strict";
|
||||
|
||||
const main = () =>
|
||||
showJSON(
|
||||
map( // Using a tolerance epsilon of 1/10000
|
||||
n => showRatio(approxRatio(0.0001)(n)),
|
||||
[0.9054054, 0.518518, 0.75]
|
||||
)
|
||||
);
|
||||
|
||||
// Epsilon -> Real -> Ratio
|
||||
// ---------------- APPROXIMATE RATIO ----------------
|
||||
|
||||
// approxRatio :: Real -> Real -> Ratio
|
||||
const approxRatio = eps => n => {
|
||||
const
|
||||
gcde = (e, x, y) => {
|
||||
const _gcd = (a, b) => (b < e ? a : _gcd(b, a % b));
|
||||
return _gcd(Math.abs(x), Math.abs(y));
|
||||
},
|
||||
c = gcde(Boolean(eps) ? eps : (1 / 10000), 1, n);
|
||||
return Ratio(
|
||||
Math.floor(n / c), // numerator
|
||||
Math.floor(1 / c) // denominator
|
||||
);
|
||||
};
|
||||
const approxRatio = epsilon =>
|
||||
n => {
|
||||
const
|
||||
c = gcdApprox(
|
||||
0 < epsilon
|
||||
? epsilon
|
||||
: (1 / 10000)
|
||||
)(1, n);
|
||||
|
||||
return Ratio(
|
||||
Math.floor(n / c),
|
||||
Math.floor(1 / c)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
// gcdApprox :: Real -> (Real, Real) -> Real
|
||||
const gcdApprox = epsilon =>
|
||||
(x, y) => {
|
||||
const _gcd = (a, b) =>
|
||||
b < epsilon
|
||||
? a
|
||||
: _gcd(b, a % b);
|
||||
|
||||
return _gcd(Math.abs(x), Math.abs(y));
|
||||
};
|
||||
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
// main :: IO ()
|
||||
const main = () =>
|
||||
// Using a tolerance of 1/10000
|
||||
[0.9054054, 0.518518, 0.75]
|
||||
.map(
|
||||
compose(
|
||||
showRatio,
|
||||
approxRatio(0.0001)
|
||||
)
|
||||
)
|
||||
.join("\n");
|
||||
|
||||
|
||||
// ---------------- GENERIC FUNCTIONS ----------------
|
||||
|
||||
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
const compose = (...fs) =>
|
||||
// A function defined by the right-to-left
|
||||
// composition of all the functions in fs.
|
||||
fs.reduce(
|
||||
(f, g) => x => f(g(x)),
|
||||
x => x
|
||||
);
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// Ratio :: Int -> Int -> Ratio
|
||||
const Ratio = (n, d) => ({
|
||||
type: 'Ratio',
|
||||
'n': n, // numerator
|
||||
'd': d // denominator
|
||||
type: "Ratio",
|
||||
n,
|
||||
d
|
||||
});
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f);
|
||||
|
||||
// showJSON :: a -> String
|
||||
const showJSON = x => JSON.stringify(x, null, 2);
|
||||
|
||||
// showRatio :: Ratio -> String
|
||||
const showRatio = nd =>
|
||||
nd.n.toString() + '/' + nd.d.toString();
|
||||
`${nd.n.toString()}/${nd.d.toString()}`;
|
||||
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
func count str$ pat$ .
|
||||
ind = 1
|
||||
while ind + len pat$ - 1 <= len str$
|
||||
if substr str$ ind len pat$ = pat$
|
||||
cnt += 1
|
||||
ind += len pat$
|
||||
else
|
||||
ind += 1
|
||||
.
|
||||
.
|
||||
return cnt
|
||||
.
|
||||
print count "the three truths" "th"
|
||||
print count "ababababab" "abab"
|
||||
print count "11111111" "11"
|
||||
print count "11111111" "12"
|
||||
print count "12" "12"
|
||||
|
|
@ -12,7 +12,7 @@ FILE-CONTROL.
|
|||
|
||||
DATA DIVISION.
|
||||
FILE SECTION.
|
||||
FD TAPE-FILE.
|
||||
FD TAPE-FILE RECORD CONTAINS 51 CHARACTERS.
|
||||
01 TAPE-FILE-RECORD PICTURE IS X(51).
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
|
|
|
|||
|
|
@ -1,20 +1,18 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. object-address-test.
|
||||
DATA DIVISION.
|
||||
LOCAL-STORAGE SECTION.
|
||||
01 int-space.
|
||||
05 val PICTURE 9(5) VALUE 12345.
|
||||
01 addr BASED.
|
||||
05 val PICTURE 9(5) VALUE ZERO.
|
||||
01 point USAGE POINTER.
|
||||
PROCEDURE DIVISION.
|
||||
DISPLAY val OF int-space END-DISPLAY
|
||||
SET point TO ADDRESS OF int-space
|
||||
DISPLAY point END-DISPLAY
|
||||
SET ADDRESS OF addr TO point
|
||||
DISPLAY val OF addr END-DISPLAY
|
||||
MOVE 65535 TO val OF addr
|
||||
DISPLAY val OF addr END-DISPLAY
|
||||
DISPLAY val OF int-space END-DISPLAY
|
||||
STOP RUN.
|
||||
END PROGRAM object-address-test.
|
||||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. object-address-test.
|
||||
DATA DIVISION.
|
||||
LOCAL-STORAGE SECTION.
|
||||
77 int-space PICTURE IS 9(5) VALUE IS 12345.
|
||||
77 addr PICTURE IS 9(5) BASED VALUE IS ZERO.
|
||||
77 point USAGE IS POINTER.
|
||||
PROCEDURE DIVISION.
|
||||
DISPLAY "Value of integer object : " int-space
|
||||
SET point TO ADDRESS OF int-space
|
||||
DISPLAY "Machine address of object : " point
|
||||
SET ADDRESS OF addr TO point
|
||||
DISPLAY "Value of referent object : " addr
|
||||
MOVE 65535 TO int-space
|
||||
DISPLAY "New value of original : " addr
|
||||
DISPLAY "New value of reference : " int-space
|
||||
GOBACK.
|
||||
END PROGRAM object-address-test.
|
||||
|
|
|
|||
34
Task/Curzon-numbers/EasyLang/curzon-numbers.easy
Normal file
34
Task/Curzon-numbers/EasyLang/curzon-numbers.easy
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
func pow_mod b power modulus .
|
||||
x = 1
|
||||
while power > 0
|
||||
if power mod 2 = 1
|
||||
x = x * b mod modulus
|
||||
.
|
||||
b = b * b mod modulus
|
||||
power = power div 2
|
||||
.
|
||||
return x
|
||||
.
|
||||
numfmt 0 4
|
||||
for k = 2 step 2 to 10
|
||||
print "The first 50 Curzon numbers using a base of" & k & ":"
|
||||
n = 1
|
||||
count = 0
|
||||
repeat
|
||||
m = k * n + 1
|
||||
p = pow_mod k n m + 1
|
||||
if p = m
|
||||
count += 1
|
||||
if count <= 50
|
||||
write " " & n
|
||||
if count mod 10 = 0
|
||||
print ""
|
||||
.
|
||||
.
|
||||
.
|
||||
until count = 1000
|
||||
n += 1
|
||||
.
|
||||
print "" ; print "One thousandth: " & n
|
||||
print ""
|
||||
.
|
||||
49
Task/Curzon-numbers/Odin/curzon-numbers.odin
Normal file
49
Task/Curzon-numbers/Odin/curzon-numbers.odin
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package curzon_numbers
|
||||
/* imports */
|
||||
import "core:c/libc"
|
||||
import "core:fmt"
|
||||
/* main block */
|
||||
main :: proc() {
|
||||
for k: int = 2; k <= 10; k += 2 {
|
||||
fmt.println("\nCurzon numbers with base ", k)
|
||||
count := 0
|
||||
n: int = 1
|
||||
for ; count < 50; n += 1 {
|
||||
if is_curzon(n, k) {
|
||||
count += 1
|
||||
libc.printf("%*d ", 4, n)
|
||||
if (count) % 10 == 0 {
|
||||
fmt.printf("\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
for {
|
||||
if is_curzon(n, k) {
|
||||
count += 1}
|
||||
if count == 1000 {
|
||||
break}
|
||||
n += 1
|
||||
}
|
||||
libc.printf("1000th Curzon number with base %d: %d \n", k, n)
|
||||
}
|
||||
}
|
||||
/* definitions */
|
||||
modpow :: proc(base, exp, mod: int) -> int {
|
||||
if mod == 1 {
|
||||
return 0}
|
||||
result: int = 1
|
||||
base := base
|
||||
exp := exp
|
||||
base %= mod
|
||||
for ; exp > 0; exp >>= 1 {
|
||||
if ((exp & 1) == 1) {
|
||||
result = (result * base) % mod}
|
||||
base = (base * base) % mod
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
is_curzon :: proc(n: int, k: int) -> bool {
|
||||
r := k * n //const?
|
||||
return modpow(k, n, r + 1) == r
|
||||
}
|
||||
43
Task/Deceptive-numbers/ALGOL-W/deceptive-numbers.alg
Normal file
43
Task/Deceptive-numbers/ALGOL-W/deceptive-numbers.alg
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
begin % find deceptive numbers - repunits R(n) evenly divisible by composite %
|
||||
% numbers and n+1; see the task talk page based on the second Wren sample %
|
||||
|
||||
% returns true if n is an odd prime, false otherwise, uses trial division %
|
||||
logical procedure isOddPrime ( integer value n ) ;
|
||||
begin
|
||||
logical prime;
|
||||
integer f, f2, toNext;
|
||||
prime := true;
|
||||
f := 3;
|
||||
f2 := 9;
|
||||
toNext := 16; % note: ( 2n + 1 )^2 - ( 2n - 1 )^2 = 8n %
|
||||
while f2 <= n and prime do begin
|
||||
prime := n rem f not = 0;
|
||||
f := f + 2;
|
||||
f2 := toNext;
|
||||
toNext := toNext + 8
|
||||
end while_f2_le_n_and_prime ;
|
||||
prime
|
||||
end isOddPrime ;
|
||||
|
||||
begin % -- task %
|
||||
integer n, count;
|
||||
count := 0;
|
||||
n := 47;
|
||||
while begin n := n + 2;
|
||||
count < 25
|
||||
end
|
||||
do begin
|
||||
if n rem 3 not = 0 and n rem 5 not = 0 and not isOddPrime( n ) then begin
|
||||
integer mp;
|
||||
mp := 10;
|
||||
for p := 2 until n - 1 do mp := ( mp * 10 ) rem n;
|
||||
if mp = 1 then begin
|
||||
count := count + 1;
|
||||
writeon( i_w := 5, s_w := 0, " ", n );
|
||||
if count rem 10 = 0 then write()
|
||||
end if_mp_eq_1
|
||||
end if_have_a_candidate
|
||||
end while_count_lt_50
|
||||
end task
|
||||
|
||||
end.
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
#include <jambo.h>
|
||||
|
||||
#prototype isdeceptive(_X_)
|
||||
#prototype modulepow(_X_,_Y_,_Z_)
|
||||
#synon _isdeceptive Isdeceptive
|
||||
#synon _modulepow ModulePow
|
||||
#define Breaking Goto(exit)
|
||||
Main
|
||||
i = 49, c=0
|
||||
Iterator ( ++i, #(c <> 10), \
|
||||
Print only if ( Is deceptive 'i', Set 'i,"\n"'; ++c ) )
|
||||
End
|
||||
|
||||
Subrutines
|
||||
|
||||
is deceptive ( n )
|
||||
x=7
|
||||
And( Bitand(n,1), And( Mod(n,3), Mod(n,5) )), do {
|
||||
Iterator( x+=6, #( (x*x) <= n ),\
|
||||
#(!( (n%x) && (n%(x+4)) )), do{ \
|
||||
Module Pow (10, Minus one(n), n), Is equal to '1', Breaking } )
|
||||
}
|
||||
Set '0'
|
||||
exit:
|
||||
Return
|
||||
|
||||
module pow(b, e, m)
|
||||
Loop for (p = 1, e, e >>= 1)
|
||||
Bitand(e, 1), do{ #( p = (p * b) % m ) }
|
||||
#( b = (b * b) % m )
|
||||
Next
|
||||
Return (p)
|
||||
38
Task/Deceptive-numbers/Bc/deceptive-numbers.bc
Normal file
38
Task/Deceptive-numbers/Bc/deceptive-numbers.bc
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/* modular exponentiation */
|
||||
define p(b, e, m) {
|
||||
auto r
|
||||
for (r = 1; e > 0; e /= 2) {
|
||||
if (e % 2 == 1) r = r * b % m
|
||||
b = b * b % m
|
||||
}
|
||||
return(r)
|
||||
}
|
||||
|
||||
/* cache for the primes found */
|
||||
p[0] = 7
|
||||
|
||||
define d(n) {
|
||||
auto i, p, r;
|
||||
if (p(10, n - 1, n) == 1) {
|
||||
for (r = sqrt(n); (p = p[i]) <= r; ++i) if (n % p == 0) return(1)
|
||||
p[++l] = n
|
||||
}
|
||||
return(0)
|
||||
}
|
||||
|
||||
/* wheel to skip multiples of 2, 3, and 5 */
|
||||
w[0] = 4
|
||||
w[1] = 2
|
||||
w[2] = 4
|
||||
w[3] = 2
|
||||
w[4] = 4
|
||||
w[5] = 6
|
||||
w[6] = 2
|
||||
w[7] = 6
|
||||
|
||||
for (n = p[0]; c != 10; i = (i + 1) % 8) {
|
||||
if (d(n += w[i]) == 1) {
|
||||
n
|
||||
c += 1
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
uint64_t power_modulus(uint64_t base, uint64_t exponent, uint64_t modulus) {
|
||||
uint64_t power_modulus(uint64_t base, uint64_t exponent, const uint64_t& modulus) {
|
||||
if ( modulus == 1 ) {
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ uint64_t power_modulus(uint64_t base, uint64_t exponent, uint64_t modulus) {
|
|||
base %= modulus;
|
||||
uint64_t result = 1;
|
||||
while ( exponent > 0 ) {
|
||||
if ( ( exponent & 1 ) == 1 ) {
|
||||
if ( ( exponent & 1 ) == 1 ) {
|
||||
result = ( result * base ) % modulus;
|
||||
}
|
||||
base = ( base * base ) % modulus;
|
||||
|
|
@ -20,7 +20,7 @@ uint64_t power_modulus(uint64_t base, uint64_t exponent, uint64_t modulus) {
|
|||
return result;
|
||||
}
|
||||
|
||||
bool is_deceptive(uint32_t n) {
|
||||
bool is_deceptive(const uint32_t& n) {
|
||||
if ( n % 2 != 0 && n % 3 != 0 && n % 5 != 0 && power_modulus(10, n - 1, n) == 1 ) {
|
||||
for ( uint32_t divisor = 7; divisor < sqrt(n); divisor += 6 ) {
|
||||
if ( n % divisor == 0 || n % ( divisor + 4 ) == 0 ) {
|
||||
|
|
|
|||
|
|
@ -1,23 +1,24 @@
|
|||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
|
||||
unsigned modpow(unsigned b, unsigned e, unsigned m)
|
||||
uint32_t modpow(uint32_t b, uint32_t e, uint32_t m)
|
||||
{
|
||||
unsigned p;
|
||||
uint32_t p;
|
||||
for (p = 1; e; e >>= 1) {
|
||||
if (e & 1)
|
||||
p = p * b % m;
|
||||
b = b * b % m;
|
||||
p = (uint64_t)p * b % m;
|
||||
b = (uint64_t)b * b % m;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
int is_deceptive(unsigned n)
|
||||
int is_deceptive(uint32_t n)
|
||||
{
|
||||
unsigned x;
|
||||
if (n & 1 && n % 3 && n % 5) {
|
||||
uint32_t x;
|
||||
if (n & 1 && n % 3 && n % 5 && modpow(10, n - 1, n) == 1) {
|
||||
for (x = 7; x * x <= n; x += 6) {
|
||||
if (!(n % x && n % (x + 4)))
|
||||
return modpow(10, n - 1, n) == 1;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
|
@ -25,10 +26,11 @@ int is_deceptive(unsigned n)
|
|||
|
||||
int main(void)
|
||||
{
|
||||
unsigned c, i = 49;
|
||||
for (c = 0; c != 50; ++i) {
|
||||
if (is_deceptive(i)) {
|
||||
printf(" %u", i);
|
||||
uint32_t n = 49;
|
||||
unsigned int c;
|
||||
for (c = 0; c != 500; ++n) {
|
||||
if (is_deceptive(n)) {
|
||||
printf(" %" PRIu32, n);
|
||||
++c;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
38
Task/Deceptive-numbers/Lua/deceptive-numbers.lua
Normal file
38
Task/Deceptive-numbers/Lua/deceptive-numbers.lua
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
do -- find deceptive numbers - repunits R(n) evenly divisible by composite numbers and n+1
|
||||
-- see tha task talk page based on the second Wren sample
|
||||
|
||||
-- returns true if n is prime, false otherwise, uses trial division %
|
||||
local function isPrime ( n )
|
||||
if n < 3 then return n == 2
|
||||
elseif n % 3 == 0 then return n == 3
|
||||
elseif n % 2 == 0 then return false
|
||||
else
|
||||
local prime = true
|
||||
local f, f2, toNext = 5, 25, 24
|
||||
while f2 <= n and prime do
|
||||
prime = n % f ~= 0
|
||||
f = f + 2
|
||||
f2 = toNext
|
||||
toNext = toNext + 8
|
||||
end
|
||||
return prime
|
||||
end
|
||||
end
|
||||
|
||||
do -- task
|
||||
local n, count = 47, 0
|
||||
while count < 25 do
|
||||
n = n + 2
|
||||
if n % 3 ~= 0 and n % 5 ~= 0 and not isPrime( n ) then
|
||||
local mp = 10
|
||||
for p = 2, n - 1 do mp = ( mp * 10 ) % n end
|
||||
if mp == 1 then
|
||||
count = count + 1
|
||||
io.write( string.format( " %5d", n ) )
|
||||
if count % 10 == 0 then io.write( "\n" ) end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -9,9 +9,9 @@ let is_deceptive n =
|
|||
let rec loop x =
|
||||
x * x <= n && (n mod x = 0 || n mod (x + 4) = 0 || loop (x + 6))
|
||||
in
|
||||
n land 1 <> 0 && n mod 3 <> 0 && n mod 5 <> 0 && loop 7 &&
|
||||
modpow n 10 (pred n) = 1
|
||||
n land 1 <> 0 && n mod 3 <> 0 && n mod 5 <> 0 &&
|
||||
modpow n 10 (pred n) = 1 && loop 7
|
||||
|
||||
let () =
|
||||
Seq.(ints 49 |> filter is_deceptive |> take 500
|
||||
Seq.(ints 49 |> filter is_deceptive |> take 100
|
||||
|> iter (Printf.printf " %u%!")) |> print_newline
|
||||
|
|
|
|||
22
Task/Deceptive-numbers/Phix/deceptive-numbers-2.phix
Normal file
22
Task/Deceptive-numbers/Phix/deceptive-numbers-2.phix
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">100</span><span style="color: #0000FF;">:</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">showlim</span><span style="color: #0000FF;">=</span><span style="color: #000000;">70</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">(),</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first %d deceptive numbers are:\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">showlim</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">count</span><span style="color: #0000FF;"><</span><span style="color: #000000;">limit</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">2</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">gcd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">and</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">and</span> <span style="color: #7060A8;">powmod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">showlim</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %7d%n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">elsif</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">=</span><span style="color: #000000;">limit</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()></span><span style="color: #000000;">t1</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The %d%s is %d\r"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">))</span>
|
||||
<!--
|
||||
|
|
@ -7,4 +7,4 @@ def is_deceptive(n):
|
|||
if not (n % d and n % (d + 4)): return True
|
||||
return False
|
||||
|
||||
print(*islice(filter(is_deceptive, count()), 100))
|
||||
print(*islice(filter(is_deceptive, count(49)), 100))
|
||||
16
Task/Deceptive-numbers/Python/deceptive-numbers-2.py
Normal file
16
Task/Deceptive-numbers/Python/deceptive-numbers-2.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
from itertools import accumulate, cycle, islice
|
||||
from math import isqrt
|
||||
|
||||
primes = []
|
||||
wheel = 4, 2, 4, 2, 4, 6, 2, 6
|
||||
|
||||
def is_pseudo(n):
|
||||
if pow(10, n - 1, n) == 1:
|
||||
s = isqrt(n)
|
||||
for p in primes:
|
||||
if p > s: break
|
||||
if n % p == 0: return True
|
||||
primes.append(n)
|
||||
return False
|
||||
|
||||
print(*islice(filter(is_pseudo, accumulate(cycle(wheel), initial=7)), 100))
|
||||
33
Task/Deceptive-numbers/UNIX-Shell/deceptive-numbers.sh
Normal file
33
Task/Deceptive-numbers/UNIX-Shell/deceptive-numbers.sh
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
is () {
|
||||
return "$((!($1)))"
|
||||
}
|
||||
|
||||
fermat_test () {
|
||||
set -- 1 "$1" "$(($2 - 1))" "$2"
|
||||
while is "$3 > 0"
|
||||
do
|
||||
set -- "$(($1 * (-($3 & 1) & ($2 ^ 1) ^ 1) % $4))" "$(($2 * $2 % $4))" "$(($3 >> 1))" "$4"
|
||||
done
|
||||
return "$(($1 != 1))"
|
||||
}
|
||||
|
||||
set -- 7
|
||||
c=0 n=$1
|
||||
while :
|
||||
do
|
||||
for w in 4 2 4 2 4 6 2 6
|
||||
do
|
||||
fermat_test 10 "$((n += w))" && for p
|
||||
do
|
||||
is 'p * p > n' && {
|
||||
set -- "$@" "$n"
|
||||
break
|
||||
}
|
||||
is 'n % p == 0' && {
|
||||
echo "$n"
|
||||
is '(c += 1) == 10' && exit
|
||||
break
|
||||
}
|
||||
done
|
||||
done
|
||||
done
|
||||
15
Task/Deceptive-numbers/Wren/deceptive-numbers-2.wren
Normal file
15
Task/Deceptive-numbers/Wren/deceptive-numbers-2.wren
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import "./math" for Int
|
||||
|
||||
var count = 0
|
||||
var limit = 25 // or 62
|
||||
var n = 49
|
||||
var deceptive = []
|
||||
while (count < limit) {
|
||||
if (!Int.isPrime(n) && n % 3 != 0 && n % 5 != 0 && Int.modPow(10, n-1, n) == 1) {
|
||||
deceptive.add(n)
|
||||
count = count + 1
|
||||
}
|
||||
n = n + 2
|
||||
}
|
||||
System.print("The first %(limit) deceptive numbers are:")
|
||||
System.print(deceptive)
|
||||
|
|
@ -55,7 +55,7 @@ public program()
|
|||
|
||||
console.printLine("4t = ", i);
|
||||
console.printLine("8t = ", j);
|
||||
console.writeLine("4t + 8t = ");
|
||||
console.write("4t + 8t = ");
|
||||
|
||||
try
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
func$ hex h .
|
||||
for d in [ h div 16 h mod 16 ]
|
||||
if d > 9
|
||||
d += 7
|
||||
.
|
||||
h$ &= strchar (d + 48)
|
||||
.
|
||||
return h$
|
||||
.
|
||||
proc samechar s$ . .
|
||||
s$[] = strchars s$
|
||||
for i = 2 to len s$[]
|
||||
if s$[i] <> s$[i - 1]
|
||||
h = strcode s$[i]
|
||||
write " --> different: '" & s$[i] & "' (" & hex h & "h)"
|
||||
print "' position: " & i
|
||||
return
|
||||
.
|
||||
.
|
||||
print " --> ok"
|
||||
.
|
||||
repeat
|
||||
s$ = input
|
||||
until s$ = "EOF"
|
||||
print "'" & s$ & "'" & " length " & len s$
|
||||
call samechar s$
|
||||
print ""
|
||||
.
|
||||
input_data
|
||||
|
||||
|
||||
2
|
||||
333
|
||||
.55
|
||||
tttTTT
|
||||
4444 444k
|
||||
EOF
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
func$ hex h .
|
||||
for d in [ h div 16 h mod 16 ]
|
||||
if d > 9
|
||||
d += 7
|
||||
.
|
||||
h$ &= strchar (d + 48)
|
||||
.
|
||||
return h$
|
||||
.
|
||||
proc unichar s$ . .
|
||||
len d[] 65536
|
||||
s$[] = strchars s$
|
||||
for i to len s$[]
|
||||
h = strcode s$[i]
|
||||
if d[h] <> 0
|
||||
write " --> duplicates: '" & s$[i] & "' (" & hex h & "h)"
|
||||
print "' positions: " & d[h] & ", " & i
|
||||
return
|
||||
.
|
||||
d[h] = i
|
||||
.
|
||||
print "ok"
|
||||
.
|
||||
repeat
|
||||
s$ = input
|
||||
until s$ = "EOF"
|
||||
print "'" & s$ & "'" & " length " & len s$
|
||||
call unichar s$
|
||||
print ""
|
||||
.
|
||||
input_data
|
||||
|
||||
.
|
||||
abcABC
|
||||
XYZ ZYX
|
||||
1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ
|
||||
EOF
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
(float2fraction=
|
||||
integerPart decimalPart d1 dn exp sign
|
||||
. @( !arg
|
||||
: ~/#?integerPart
|
||||
( &0:?decimalPart:?d1:?dn
|
||||
| "."
|
||||
[?d1
|
||||
(|? 0|`)
|
||||
( &0:?decimalPart
|
||||
| ~/#?decimalPart:>0
|
||||
)
|
||||
[?dn
|
||||
)
|
||||
( &0:?exp
|
||||
| (E|e) ~/#?exp
|
||||
)
|
||||
)
|
||||
& ( !integerPart*(-1:?sign):>0:?integerPart
|
||||
| 1:?sign
|
||||
)
|
||||
& !sign*(!integerPart+!decimalPart*10^(!d1+-1*!dn))*10^!exp
|
||||
);
|
||||
|
||||
( out$float2fraction$"1.2"
|
||||
& out$float2fraction$"1.02"
|
||||
& out$float2fraction$"1.01"
|
||||
& out$float2fraction$"10.01"
|
||||
& out$float2fraction$"10.01e10"
|
||||
& out$float2fraction$"10.01e1"
|
||||
& out$float2fraction$"10.01e2"
|
||||
& out$float2fraction$"10.01e-2"
|
||||
& out$float2fraction$"-10.01e-2"
|
||||
& out$float2fraction$"-10e-2"
|
||||
& out$float2fraction$"0.000"
|
||||
);
|
||||
|
|
@ -3,19 +3,19 @@ import extensions;
|
|||
|
||||
class TestClass
|
||||
{
|
||||
object theVariables;
|
||||
object variables;
|
||||
|
||||
constructor()
|
||||
{
|
||||
theVariables := new DynamicStruct()
|
||||
variables := new DynamicStruct()
|
||||
}
|
||||
|
||||
function()
|
||||
{
|
||||
auto prop := new MessageName(console.write:"Enter the variable name:".readLine());
|
||||
(prop.setPropertyMessage())(theVariables,42);
|
||||
(prop.setPropertyMessage())(variables,42);
|
||||
|
||||
console.printLine(prop.toPrintable(),"=",(prop.getPropertyMessage())(theVariables)).readChar()
|
||||
console.printLine(prop.toPrintable(),"=",(prop.getPropertyMessage())(variables)).readChar()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
(let var-name "hello")
|
||||
((eval (str "(var " var-name ")")) 123)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(let var-name "hello")
|
||||
(eval (str "(var " var-name " 123)"))
|
||||
39
Task/Eban-numbers/EasyLang/eban-numbers.easy
Normal file
39
Task/Eban-numbers/EasyLang/eban-numbers.easy
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
func x n .
|
||||
if n = 0 or n = 2 or n = 4 or n = 6
|
||||
return 1
|
||||
.
|
||||
.
|
||||
proc go start stop printable . .
|
||||
write start & " - " & stop & ":"
|
||||
for i = start step 2 to stop
|
||||
b = i div 1000000000
|
||||
r = i mod 1000000000
|
||||
m = r div 1000000
|
||||
r = i mod 1000000
|
||||
t = r div 1000
|
||||
r = r mod 1000
|
||||
if m >= 30 and m <= 66
|
||||
m = m mod 10
|
||||
.
|
||||
if t >= 30 and t <= 66
|
||||
t = t mod 10
|
||||
.
|
||||
if r >= 30 and r <= 66
|
||||
r = r mod 10
|
||||
.
|
||||
if x b = 1 and x m = 1 and x t = 1 and x r = 1
|
||||
count += 1
|
||||
if printable = 1
|
||||
write " " & i
|
||||
.
|
||||
.
|
||||
.
|
||||
print " (count=" & count & ")"
|
||||
.
|
||||
go 2 1000 1
|
||||
go 1000 4000 1
|
||||
go 2 10000 0
|
||||
go 2 100000 0
|
||||
go 2 1000000 0
|
||||
go 2 10000000 0
|
||||
go 2 100000000 0
|
||||
64
Task/Eban-numbers/Odin/eban-numbers.odin
Normal file
64
Task/Eban-numbers/Odin/eban-numbers.odin
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
package eban_numbers
|
||||
/* imports */
|
||||
import "core:fmt"
|
||||
/* globals */
|
||||
Range :: struct {
|
||||
start: i32,
|
||||
end: i32,
|
||||
print: bool,
|
||||
}
|
||||
printcounter: i32 = 0
|
||||
/* main block */
|
||||
main :: proc() {
|
||||
rgs := [?]Range{
|
||||
{2, 1000, true},
|
||||
{1000, 4000, true},
|
||||
{2, 1e4, false},
|
||||
{2, 1e5, false},
|
||||
{2, 1e6, false},
|
||||
{2, 1e7, false},
|
||||
{2, 1e8, false},
|
||||
{2, 1e9, false},
|
||||
}
|
||||
for rg in rgs {
|
||||
if rg.start == 2 {
|
||||
fmt.printf("eban numbers up to and including %d:\n", rg.end)
|
||||
} else {
|
||||
fmt.printf("eban numbers between %d and %d (inclusive):\n", rg.start, rg.end)
|
||||
}
|
||||
count := i32(0)
|
||||
for i := rg.start; i <= i32(rg.end); i += i32(2) {
|
||||
b := i / 1000000000
|
||||
r := i % 1000000000
|
||||
m := r / 1000000
|
||||
r = i % 1000000
|
||||
t := r / 1000
|
||||
r %= 1000
|
||||
if m >= 30 && m <= 66 {
|
||||
m %= 10
|
||||
}
|
||||
if t >= 30 && t <= 66 {
|
||||
t %= 10
|
||||
}
|
||||
if r >= 30 && r <= 66 {
|
||||
r %= 10
|
||||
}
|
||||
if b == 0 || b == 2 || b == 4 || b == 6 {
|
||||
if m == 0 || m == 2 || m == 4 || m == 6 {
|
||||
if t == 0 || t == 2 || t == 4 || t == 6 {
|
||||
if r == 0 || r == 2 || r == 4 || r == 6 {
|
||||
if rg.print {
|
||||
fmt.printf("%d ", i)
|
||||
}
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if rg.print {
|
||||
fmt.println()
|
||||
}
|
||||
fmt.println("count =", count, "\n")
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
SPECIAL-NAMES.
|
||||
SYMBOLIC CHARACTERS NUL IS 0, TAB IS 9.
|
||||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
SPECIAL-NAMES.
|
||||
SYMBOLIC CHARACTERS NUL IS 0, TAB IS 9.
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
01 Foo CONSTANT AS "Foo".
|
||||
01 Foo CONSTANT AS "Foo".
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
78 Foo VALUE "Foo".
|
||||
78 Foo VALUE "Foo".
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
CONSTANT SECTION.
|
||||
01 Foo VALUE "Foo".
|
||||
CONSTANT SECTION.
|
||||
01 Foo VALUE "Foo".
|
||||
|
|
|
|||
14
Task/Entropy/EasyLang/entropy.easy
Normal file
14
Task/Entropy/EasyLang/entropy.easy
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
func entropy s$ .
|
||||
len d[] 255
|
||||
for c$ in strchars s$
|
||||
d[strcode c$] += 1
|
||||
.
|
||||
for cnt in d[]
|
||||
if cnt > 0
|
||||
prop = cnt / len s$
|
||||
entr -= (prop * logn prop / logn 2)
|
||||
.
|
||||
.
|
||||
return entr
|
||||
.
|
||||
print entropy "1223334444"
|
||||
|
|
@ -29,7 +29,7 @@ class EquilibriumEnumerator : Enumerator
|
|||
|
||||
while(en.next())
|
||||
{
|
||||
var element := en.get();
|
||||
var element := *en;
|
||||
right -= element;
|
||||
bool found := (left == right);
|
||||
left += element;
|
||||
|
|
@ -56,7 +56,7 @@ class EquilibriumEnumerator : Enumerator
|
|||
en.reset();
|
||||
}
|
||||
|
||||
get() = index;
|
||||
get Value() = index;
|
||||
|
||||
enumerable() => en;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
euclid_mullin(n):=if n=1 then 2 else ifactors(1+product(euclid_mullin(i),i,1,n-1))[1][1]$
|
||||
|
||||
/* Test case */
|
||||
makelist(euclid_mullin(k),k,16);
|
||||
|
|
@ -0,0 +1 @@
|
|||
%gamma,numer;
|
||||
1
Task/Eulers-identity/Maxima/eulers-identity.maxima
Normal file
1
Task/Eulers-identity/Maxima/eulers-identity.maxima
Normal file
|
|
@ -0,0 +1 @@
|
|||
is(equal(%e^(%i*%pi)+1,0));
|
||||
6
Task/Even-or-odd/EasyLang/even-or-odd.easy
Normal file
6
Task/Even-or-odd/EasyLang/even-or-odd.easy
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
a = 13
|
||||
if a mod 2 = 0
|
||||
print a & " is even"
|
||||
else
|
||||
print a & " is odd"
|
||||
.
|
||||
|
|
@ -27,39 +27,39 @@ extension evoHelper
|
|||
|
||||
class EvoAlgorithm : Enumerator
|
||||
{
|
||||
object theTarget;
|
||||
object theCurrent;
|
||||
object theVariantCount;
|
||||
object _target;
|
||||
object _current;
|
||||
object _variantCount;
|
||||
|
||||
constructor new(s,count)
|
||||
{
|
||||
theTarget := s;
|
||||
theVariantCount := count.toInt();
|
||||
_target := s;
|
||||
_variantCount := count.toInt();
|
||||
}
|
||||
|
||||
get() = theCurrent;
|
||||
get Value() = _current;
|
||||
|
||||
bool next()
|
||||
{
|
||||
if (nil == theCurrent)
|
||||
{ theCurrent := theTarget.Length.randomString(); ^ true };
|
||||
if (nil == _current)
|
||||
{ _current := _target.Length.randomString(); ^ true };
|
||||
|
||||
if (theTarget == theCurrent)
|
||||
if (_target == _current)
|
||||
{ ^ false };
|
||||
|
||||
auto variants := Array.allocate(theVariantCount).populate:(x => theCurrent.mutate:P );
|
||||
auto variants := Array.allocate(_variantCount).populate:(x => _current.mutate:P );
|
||||
|
||||
theCurrent := variants.sort:(a,b => a.fitnessOf:Target > b.fitnessOf:Target ).at:0;
|
||||
_current := variants.sort:(a,b => a.fitnessOf:Target > b.fitnessOf:Target ).at:0;
|
||||
|
||||
^ true
|
||||
}
|
||||
|
||||
reset()
|
||||
{
|
||||
theCurrent := nil
|
||||
_current := nil
|
||||
}
|
||||
|
||||
enumerable() => theTarget;
|
||||
enumerable() => _target;
|
||||
}
|
||||
|
||||
public program()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
(var c 100) ;number of children in each generation
|
||||
(var p 0.05) ;mutation probability
|
||||
|
||||
(var target "METHINKS IT IS LIKE A WEASEL")
|
||||
(var tsize (len target))
|
||||
|
||||
(var alphabet (to-vec " ABCDEFGHIJLKLMNOPQRSTUVWXYZ"))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(var fitness (comp @(map = target) (count val)))
|
||||
(var perfect-fit? (comp fitness (= tsize)))
|
||||
|
||||
(var rand-char #(rand-pick alphabet))
|
||||
(var mutate (map #(if (< (rand) p) (rand-char) %)))
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
(function evolve generation parent
|
||||
|
||||
(print (pad-left " " 3 generation) " " (... str parent) " " (fitness parent))
|
||||
|
||||
(return-when (perfect-fit? parent))
|
||||
|
||||
(let children (times c #(mutate parent))
|
||||
fittest (max-by fitness (... vec parent children)))
|
||||
|
||||
(recur (inc generation) fittest))
|
||||
|
||||
(evolve 1 (times tsize rand-char))
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
#include <jambo.h>
|
||||
|
||||
Main
|
||||
e=0, se=""
|
||||
Try
|
||||
Gosub 'Foo'
|
||||
Catch (e)
|
||||
Get msg exception, and Move to 'se'
|
||||
Printnl ("+-MAIN-FOO CALL Error: ",e, " : ", se )
|
||||
Finish
|
||||
End
|
||||
|
||||
Subrutines
|
||||
|
||||
Define ' Foo '
|
||||
Gosub ' Bar '
|
||||
Return
|
||||
|
||||
Define ' Bar '
|
||||
Set '0', Gosub ' Biz '
|
||||
Set '1', Gosub ' Biz '
|
||||
Return
|
||||
|
||||
Define ' Biz, x '
|
||||
a=0, b=0
|
||||
If ( x )
|
||||
Let ' b:=Sqrt(-1) '
|
||||
Nan( a ) do{ Raise (1000,"\n+----Func BIZ: NaN!") }
|
||||
Else
|
||||
#( a=log(-1) + log(-1) )
|
||||
Nan( a ) do{ Raise (1001,"\n+----Func BIZ: NaN!") }
|
||||
End If
|
||||
|
||||
Printnl ' "a = ", a, " b = ", b '
|
||||
|
||||
Return
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
#include <jambo.h>
|
||||
|
||||
Main
|
||||
e=0, se=""
|
||||
Try
|
||||
Gosub 'Foo'
|
||||
Catch (e)
|
||||
Get msg exception, and Move to 'se'
|
||||
Printnl ("+-MAIN Error: ",e, " : ", se )
|
||||
Finish
|
||||
End
|
||||
|
||||
Subrutines
|
||||
|
||||
/*
|
||||
This "Try" is not considered nested, then, it is necessary
|
||||
to capture the error and raise the error
|
||||
*/
|
||||
Define ' Foo '
|
||||
Try
|
||||
Gosub ' Bar '
|
||||
Catch (e)
|
||||
Get msg exception, and Move to 'se'
|
||||
Free try // absolutly nessesary in this chase!
|
||||
Raise (e, Cat ("\n+--FUNC FOO: ", se) )
|
||||
Finish
|
||||
Return
|
||||
|
||||
Define ' Bar '
|
||||
Try
|
||||
Set '0', Gosub ' Biz '
|
||||
Set '1', Gosub ' Biz '
|
||||
Catch(e)
|
||||
Get msg exception, and Move to 'se'
|
||||
Free try // absolutly nessesary in this chase!
|
||||
Raise (e, Cat ("\n+---FUNC BAR: ", se) )
|
||||
Finish
|
||||
|
||||
Return
|
||||
|
||||
Define ' Biz, x '
|
||||
a=0, b=0
|
||||
If ( x )
|
||||
Let ' b:=Sqrt(-1) '
|
||||
Nan( a ) do{ Raise (1000,"\n+----Func BIZ: NaN!") }
|
||||
Else
|
||||
#( a=log(-1) + log(-1) )
|
||||
Nan( a ) do{ Raise (1001,"\n+----Func BIZ: NaN!") }
|
||||
End If
|
||||
|
||||
Printnl ' "a = ", a, " b = ", b '
|
||||
|
||||
Return
|
||||
|
|
@ -2,48 +2,48 @@ import extensions;
|
|||
|
||||
class U0 : Exception
|
||||
{
|
||||
constructor new()
|
||||
<= new();
|
||||
constructor new()
|
||||
<= super new("U0 exception");
|
||||
}
|
||||
|
||||
class U1 : Exception
|
||||
{
|
||||
constructor new()
|
||||
<= new();
|
||||
constructor new()
|
||||
<= super new("U1 exception");
|
||||
}
|
||||
|
||||
singleton Exceptions
|
||||
{
|
||||
static int i;
|
||||
static int i;
|
||||
|
||||
bar()
|
||||
<= baz();
|
||||
bar()
|
||||
<= baz();
|
||||
|
||||
baz()
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
U0.raise()
|
||||
}
|
||||
else
|
||||
{
|
||||
U1.raise()
|
||||
}
|
||||
}
|
||||
baz()
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
U0.raise()
|
||||
}
|
||||
else
|
||||
{
|
||||
U1.raise()
|
||||
}
|
||||
}
|
||||
|
||||
foo()
|
||||
{
|
||||
for(i := 0, i < 2, i += 1)
|
||||
{
|
||||
try
|
||||
{
|
||||
self.bar()
|
||||
}
|
||||
catch(U0 e)
|
||||
{
|
||||
console.printLine("U0 Caught")
|
||||
}
|
||||
}
|
||||
foo()
|
||||
{
|
||||
for(i := 0, i < 2, i := i + 1)
|
||||
{
|
||||
try
|
||||
{
|
||||
self.bar()
|
||||
}
|
||||
catch(U0 e)
|
||||
{
|
||||
console.printLine("U0 Caught")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
class MyException : Exception
|
||||
{
|
||||
constructor new()
|
||||
<= new("MyException raised");
|
||||
<= super new("MyException raised");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
try
|
||||
{
|
||||
o.foo()
|
||||
foo()
|
||||
}
|
||||
catch:(MyException e)
|
||||
catch(MyException e)
|
||||
{
|
||||
// handle exceptions of type MyException and derived
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,8 @@
|
|||
o.foo() | on:(e)
|
||||
try
|
||||
{
|
||||
foo.fail()
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
// handle any type of exception
|
||||
};
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue