Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
23
Task/Hailstone-sequence/Ceylon/hailstone-sequence.ceylon
Normal file
23
Task/Hailstone-sequence/Ceylon/hailstone-sequence.ceylon
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
shared void run() {
|
||||
|
||||
{Integer*} hailstone(variable Integer n) {
|
||||
variable [Integer*] stones = [n];
|
||||
while(n != 1) {
|
||||
n = if(n.even) then n / 2 else 3 * n + 1;
|
||||
stones = stones.append([n]);
|
||||
}
|
||||
return stones;
|
||||
}
|
||||
|
||||
value hs27 = hailstone(27);
|
||||
print("hailstone sequence for 27 is ``hs27.take(3)``...``hs27.skip(hs27.size - 3).take(3)`` with length ``hs27.size``");
|
||||
|
||||
variable value longest = hailstone(1);
|
||||
for(i in 2..100k - 1) {
|
||||
value current = hailstone(i);
|
||||
if(current.size > longest.size) {
|
||||
longest = current;
|
||||
}
|
||||
}
|
||||
print("the longest sequence under 100,000 starts with ``longest.first else "what?"`` and has length ``longest.size``");
|
||||
}
|
||||
30
Task/Hailstone-sequence/ERRE/hailstone-sequence.erre
Normal file
30
Task/Hailstone-sequence/ERRE/hailstone-sequence.erre
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
PROGRAM ULAM
|
||||
|
||||
!$DOUBLE
|
||||
|
||||
PROCEDURE HAILSTONE(X,PRT%->COUNT)
|
||||
COUNT=1
|
||||
IF PRT% THEN PRINT(X,) END IF
|
||||
REPEAT
|
||||
IF X/2<>INT(X/2) THEN
|
||||
X=X*3+1
|
||||
ELSE
|
||||
X=X/2
|
||||
END IF
|
||||
IF PRT% THEN PRINT(X,) END IF
|
||||
COUNT=COUNT+1
|
||||
UNTIL X=1
|
||||
IF PRT% THEN PRINT END IF
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
HAILSTONE(27,TRUE->COUNT)
|
||||
PRINT("Sequence length for 27:";COUNT)
|
||||
MAX_COUNT=2
|
||||
NMAX=2
|
||||
FOR I=3 TO 100000 DO
|
||||
HAILSTONE(I,FALSE->COUNT)
|
||||
IF COUNT>MAX_COUNT THEN NMAX=I MAX_COUNT=COUNT END IF
|
||||
END FOR
|
||||
PRINT("Max. number is";NMAX;" with";MAX_COUNT;"elements")
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
(lib 'hash)
|
||||
(lib 'sequences)
|
||||
(lib 'compile)
|
||||
|
||||
(define (hailstone n)
|
||||
(when (> n 1)
|
||||
(if (even? n) (/ n 2) (1+ (* n 3)))))
|
||||
|
||||
(define H (make-hash))
|
||||
|
||||
;; (iterator/f seed f) returns seed, (f seed) (f(f seed)) ...
|
||||
|
||||
(define (hlength seed)
|
||||
(define collatz (iterator/f hailstone seed))
|
||||
(or
|
||||
(hash-ref H seed) ;; known ?
|
||||
(hash-set H seed
|
||||
(for ((i (in-naturals)) (h collatz))
|
||||
;; add length of subsequence if already known
|
||||
#:break (hash-ref H h) => (+ i (hash-ref H h))
|
||||
(1+ i)))))
|
||||
|
||||
(define (task (nmax 100000))
|
||||
(for ((n [1 .. nmax])) (hlength n)) ;; fill hash table
|
||||
|
||||
(define hmaxlength (apply max (hash-values H)))
|
||||
(define hmaxseed (hash-get-key H hmaxlength))
|
||||
(writeln 'maxlength= hmaxlength 'for hmaxseed))
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
(define H27 (iterator/f hailstone 27))
|
||||
(take H27 6)
|
||||
→ (27 82 41 124 62 31)
|
||||
(length H27)
|
||||
→ 112
|
||||
(list-tail (take H27 112) -6)
|
||||
→ (5 16 8 4 2 1)
|
||||
|
||||
(task)
|
||||
maxlength= 351 for 77031
|
||||
|
||||
;; more ...
|
||||
(lib 'bigint)
|
||||
|
||||
(task 200000)
|
||||
maxlength= 383 for 156159
|
||||
(task 300000)
|
||||
maxlength= 443 for 230631
|
||||
(task 400000)
|
||||
maxlength= 443 for 230631
|
||||
(task 500000)
|
||||
maxlength= 449 for 410011
|
||||
(task 600000)
|
||||
maxlength= 470 for 511935
|
||||
(task 700000)
|
||||
maxlength= 509 for 626331
|
||||
(task 800000)
|
||||
maxlength= 509 for 626331
|
||||
(task 900000)
|
||||
maxlength= 525 for 837799
|
||||
(task 1000000)
|
||||
maxlength= 525 for 837799
|
||||
20
Task/Hailstone-sequence/Ezhil/hailstone-sequence.ezhil
Normal file
20
Task/Hailstone-sequence/Ezhil/hailstone-sequence.ezhil
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
நிரல்பாகம் hailstone ( எண் )
|
||||
பதிப்பி "=> ",எண் #hailstone seq
|
||||
@( எண் == 1 ) ஆனால்
|
||||
பின்கொடு எண்
|
||||
முடி
|
||||
|
||||
@( (எண்%2) == 1 ) ஆனால்
|
||||
hailstone( 3*எண் + 1)
|
||||
இல்லை
|
||||
hailstone( எண்/2 )
|
||||
முடி
|
||||
முடி
|
||||
|
||||
|
||||
எண்கள் = [5,17,19,23,37]
|
||||
@(எண்கள் இல் இவ்வெண்) ஒவ்வொன்றாக
|
||||
பதிப்பி "****** calculating hailstone seq for ",இவ்வெண்," *********"
|
||||
hailstone( இவ்வெண் )
|
||||
பதிப்பி "**********************************************"
|
||||
முடி
|
||||
11
Task/Hailstone-sequence/FunL/hailstone-sequence.funl
Normal file
11
Task/Hailstone-sequence/FunL/hailstone-sequence.funl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def
|
||||
hailstone( 1 ) = [1]
|
||||
hailstone( n ) = n # hailstone( if 2|n then n/2 else n*3 + 1 )
|
||||
|
||||
if _name_ == '-main-'
|
||||
h27 = hailstone( 27 )
|
||||
assert( h27.length() == 112 and h27.startsWith([27, 82, 41, 124]) and h27.endsWith([8, 4, 2, 1]) )
|
||||
|
||||
val (n, len) = maxBy( snd, [(i, hailstone( i ).length()) | i <- 1:100000] )
|
||||
|
||||
println( n, len )
|
||||
32
Task/Hailstone-sequence/Futhark/hailstone-sequence.futhark
Normal file
32
Task/Hailstone-sequence/Futhark/hailstone-sequence.futhark
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
fun hailstone_step(x: int): int =
|
||||
if (x % 2) == 0
|
||||
then x/2
|
||||
else (3*x) + 1
|
||||
|
||||
fun hailstone_seq(x: int): []int =
|
||||
let capacity = 100
|
||||
let i = 1
|
||||
let steps = replicate capacity (-1)
|
||||
let steps[0] = x
|
||||
loop ((capacity,i,steps,x)) = while x != 1 do
|
||||
let (steps, capacity) =
|
||||
if i == capacity then
|
||||
(concat steps (replicate capacity (-1)),
|
||||
capacity * 2)
|
||||
else (steps, capacity)
|
||||
let x = hailstone_step x
|
||||
let steps[i] = x
|
||||
in (capacity, i+1, steps, x)
|
||||
in (split i steps).0
|
||||
|
||||
fun hailstone_len(x: int): int =
|
||||
let i = 1
|
||||
loop ((i,x)) = while x != 1 do
|
||||
(i+1, hailstone_step x)
|
||||
in i
|
||||
|
||||
fun max (x: int) (y: int): int = if x < y then y else x
|
||||
|
||||
fun main (x: int) (n: int): ([]int, int) =
|
||||
(hailstone_seq x,
|
||||
reduce max 0 (map hailstone_len (map (1+) (iota (n-1)))))
|
||||
32
Task/Hailstone-sequence/Lasso/hailstone-sequence.lasso
Normal file
32
Task/Hailstone-sequence/Lasso/hailstone-sequence.lasso
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
[
|
||||
define_tag("hailstone", -required="n", -type="integer", -copy);
|
||||
local("sequence") = array(#n);
|
||||
while(#n != 1);
|
||||
((#n % 2) == 0) ? #n = (#n / 2) | #n = (#n * 3 + 1);
|
||||
#sequence->insert(#n);
|
||||
/while;
|
||||
return(#sequence);
|
||||
/define_tag;
|
||||
|
||||
local("result");
|
||||
#result = hailstone(27);
|
||||
while(#result->size > 8);
|
||||
#result->remove(5);
|
||||
/while;
|
||||
#result->insert("...",5);
|
||||
|
||||
"Hailstone sequence for n = 27 -> { " + #result->join(", ") + " }";
|
||||
|
||||
local("longest_sequence") = 0;
|
||||
local("longest_index") = 0;
|
||||
loop(-from=1, -to=100000);
|
||||
local("length") = hailstone(loop_count)->size;
|
||||
if(#length > #longest_sequence);
|
||||
#longest_index = loop_count;
|
||||
#longest_sequence = #length;
|
||||
/if;
|
||||
/loop;
|
||||
|
||||
"<br/>";
|
||||
"Number with the longest sequence under 100,000: " #longest_index + ", with " + #longest_sequence + " elements.";
|
||||
]
|
||||
14
Task/Hailstone-sequence/Lingo/hailstone-sequence-1.lingo
Normal file
14
Task/Hailstone-sequence/Lingo/hailstone-sequence-1.lingo
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
on hailstone (n, sequenceList)
|
||||
len = 1
|
||||
repeat while n<>1
|
||||
if listP(sequenceList) then sequenceList.add(n)
|
||||
if n mod 2 = 0 then
|
||||
n = n / 2
|
||||
else
|
||||
n = 3 * n + 1
|
||||
end if
|
||||
len = len + 1
|
||||
end repeat
|
||||
if listP(sequenceList) then sequenceList.add(n)
|
||||
return len
|
||||
end
|
||||
16
Task/Hailstone-sequence/Lingo/hailstone-sequence-2.lingo
Normal file
16
Task/Hailstone-sequence/Lingo/hailstone-sequence-2.lingo
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
sequenceList = []
|
||||
hailstone(27, sequenceList)
|
||||
put sequenceList
|
||||
-- [27, 82, 41, 124, ... , 8, 4, 2, 1]
|
||||
|
||||
n = 0
|
||||
maxLen = 0
|
||||
repeat with i = 1 to 99999
|
||||
len = hailstone(i)
|
||||
if len>maxLen then
|
||||
n = i
|
||||
maxLen = len
|
||||
end if
|
||||
end repeat
|
||||
put n, maxLen
|
||||
-- 77031 351
|
||||
19
Task/Hailstone-sequence/Nim/hailstone-sequence.nim
Normal file
19
Task/Hailstone-sequence/Nim/hailstone-sequence.nim
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
proc hailstone(n): auto =
|
||||
result = @[n]
|
||||
var n = n
|
||||
while n > 1:
|
||||
if (n and 1) == 1:
|
||||
n = 3 * n + 1
|
||||
else:
|
||||
n = n div 2
|
||||
result.add n
|
||||
|
||||
let h = hailstone 27
|
||||
assert h.len == 112 and h[0..3] == @[27,82,41,124] and h[h.high-3..h.high] == @[8,4,2,1]
|
||||
var m, mi = 0
|
||||
for i in 1 .. <100_000:
|
||||
let n = hailstone(i).len
|
||||
if n > m:
|
||||
m = n
|
||||
mi = i
|
||||
echo "Maximum length ", m, " was found for hailstone(", mi, ") for numbers <100,000"
|
||||
8
Task/Hailstone-sequence/Oforth/hailstone-sequence.oforth
Normal file
8
Task/Hailstone-sequence/Oforth/hailstone-sequence.oforth
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
: hailstone // n -- [n]
|
||||
| l |
|
||||
ListBuffer new ->l
|
||||
while(dup 1 <>) [ dup l add dup isEven ifTrue: [ 2 / ] else: [ 3 * 1+ ] ]
|
||||
l add l dup freeze ;
|
||||
|
||||
hailstone(27) dup size println dup left(4) println right(4) println
|
||||
100000 seq map(#[ dup hailstone size swap Pair new ]) reduce(#maxKey) println
|
||||
43
Task/Hailstone-sequence/Phix/hailstone-sequence.phix
Normal file
43
Task/Hailstone-sequence/Phix/hailstone-sequence.phix
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
function hailstone(atom n)
|
||||
sequence s = {n}
|
||||
while n!=1 do
|
||||
if remainder(n,2)=0 then
|
||||
n /= 2
|
||||
else
|
||||
n = 3*n+1
|
||||
end if
|
||||
s &= n
|
||||
end while
|
||||
return s
|
||||
end function
|
||||
|
||||
function hailstone_count(atom n)
|
||||
integer count = 1
|
||||
while n!=1 do
|
||||
if remainder(n,2)=0 then
|
||||
n /= 2
|
||||
else
|
||||
n = 3*n+1
|
||||
end if
|
||||
count += 1
|
||||
end while
|
||||
return count
|
||||
end function
|
||||
|
||||
sequence s = hailstone(27)
|
||||
integer ls = length(s)
|
||||
s[5..-5] = {".."}
|
||||
puts(1,"hailstone(27) = ")
|
||||
? s
|
||||
printf(1,"length = %d\n\n",ls)
|
||||
|
||||
integer hmax = 1, imax = 1,count
|
||||
for i=2 to 1e5-1 do
|
||||
count = hailstone_count(i)
|
||||
if count>hmax then
|
||||
hmax = count
|
||||
imax = i
|
||||
end if
|
||||
end for
|
||||
|
||||
printf(1,"The longest hailstone sequence under 100,000 is %d with %d elements.\n",{imax,hmax})
|
||||
20
Task/Hailstone-sequence/Ring/hailstone-sequence.ring
Normal file
20
Task/Hailstone-sequence/Ring/hailstone-sequence.ring
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
size = 27
|
||||
aList = []
|
||||
hailstone(size)
|
||||
|
||||
func hailstone n
|
||||
add(aList,n)
|
||||
while n != 1
|
||||
if n % 2 = 0 n = n / 2
|
||||
else n = 3 * n + 1 ok
|
||||
add(aList, n)
|
||||
end
|
||||
see "first 4 elements : "
|
||||
for i = 1 to 4
|
||||
see "" + aList[i] + " "
|
||||
next
|
||||
see nl
|
||||
see "last 4 elements : "
|
||||
for i = len(aList) - 3 to len(aList)
|
||||
see "" + aList[i] + " "
|
||||
next
|
||||
24
Task/Hailstone-sequence/Sidef/hailstone-sequence.sidef
Normal file
24
Task/Hailstone-sequence/Sidef/hailstone-sequence.sidef
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
func hailstone (n) {
|
||||
var sequence = [n];
|
||||
while (n > 1) {
|
||||
sequence.append(
|
||||
n.is_even ? n.div!(2)
|
||||
: n.mul!(3).add!(1)
|
||||
);
|
||||
}
|
||||
return(sequence);
|
||||
}
|
||||
|
||||
# The hailstone sequence for the number 27
|
||||
var arr = hailstone(var nr = 27);
|
||||
say "#{nr}: #{arr.first(4).to_s} ... #{arr.last(4).to_s} (#{arr.len})";
|
||||
|
||||
# The longest hailstone sequence for a number less than 100,000
|
||||
var h = [0, 0];
|
||||
99_999.times { |i|
|
||||
(var l = hailstone(i).len) > h[1] && (
|
||||
h = [i, l];
|
||||
);
|
||||
}
|
||||
|
||||
printf("%d: (%d)\n", h...);
|
||||
34
Task/Hailstone-sequence/Swift/hailstone-sequence.swift
Normal file
34
Task/Hailstone-sequence/Swift/hailstone-sequence.swift
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
func hailstone(var n:Int) -> [Int] {
|
||||
|
||||
var arr = [n]
|
||||
|
||||
while n != 1 {
|
||||
|
||||
if n % 2 == 0 {
|
||||
n /= 2
|
||||
} else {
|
||||
n = (3 * n) + 1
|
||||
}
|
||||
|
||||
arr.append(n)
|
||||
}
|
||||
|
||||
return arr
|
||||
}
|
||||
|
||||
let n = hailstone(27)
|
||||
|
||||
println("hailstone(27): \(n[0...3]) ... \(n[n.count-4...n.count-1]) for a count of \(n.count).")
|
||||
|
||||
var longest = (n: 1, len: 1)
|
||||
|
||||
for i in 1...100_000 {
|
||||
|
||||
let new = hailstone(i)
|
||||
|
||||
if new.count > longest.len {
|
||||
longest = (i, new.count)
|
||||
}
|
||||
}
|
||||
|
||||
println("Longest sequence for numbers under 100,000 is with \(longest.n). Which has \(longest.len) items.")
|
||||
15
Task/Hailstone-sequence/Ursa/hailstone-sequence.ursa
Normal file
15
Task/Hailstone-sequence/Ursa/hailstone-sequence.ursa
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import "math"
|
||||
|
||||
def hailstone (int n)
|
||||
decl int<> seq
|
||||
while (> n 1)
|
||||
append n seq
|
||||
if (= (mod n 2) 0)
|
||||
set n (floor (/ n 2))
|
||||
else
|
||||
set n (int (+ (* 3 n) 1))
|
||||
end if
|
||||
end while
|
||||
append n seq
|
||||
return seq
|
||||
end hailstone
|
||||
16
Task/Hailstone-sequence/jq/hailstone-sequence-1.jq
Normal file
16
Task/Hailstone-sequence/jq/hailstone-sequence-1.jq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
# Generate the hailstone sequence as a stream to save space (and time) when counting
|
||||
def hailstone:
|
||||
recurse( if . > 1 then
|
||||
if . % 2 == 0 then ./2|floor else 3*. + 1 end
|
||||
else empty
|
||||
end );
|
||||
|
||||
def count(g): reduce g as $i (0; .+1);
|
||||
|
||||
# return [i, length] for the first maximal-length hailstone sequence where i is in [1 .. n]
|
||||
def max_hailstone(n):
|
||||
# state: [i, length]
|
||||
reduce range(1; n+1) as $i
|
||||
([0,0];
|
||||
($i | count(hailstone)) as $l
|
||||
| if $l > .[1] then [$i, $l] else . end);
|
||||
7
Task/Hailstone-sequence/jq/hailstone-sequence-2.jq
Normal file
7
Task/Hailstone-sequence/jq/hailstone-sequence-2.jq
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[27|hailstone] as $h
|
||||
| "[27|hailstone]|length is \($h|length)",
|
||||
"The first four numbers: \($h[0:4])",
|
||||
"The last four numbers: \($h|.[length-4:length])",
|
||||
"",
|
||||
max_hailstone(100000) as $m
|
||||
| "Maximum length for n|hailstone for n in 1..100000 is \($m[1]) (n == \($m[0]))"
|
||||
6
Task/Hailstone-sequence/jq/hailstone-sequence-3.jq
Normal file
6
Task/Hailstone-sequence/jq/hailstone-sequence-3.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
$ jq -M -r -n -f hailstone.jq
|
||||
[27|hailstone]|length is 112
|
||||
The first four numbers: [27,82,41,124]
|
||||
The last four numbers: [8,4,2,1]
|
||||
|
||||
Maximum length for n|hailstone for n in 1..100000 is 351 (n == 77031)
|
||||
Loading…
Add table
Add a link
Reference in a new issue