update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
29
Task/Hailstone-sequence/AWK/hailstone-sequence.awk
Normal file
29
Task/Hailstone-sequence/AWK/hailstone-sequence.awk
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#!/usr/bin/awk -f
|
||||
function hailstone(v, verbose) {
|
||||
n = 1;
|
||||
u = v;
|
||||
while (1) {
|
||||
if (verbose) printf " "u;
|
||||
if (u==1) return(n);
|
||||
n++;
|
||||
if (u%2 > 0 )
|
||||
u = 3*u+1;
|
||||
else
|
||||
u = u/2;
|
||||
}
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
i = 27;
|
||||
printf("hailstone(%i) has %i elements\n",i,hailstone(i,1));
|
||||
ix=0;
|
||||
m=0;
|
||||
for (i=1; i<100000; i++) {
|
||||
n = hailstone(i,0);
|
||||
if (m<n) {
|
||||
m=n;
|
||||
ix=i;
|
||||
}
|
||||
}
|
||||
printf("longest hailstone sequence is %i and has %i elements\n",ix,m);
|
||||
}
|
||||
37
Task/Hailstone-sequence/Io/hailstone-sequence.io
Normal file
37
Task/Hailstone-sequence/Io/hailstone-sequence.io
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
makeItHail := method(n,
|
||||
stones := list(n)
|
||||
while (n != 1,
|
||||
if(n isEven,
|
||||
n = n / 2,
|
||||
n = 3 * n + 1
|
||||
)
|
||||
stones append(n)
|
||||
)
|
||||
)
|
||||
|
||||
out := makeItHail(27)
|
||||
writeln("For the sequence beginning at 27, the number of elements generated is ", out size, ".")
|
||||
write("The first four elements generated are ")
|
||||
for(i, 0, 3,
|
||||
write(out at(i), " ")
|
||||
)
|
||||
writeln(".")
|
||||
|
||||
write("The last four elements generated are ")
|
||||
for(i, out size - 4, out size - 1,
|
||||
write(out at(i), " ")
|
||||
)
|
||||
writeln(".")
|
||||
|
||||
numOfElems := 0
|
||||
nn := 3
|
||||
for(x, 3, 100000,
|
||||
out = makeItHail(x)
|
||||
if(out size > numOfElems,
|
||||
numOfElems = out size
|
||||
nn = x
|
||||
)
|
||||
)
|
||||
|
||||
writeln("For numbers less than or equal to 100,000, ", nn,
|
||||
" has the longest sequence of ", numOfElems, " elements.")
|
||||
|
|
@ -1,19 +1,20 @@
|
|||
#lang racket
|
||||
(define memo (make-hash))
|
||||
(hash-set! memo 1 '((1) 1))
|
||||
|
||||
(define (hailstone n)
|
||||
(hash-ref memo n
|
||||
(λ ()
|
||||
(define h (hailstone (if (even? n) (/ n 2) (+ (* 3 n) 1))))
|
||||
(hash-set! memo n (list (cons n (first h)) (+ (second h) 1)))
|
||||
(hash-ref memo n))))
|
||||
(define hailstone
|
||||
(let ([t (make-hasheq)])
|
||||
(hash-set! t 1 '(1))
|
||||
(λ(n) (hash-ref! t n
|
||||
(λ() (cons n (hailstone (if (even? n) (/ n 2) (+ (* 3 n) 1)))))))))
|
||||
|
||||
(define h27 (first (hailstone 27)))
|
||||
(define h27 (hailstone 27))
|
||||
(printf "h(27) = ~s, ~s items\n"
|
||||
`(,@(take h27 4) ... ,@(take-right h27 4))
|
||||
(length h27))
|
||||
|
||||
(printf "first 4 elements of h(27): ~v\n" (take h27 4))
|
||||
(printf "last 4 elements of h(27): ~v\n" (take-right h27 4))
|
||||
|
||||
(printf "x < 10000 such that h(x) gives the longest sequence: ")
|
||||
(for/fold ([m 0]) ([n (in-range 1 100000)])
|
||||
(max m (second (hailstone n))))
|
||||
(define N 100000)
|
||||
(define longest
|
||||
(for/fold ([m #f]) ([i (in-range 1 (add1 N))])
|
||||
(define h (hailstone i))
|
||||
(if (and m (> (cdr m) (length h))) m (cons i (length h)))))
|
||||
(printf "for x<=~s, ~s has the longest sequence with ~s items\n"
|
||||
N (car longest) (cdr longest))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue