langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
44
Task/Hailstone-sequence/NetRexx/hailstone-sequence.netrexx
Normal file
44
Task/Hailstone-sequence/NetRexx/hailstone-sequence.netrexx
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/* NetRexx */
|
||||
|
||||
options replace format comments java crossref savelog symbols binary
|
||||
|
||||
do
|
||||
start = 27
|
||||
hs = hailstone(start)
|
||||
hsCount = hs.words
|
||||
say 'The number' start 'has a hailstone sequence comprising' hsCount 'elements'
|
||||
say ' its first four elements are:' hs.subword(1, 4)
|
||||
say ' and last four elements are:' hs.subword(hsCount - 3)
|
||||
|
||||
hsMax = 0
|
||||
hsCountMax = 0
|
||||
llimit = 100000
|
||||
loop x_ = 1 to llimit - 1
|
||||
hs = hailstone(x_)
|
||||
hsCount = hs.words
|
||||
if hsCount > hsCountMax then do
|
||||
hsMax = x_
|
||||
hsCountMax = hsCount
|
||||
end
|
||||
end x_
|
||||
|
||||
say 'The number' hsMax 'has the longest hailstone sequence in the range 1 to' llimit - 1 'with a sequence length of' hsCountMax
|
||||
catch ex = Exception
|
||||
ex.printStackTrace
|
||||
end
|
||||
|
||||
return
|
||||
|
||||
method hailstone(hn = long) public static returns Rexx signals IllegalArgumentException
|
||||
|
||||
hs = Rexx('')
|
||||
if hn <= 0 then signal IllegalArgumentException('Invalid start point. Must be a positive integer greater than 0')
|
||||
|
||||
loop label n_ while hn > 1
|
||||
hs = hs' 'hn
|
||||
if hn // 2 \= 0 then hn = hn * 3 + 1
|
||||
else hn = hn % 2
|
||||
end n_
|
||||
hs = hs' 'hn
|
||||
|
||||
return hs.strip
|
||||
63
Task/Hailstone-sequence/OCaml/hailstone-sequence.ocaml
Normal file
63
Task/Hailstone-sequence/OCaml/hailstone-sequence.ocaml
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#load "nums.cma";;
|
||||
open Num;;
|
||||
|
||||
(* generate Hailstone sequence *)
|
||||
let hailstone n =
|
||||
let one = Int 1
|
||||
and two = Int 2
|
||||
and three = Int 3 in
|
||||
let rec g s x =
|
||||
if x =/ one
|
||||
then x::s
|
||||
else g (x::s) (if mod_num x two =/ one
|
||||
then three */ x +/ one
|
||||
else x // two)
|
||||
in
|
||||
g [] (Int n)
|
||||
;;
|
||||
|
||||
(* compute only sequence length *)
|
||||
let haillen n =
|
||||
let one = Int 1
|
||||
and two = Int 2
|
||||
and three = Int 3 in
|
||||
let rec g s x =
|
||||
if x =/ one
|
||||
then s+1
|
||||
else g (s+1) (if mod_num x two =/ one
|
||||
then three */ x +/ one
|
||||
else x // two)
|
||||
in
|
||||
g 0 (Int n)
|
||||
;;
|
||||
|
||||
(* max length for starting values in 1..n *)
|
||||
let hailmax =
|
||||
let rec g idx len = function
|
||||
| 0 -> (idx, len)
|
||||
| i ->
|
||||
let a = haillen i in
|
||||
if a > len
|
||||
then g i a (i-1)
|
||||
else g idx len (i-1)
|
||||
in
|
||||
g 0 0
|
||||
;;
|
||||
|
||||
hailmax 100000 ;;
|
||||
(* - : int * int = (77031, 351) *)
|
||||
|
||||
List.rev_map string_of_num (hailstone 27) ;;
|
||||
|
||||
(* - : string list =
|
||||
["27"; "82"; "41"; "124"; "62"; "31"; "94"; "47"; "142"; "71"; "214"; "107";
|
||||
"322"; "161"; "484"; "242"; "121"; "364"; "182"; "91"; "274"; "137"; "412";
|
||||
"206"; "103"; "310"; "155"; "466"; "233"; "700"; "350"; "175"; "526"; "263";
|
||||
"790"; "395"; "1186"; "593"; "1780"; "890"; "445"; "1336"; "668"; "334";
|
||||
"167"; "502"; "251"; "754"; "377"; "1132"; "566"; "283"; "850"; "425";
|
||||
"1276"; "638"; "319"; "958"; "479"; "1438"; "719"; "2158"; "1079"; "3238";
|
||||
"1619"; "4858"; "2429"; "7288"; "3644"; "1822"; "911"; "2734"; "1367";
|
||||
"4102"; "2051"; "6154"; "3077"; "9232"; "4616"; "2308"; "1154"; "577";
|
||||
"1732"; "866"; "433"; "1300"; "650"; "325"; "976"; "488"; "244"; "122";
|
||||
"61"; "184"; "92"; "46"; "23"; "70"; "35"; "106"; "53"; "160"; "80"; "40";
|
||||
"20"; "10"; "5"; "16"; "8"; "4"; "2"; "1"] *)
|
||||
80
Task/Hailstone-sequence/Oberon-2/hailstone-sequence.oberon-2
Normal file
80
Task/Hailstone-sequence/Oberon-2/hailstone-sequence.oberon-2
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
MODULE hailst;
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
CONST maxCard = MAX (INTEGER) DIV 3;
|
||||
List = 1;
|
||||
Count = 2;
|
||||
Max = 3;
|
||||
|
||||
VAR a : INTEGER;
|
||||
|
||||
PROCEDURE HailStone (start, type : INTEGER) : INTEGER;
|
||||
|
||||
VAR n, max, count : INTEGER;
|
||||
|
||||
BEGIN
|
||||
count := 1;
|
||||
n := start;
|
||||
max := n;
|
||||
LOOP
|
||||
IF type = List THEN
|
||||
Out.Int (n, 12);
|
||||
IF count MOD 6 = 0 THEN Out.Ln END
|
||||
END;
|
||||
IF n = 1 THEN EXIT END;
|
||||
IF ODD (n) THEN
|
||||
IF n < maxCard THEN
|
||||
n := 3 * n + 1;
|
||||
IF n > max THEN max := n END
|
||||
ELSE
|
||||
Out.String ("Exceeding max value for type INTEGER at: ");
|
||||
Out.String (" n = "); Out.Int (start, 12);
|
||||
Out.String (" , count = "); Out.Int (count, 12);
|
||||
Out.String (" and intermediate value ");
|
||||
Out.Int (n, 1);
|
||||
Out.String (". Aborting.");
|
||||
Out.Ln;
|
||||
HALT (2)
|
||||
END
|
||||
ELSE
|
||||
n := n DIV 2
|
||||
END;
|
||||
INC (count)
|
||||
END;
|
||||
IF type = Max THEN RETURN max ELSE RETURN count END
|
||||
END HailStone;
|
||||
|
||||
|
||||
PROCEDURE FindMax (num : INTEGER);
|
||||
|
||||
VAR val, maxCount, maxVal, cnt : INTEGER;
|
||||
|
||||
BEGIN
|
||||
maxCount := 0;
|
||||
maxVal := 0;
|
||||
FOR val := 2 TO num DO
|
||||
cnt := HailStone (val, Count);
|
||||
IF cnt > maxCount THEN
|
||||
maxVal := val;
|
||||
maxCount := cnt
|
||||
END
|
||||
END;
|
||||
Out.String ("Longest sequence below "); Out.Int (num, 1);
|
||||
Out.String (" is "); Out.Int (HailStone (maxVal, Count), 1);
|
||||
Out.String (" for n = "); Out.Int (maxVal, 1);
|
||||
Out.String (" with an intermediate maximum of ");
|
||||
Out.Int (HailStone (maxVal, Max), 1);
|
||||
Out.Ln
|
||||
END FindMax;
|
||||
|
||||
BEGIN
|
||||
a := HailStone (27, List);
|
||||
Out.Ln;
|
||||
Out.String ("Iterations total = "); Out.Int (HailStone (27, Count), 12);
|
||||
Out.String (" max value = "); Out.Int (HailStone (27, Max) , 12);
|
||||
Out.Ln;
|
||||
FindMax (1000000);
|
||||
Out.String ("Done.");
|
||||
Out.Ln
|
||||
END hailst.
|
||||
17
Task/Hailstone-sequence/Order/hailstone-sequence-1.order
Normal file
17
Task/Hailstone-sequence/Order/hailstone-sequence-1.order
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include <order/interpreter.h>
|
||||
|
||||
#define ORDER_PP_DEF_8hailstone ORDER_PP_FN( \
|
||||
8fn(8N, \
|
||||
8cond((8equal(8N, 1), 8seq(1)) \
|
||||
(8is_0(8remainder(8N, 2)), \
|
||||
8seq_push_front(8N, 8hailstone(8quotient(8N, 2)))) \
|
||||
(8else, \
|
||||
8seq_push_front(8N, 8hailstone(8inc(8times(8N, 3))))))) )
|
||||
|
||||
ORDER_PP(
|
||||
8lets((8H, 8seq_map(8to_lit, 8hailstone(27)))
|
||||
(8S, 8seq_size(8H)),
|
||||
8print(8(h(27) - length:) 8to_lit(8S) 8comma 8space
|
||||
8(starts with:) 8seq_take(4, 8H) 8comma 8space
|
||||
8(ends with:) 8seq_drop(8minus(8S, 4), 8H))
|
||||
) )
|
||||
1
Task/Hailstone-sequence/Order/hailstone-sequence-2.order
Normal file
1
Task/Hailstone-sequence/Order/hailstone-sequence-2.order
Normal file
|
|
@ -0,0 +1 @@
|
|||
h(27) - length:112, starts with:(27)(82)(41)(124), ends with:(8)(4)(2)(1)
|
||||
13
Task/Hailstone-sequence/Order/hailstone-sequence-3.order
Normal file
13
Task/Hailstone-sequence/Order/hailstone-sequence-3.order
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#define ORDER_PP_DEF_8h_longest ORDER_PP_FN( \
|
||||
8fn(8M, 8P, \
|
||||
8if(8is_0(8M), \
|
||||
8P, \
|
||||
8let((8L, 8seq_size(8hailstone(8M))), \
|
||||
8h_longest(8dec(8M), \
|
||||
8if(8greater(8L, 8tuple_at_1(8P)), \
|
||||
8pair(8M, 8L), 8P))))) )
|
||||
|
||||
ORDER_PP(
|
||||
8let((8P, 8h_longest(8nat(1,0,0,0,0,0), 8pair(0, 0))),
|
||||
8pair(8to_lit(8tuple_at_0(8P)), 8to_lit(8tuple_at_1(8P))))
|
||||
)
|
||||
9
Task/Hailstone-sequence/Order/hailstone-sequence-4.order
Normal file
9
Task/Hailstone-sequence/Order/hailstone-sequence-4.order
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
ORDER_PP(
|
||||
8let((8P,
|
||||
8seq_head(
|
||||
8seq_sort(8fn(8P, 8Q, 8greater(8tuple_at_1(8P),
|
||||
8tuple_at_1(8Q))),
|
||||
8seq_map(8fn(8N,
|
||||
8pair(8N, 8seq_size(8hailstone(8N)))),
|
||||
8seq_iota(1, 8nat(1,0,0,0,0,0)))))),
|
||||
8pair(8to_lit(8tuple_at_0(8P)), 8to_lit(8tuple_at_1(8P)))) )
|
||||
37
Task/Hailstone-sequence/OxygenBasic/hailstone-sequence.oxy
Normal file
37
Task/Hailstone-sequence/OxygenBasic/hailstone-sequence.oxy
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
function Hailstone(sys *n)
|
||||
'=========================
|
||||
if n and 1
|
||||
n=n*3+1
|
||||
else
|
||||
n=n>>1
|
||||
end if
|
||||
end function
|
||||
|
||||
function HailstoneSequence(sys n) as sys
|
||||
'=======================================
|
||||
count=1
|
||||
do
|
||||
Hailstone n
|
||||
Count++
|
||||
if n=1 then exit do
|
||||
end do
|
||||
return count
|
||||
end function
|
||||
|
||||
'MAIN
|
||||
'====
|
||||
|
||||
maxc=0
|
||||
maxn=0
|
||||
e=100000
|
||||
for n=1 to e
|
||||
c=HailstoneSequence n
|
||||
if c>maxc
|
||||
maxc=c
|
||||
maxn=n
|
||||
end if
|
||||
next
|
||||
|
||||
print e ", " maxn ", " maxc
|
||||
|
||||
'result 100000, 77031, 351
|
||||
24
Task/Hailstone-sequence/Oz/hailstone-sequence.oz
Normal file
24
Task/Hailstone-sequence/Oz/hailstone-sequence.oz
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
declare
|
||||
fun {HailstoneSeq N}
|
||||
N > 0 = true %% assert
|
||||
if N == 1 then [1]
|
||||
elseif {IsEven N} then N|{HailstoneSeq N div 2}
|
||||
else N|{HailstoneSeq 3*N+1}
|
||||
end
|
||||
end
|
||||
|
||||
HSeq27 = {HailstoneSeq 27}
|
||||
{Length HSeq27} = 112
|
||||
{List.take HSeq27 4} = [27 82 41 124]
|
||||
{List.drop HSeq27 108} = [8 4 2 1]
|
||||
|
||||
fun {MaxBy2nd A=A1#A2 B=B1#B2}
|
||||
if B2 > A2 then B else A end
|
||||
end
|
||||
|
||||
Pairs = {Map {List.number 1 99999 1}
|
||||
fun {$ I} I#{Length {HailstoneSeq I}} end}
|
||||
|
||||
MaxI#MaxLen = {List.foldL Pairs MaxBy2nd 0#0}
|
||||
{System.showInfo
|
||||
"Maximum length "#MaxLen#" was found for hailstone("#MaxI#")"}
|
||||
31
Task/Hailstone-sequence/PARI-GP/hailstone-sequence.pari
Normal file
31
Task/Hailstone-sequence/PARI-GP/hailstone-sequence.pari
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
show(n)={
|
||||
my(t=1);
|
||||
while(n>1,
|
||||
print1(n",");
|
||||
n=if(n%2,
|
||||
3*n+1
|
||||
,
|
||||
n/2
|
||||
);
|
||||
t++
|
||||
);
|
||||
print(1);
|
||||
t
|
||||
};
|
||||
|
||||
len(n)={
|
||||
my(t=1);
|
||||
while(n>1,
|
||||
if(n%2,
|
||||
t+=2;
|
||||
n+=(n>>1)+1
|
||||
,
|
||||
t++;
|
||||
n>>=1
|
||||
)
|
||||
);
|
||||
t
|
||||
};
|
||||
|
||||
show(27)
|
||||
r=0;for(n=1,1e5,t=len(n);if(t>r,r=t;ra=n));print(ra"\t"r)
|
||||
38
Task/Hailstone-sequence/PL-I/hailstone-sequence.pli
Normal file
38
Task/Hailstone-sequence/PL-I/hailstone-sequence.pli
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
test: proc options (main);
|
||||
declare (longest, n) fixed (15);
|
||||
declare flag bit (1);
|
||||
declare (i, value) fixed (15);
|
||||
|
||||
/* Task 1: */
|
||||
flag = '1'b;
|
||||
put skip list ('The sequence for 27 is');
|
||||
i = hailstones(27);
|
||||
|
||||
/* Task 2: */
|
||||
flag = '0'b;
|
||||
longest = 0;
|
||||
do i = 1 to 99999;
|
||||
if longest < hailstones(i) then
|
||||
do; longest = hailstones(i); value = i; end;
|
||||
end;
|
||||
put skip edit (value, ' has the longest sequence of ', longest) (a);
|
||||
|
||||
hailstones: procedure (n) returns ( fixed (15));
|
||||
declare n fixed (15) nonassignable;
|
||||
declare (m, p) fixed (15);
|
||||
|
||||
m = n;
|
||||
p = 1;
|
||||
if flag then put skip list (m);
|
||||
do p = 1 by 1 while (m > 1);
|
||||
if iand(m, 1) = 0 then
|
||||
m = m/2;
|
||||
else
|
||||
m = 3*m + 1;
|
||||
if flag then put skip list (m);
|
||||
end;
|
||||
if flag then put skip list ('The hailstone sequence has length' || p);
|
||||
return (p);
|
||||
end hailstones;
|
||||
|
||||
end test;
|
||||
8
Task/Hailstone-sequence/Perl-6/hailstone-sequence.pl6
Normal file
8
Task/Hailstone-sequence/Perl-6/hailstone-sequence.pl6
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
sub hailstone($n) { $n, { $_ %% 2 ?? $_ div 2 !! $_ * 3 + 1 } ... 1 }
|
||||
|
||||
my @h = hailstone(27);
|
||||
say "Length of hailstone(27) = {+@h}";
|
||||
say ~@h;
|
||||
|
||||
my $m max= +hailstone($_) => $_ for 1..99_999;
|
||||
say "Max length $m.key() was found for hailstone($m.value()) for numbers < 100_000";
|
||||
39
Task/Hailstone-sequence/Pike/hailstone-sequence.pike
Normal file
39
Task/Hailstone-sequence/Pike/hailstone-sequence.pike
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#!/usr/bin/env pike
|
||||
|
||||
int next(int n)
|
||||
{
|
||||
if (n==1)
|
||||
return 0;
|
||||
if (n%2)
|
||||
return 3*n+1;
|
||||
else
|
||||
return n/2;
|
||||
}
|
||||
|
||||
array(int) hailstone(int n)
|
||||
{
|
||||
array seq = ({ n });
|
||||
while (n=next(n))
|
||||
seq += ({ n });
|
||||
return seq;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
array(int) two = hailstone(27);
|
||||
if (equal(two[0..3], ({ 27, 82, 41, 124 })) && equal(two[<3..], ({ 8,4,2,1 })))
|
||||
write("sizeof(({ %{%d, %}, ... %{%d, %} }) == %d\n", two[0..3], two[<3..], sizeof(two));
|
||||
|
||||
mapping longest = ([ "length":0, "start":0 ]);
|
||||
|
||||
foreach(allocate(100000); int start; )
|
||||
{
|
||||
int length = sizeof(hailstone(start));
|
||||
if (length > longest->length)
|
||||
{
|
||||
longest->length = length;
|
||||
longest->start = start;
|
||||
}
|
||||
}
|
||||
write("longest sequence starting at %d has %d elements\n", longest->start, longest->length);
|
||||
}
|
||||
29
Task/Hailstone-sequence/Pure/hailstone-sequence.pure
Normal file
29
Task/Hailstone-sequence/Pure/hailstone-sequence.pure
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// 1. Create a routine to generate the hailstone sequence for a number.
|
||||
type odd x::int = x mod 2;
|
||||
type even x::int = ~odd x;
|
||||
odd x = typep odd x;
|
||||
even x = typep even x;
|
||||
|
||||
hailstone 1 = [1];
|
||||
hailstone n::even = n:hailstone (n div 2);
|
||||
hailstone n::odd = n:hailstone (3*n + 1);
|
||||
|
||||
// 2. Use the routine to show that the hailstone sequence for the number 27
|
||||
// has 112 elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1
|
||||
n = 27;
|
||||
hs = hailstone n;
|
||||
l = # hs;
|
||||
using system;
|
||||
|
||||
printf
|
||||
("the hailstone sequence for the number %d has %d elements " +
|
||||
"starting with %s and ending with %s\n")
|
||||
(n, l, __str__ (hs!!(0..3)), __str__ ( hs!!((l-4)..l)));
|
||||
|
||||
// 3. Show the number less than 100,000 which has the longest hailstone
|
||||
// sequence together with that sequences length.
|
||||
printf ("the number under 100,000 with the longest sequence is %d " +
|
||||
"with a sequence length of %d\n")
|
||||
(foldr (\ (a,b) (c,d) -> if (b > d) then (a,b) else (c,d))
|
||||
(0,0)
|
||||
(map (\ x -> (x, # hailstone x)) (1..100000)));
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
NewList Hailstones.i() ; Make a linked list to use as we do not know the numbers of elements needed for an Array
|
||||
|
||||
Procedure.i FillHailstones(n) ; Fills the list & returns the amount of elements in the list
|
||||
Shared Hailstones() ; Get access to the Hailstones-List
|
||||
ClearList(Hailstones()) ; Remove old data
|
||||
Repeat
|
||||
AddElement(Hailstones()) ; Add an element to the list
|
||||
Hailstones()=n ; Fill current value in the new list element
|
||||
If n=1
|
||||
ProcedureReturn ListSize(Hailstones())
|
||||
ElseIf n%2=0
|
||||
n/2
|
||||
Else
|
||||
n=(3*n)+1
|
||||
EndIf
|
||||
ForEver
|
||||
EndProcedure
|
||||
|
||||
If OpenConsole()
|
||||
Define i, l, maxl, maxi
|
||||
l=FillHailstones(27)
|
||||
Print("#27 has "+Str(l)+" elements and the sequence is: "+#CRLF$)
|
||||
ForEach Hailstones()
|
||||
If i=6
|
||||
Print(#CRLF$)
|
||||
i=0
|
||||
EndIf
|
||||
i+1
|
||||
Print(RSet(Str(Hailstones()),5))
|
||||
If Hailstones()<>1
|
||||
Print(", ")
|
||||
EndIf
|
||||
Next
|
||||
|
||||
i=1
|
||||
Repeat
|
||||
l=FillHailstones(i)
|
||||
If l>maxl
|
||||
maxl=l
|
||||
maxi=i
|
||||
EndIf
|
||||
i+1
|
||||
Until i>=100000
|
||||
Print(#CRLF$+#CRLF$+"The longest sequence below 100000 is #"+Str(maxi)+", and it has "+Str(maxl)+" elements.")
|
||||
|
||||
Print(#CRLF$+#CRLF$+"Press ENTER to exit."): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
40
Task/Hailstone-sequence/Run-BASIC/hailstone-sequence.run
Normal file
40
Task/Hailstone-sequence/Run-BASIC/hailstone-sequence.run
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
print "Part 1: Create a routine to generate the hailstone sequence for a number."
|
||||
print ""
|
||||
|
||||
while hailstone < 1 or hailstone <> int(hailstone)
|
||||
input "Please enter a positive integer: "; hailstone
|
||||
wend
|
||||
count = doHailstone(hailstone,"Y")
|
||||
|
||||
print: print "Part 2: Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1."
|
||||
count = doHailstone(27,"Y")
|
||||
|
||||
print: print "Part 3: Show the number less than 100,000 which has the longest hailstone sequence together with that sequence's length.(But don't show the actual sequence)!"
|
||||
print "Calculating result... Please wait... This could take a little while..."
|
||||
print "Stone Percent Count"
|
||||
for i = 1 to 99999
|
||||
count = doHailstone(i,"N")
|
||||
if count > maxCount then
|
||||
theBigStone = i
|
||||
maxCount = count
|
||||
print using("#####",i);" ";using("###.#", i / 99999 * 100);"% ";using("####",count)
|
||||
end if
|
||||
next i
|
||||
end
|
||||
|
||||
'---------------------------------------------
|
||||
' pass number and print (Y/N)
|
||||
FUNCTION doHailstone(hailstone,prnt$)
|
||||
if prnt$ = "Y" then
|
||||
print
|
||||
print "The following is the 'Hailstone Sequence' for number:";hailstone
|
||||
end if
|
||||
while hailstone <> 1
|
||||
if (hailstone and 1) then hailstone = (hailstone * 3) + 1 else hailstone = hailstone / 2
|
||||
doHailstone = doHailstone + 1
|
||||
if prnt$ = "Y" then
|
||||
print hailstone;chr$(9);
|
||||
if (doHailstone mod 10) = 0 then print
|
||||
end if
|
||||
wend
|
||||
END FUNCTION
|
||||
58
Task/Hailstone-sequence/Seed7/hailstone-sequence.seed7
Normal file
58
Task/Hailstone-sequence/Seed7/hailstone-sequence.seed7
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const func array integer: hailstone (in var integer: n) is func
|
||||
result
|
||||
var array integer: hSequence is 0 times 0;
|
||||
begin
|
||||
while n <> 1 do
|
||||
hSequence &:= n;
|
||||
if odd(n) then
|
||||
n := 3 * n + 1;
|
||||
else
|
||||
n := n div 2;
|
||||
end if;
|
||||
end while;
|
||||
hSequence &:= n;
|
||||
end func;
|
||||
|
||||
const func integer: hailstoneSequenceLength (in var integer: n) is func
|
||||
result
|
||||
var integer: sequenceLength is 1;
|
||||
begin
|
||||
while n <> 1 do
|
||||
incr(sequenceLength);
|
||||
if odd(n) then
|
||||
n := 3 * n + 1;
|
||||
else
|
||||
n := n div 2;
|
||||
end if;
|
||||
end while;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var integer: number is 0;
|
||||
var integer: length is 0;
|
||||
var integer: maxLength is 0;
|
||||
var integer: numberOfMaxLength is 0;
|
||||
var array integer: h27 is 0 times 0;
|
||||
begin
|
||||
for number range 1 to 99999 do
|
||||
length := hailstoneSequenceLength(number);
|
||||
if length > maxLength then
|
||||
maxLength := length;
|
||||
numberOfMaxLength := number;
|
||||
end if;
|
||||
end for;
|
||||
h27 := hailstone(27);
|
||||
writeln("hailstone(27):");
|
||||
for number range 1 to 4 do
|
||||
write(h27[number] <& ", ");
|
||||
end for;
|
||||
write("....");
|
||||
for number range length(h27) -3 to length(h27) do
|
||||
write(", " <& h27[number]);
|
||||
end for;
|
||||
writeln(" length=" <& length(h27));
|
||||
writeln("Maximum length " <& maxLength <& " at number=" <& numberOfMaxLength);
|
||||
end func;
|
||||
26
Task/Hailstone-sequence/TXR/hailstone-sequence.txr
Normal file
26
Task/Hailstone-sequence/TXR/hailstone-sequence.txr
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
@(do (defun hailstone (n)
|
||||
(cons n
|
||||
(gen (not (eq n 1))
|
||||
(set n (if (evenp n)
|
||||
(trunc n 2)
|
||||
(+ (* 3 n) 1)))))))
|
||||
@(next :list @(mapcar* (fun tostring) (hailstone 27)))
|
||||
27
|
||||
82
|
||||
41
|
||||
124
|
||||
@(skip)
|
||||
8
|
||||
4
|
||||
2
|
||||
1
|
||||
@(eof)
|
||||
@(do (let ((max 0) maxi)
|
||||
(each* ((i (range 1 99999))
|
||||
(h (mapcar* (fun hailstone) i))
|
||||
(len (mapcar* (fun length) h)))
|
||||
(if (> len max)
|
||||
(progn
|
||||
(set max len)
|
||||
(set maxi i))))
|
||||
(format t "longest sequence is ~a for n = ~a\n" max maxi)))
|
||||
37
Task/Hailstone-sequence/UNIX-Shell/hailstone-sequence-1.sh
Normal file
37
Task/Hailstone-sequence/UNIX-Shell/hailstone-sequence-1.sh
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#!/bin/bash
|
||||
# seq is the array genereated by hailstone
|
||||
# index is used for seq
|
||||
declare -a seq
|
||||
declare -i index
|
||||
|
||||
# Create a routine to generate the hailstone sequence for a number
|
||||
hailstone () {
|
||||
unset seq index
|
||||
seq[$((index++))]=$((n=$1))
|
||||
while [ $n -ne 1 ]; do
|
||||
[ $((n % 2)) -eq 1 ] && ((n=n*3+1)) || ((n=n/2))
|
||||
seq[$((index++))]=$n
|
||||
done
|
||||
}
|
||||
|
||||
# Use the routine to show that the hailstone sequence for the number 27
|
||||
# has 112 elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1
|
||||
i=27
|
||||
hailstone $i
|
||||
echo "$i: ${#seq[@]}"
|
||||
echo "${seq[@]:0:4} ... ${seq[@]:(-4):4}"
|
||||
|
||||
# Show the number less than 100,000 which has the longest hailstone
|
||||
# sequence together with that sequences length.
|
||||
# (But don't show the actual sequence)!
|
||||
max=0
|
||||
maxlen=0
|
||||
for ((i=1;i<100000;i++)); do
|
||||
hailstone $i
|
||||
if [ $((len=${#seq[@]})) -gt $maxlen ]; then
|
||||
max=$i
|
||||
maxlen=$len
|
||||
fi
|
||||
done
|
||||
|
||||
echo "${max} has a hailstone sequence length of ${maxlen}"
|
||||
30
Task/Hailstone-sequence/UNIX-Shell/hailstone-sequence-2.sh
Normal file
30
Task/Hailstone-sequence/UNIX-Shell/hailstone-sequence-2.sh
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# Outputs a hailstone sequence from $1, with one element per line.
|
||||
# Clobbers $n.
|
||||
hailstone() {
|
||||
n=`expr "$1" + 0`
|
||||
eval "test $? -lt 2 || return $?" # $n must be integer.
|
||||
|
||||
echo $n
|
||||
while test $n -ne 1; do
|
||||
if expr $n % 2 >/dev/null; then
|
||||
n=`expr 3 \* $n + 1`
|
||||
else
|
||||
n=`expr $n / 2`
|
||||
fi
|
||||
echo $n
|
||||
done
|
||||
}
|
||||
|
||||
set -- `hailstone 27`
|
||||
echo "Hailstone sequence from 27 has $# elements:"
|
||||
first="$1, $2, $3, $4"
|
||||
shift `expr $# - 4`
|
||||
echo " $first, ..., $1, $2, $3, $4"
|
||||
|
||||
i=1 max=0 maxlen=0
|
||||
while test $i -lt 1000; do
|
||||
len=`hailstone $i | wc -l | tr -d ' '`
|
||||
test $len -gt $maxlen && max=$i maxlen=$len
|
||||
i=`expr $i + 1`
|
||||
done
|
||||
echo "Hailstone sequence from $max has $maxlen elements."
|
||||
51
Task/Hailstone-sequence/UNIX-Shell/hailstone-sequence-3.sh
Normal file
51
Task/Hailstone-sequence/UNIX-Shell/hailstone-sequence-3.sh
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
# Outputs a hailstone sequence from !:1, with one element per line.
|
||||
# Clobbers $n.
|
||||
alias hailstone eval \''@ n = \!:1:q \\
|
||||
echo $n \\
|
||||
while ( $n != 1 ) \\
|
||||
if ( $n % 2 ) then \\
|
||||
@ n = 3 * $n + 1 \\
|
||||
else \\
|
||||
@ n /= 2 \\
|
||||
endif \\
|
||||
echo $n \\
|
||||
end \\
|
||||
'\'
|
||||
|
||||
set sequence=(`hailstone 27`)
|
||||
echo "Hailstone sequence from 27 has $#sequence elements:"
|
||||
@ i = $#sequence - 3
|
||||
echo " $sequence[1-4] ... $sequence[$i-]"
|
||||
|
||||
# hailstone-length $i
|
||||
# acts like
|
||||
# @ len = `hailstone $i | wc -l | tr -d ' '`
|
||||
# but without forking any subshells.
|
||||
alias hailstone-length eval \''@ n = \!:1:q \\
|
||||
@ len = 1 \\
|
||||
while ( $n != 1 ) \\
|
||||
if ( $n % 2 ) then \\
|
||||
@ n = 3 * $n + 1 \\
|
||||
else \\
|
||||
@ n /= 2 \\
|
||||
endif \\
|
||||
@ len += 1 \\
|
||||
end \\
|
||||
'\'
|
||||
|
||||
@ i = 1
|
||||
@ max = 0
|
||||
@ maxlen = 0
|
||||
while ($i < 100000)
|
||||
# XXX - I must run hailstone-length in a subshell, because my
|
||||
# C Shell has a bug when it runs hailstone-length inside this
|
||||
# while ($i < 1000) loop: it forgets about this loop, and
|
||||
# reports an error <<end: Not in while/foreach.>>
|
||||
@ len = `hailstone-length $i; echo $len`
|
||||
if ($len > $maxlen) then
|
||||
@ max = $i
|
||||
@ maxlen = $len
|
||||
endif
|
||||
@ i += 1
|
||||
end
|
||||
echo "Hailstone sequence from $max has $maxlen elements."
|
||||
12
Task/Hailstone-sequence/Ursala/hailstone-sequence.ursala
Normal file
12
Task/Hailstone-sequence/Ursala/hailstone-sequence.ursala
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#import std
|
||||
#import nat
|
||||
|
||||
hail = @iNC ~&h~=1->x ^C\~& @h ~&h?\~&t successor+ sum@iNiCX
|
||||
|
||||
#show+
|
||||
|
||||
main =
|
||||
|
||||
<
|
||||
^T(@ixX take/$4; %nLP~~lrxPX; ^|TL/~& :/'...',' has length '--@h+ %nP+ length) hail 27,
|
||||
^|TL(~&,:/' has sequence length ') %nP~~ nleq$^&r ^(~&,length+ hail)* nrange/1 100000>
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
Module HailstoneSequence
|
||||
Sub Main()
|
||||
' Checking sequence of 27.
|
||||
|
||||
Dim l As List(Of Long) = HailstoneSequence(27)
|
||||
Console.WriteLine("27 has {0} elements in sequence:", l.Count())
|
||||
|
||||
For i As Integer = 0 To 3 : Console.Write("{0}, ", l(i)) : Next
|
||||
Console.Write("... ")
|
||||
For i As Integer = l.Count - 4 To l.Count - 1 : Console.Write(", {0}", l(i)) : Next
|
||||
|
||||
Console.WriteLine()
|
||||
|
||||
' Finding longest sequence for numbers below 100000.
|
||||
|
||||
Dim max As Integer = 0
|
||||
Dim maxCount As Integer = 0
|
||||
|
||||
For i = 1 To 99999
|
||||
l = HailstoneSequence(i)
|
||||
If l.Count > maxCount Then
|
||||
max = i
|
||||
maxCount = l.Count
|
||||
End If
|
||||
Next
|
||||
Console.WriteLine("Max elements in sequence for number below 100k: {0} with {1} elements.", max, maxCount)
|
||||
Console.ReadLine()
|
||||
End Sub
|
||||
|
||||
Private Function HailstoneSequence(ByVal n As Long) As List(Of Long)
|
||||
Dim valList As New List(Of Long)()
|
||||
valList.Add(n)
|
||||
|
||||
Do Until n = 1
|
||||
n = IIf(n Mod 2 = 0, n / 2, (3 * n) + 1)
|
||||
valList.Add(n)
|
||||
Loop
|
||||
|
||||
Return valList
|
||||
End Function
|
||||
|
||||
End Module
|
||||
30
Task/Hailstone-sequence/XPL0/hailstone-sequence.xpl0
Normal file
30
Task/Hailstone-sequence/XPL0/hailstone-sequence.xpl0
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
include c:\cxpl\codes; \intrinsic 'code' declarations
|
||||
int Seq(1000); \more than enough for longest sequence
|
||||
|
||||
func Hailstone(N); \Return length of Hailstone sequence starting at N
|
||||
int N; \ also fills Seq array with sequence
|
||||
int I;
|
||||
[I:= 0;
|
||||
loop [Seq(I):= N; I:= I+1;
|
||||
if N=1 then return I;
|
||||
N:= if N&1 then N*3+1 else N/2;
|
||||
];
|
||||
];
|
||||
|
||||
int N, SN, Len, MaxLen;
|
||||
[Len:= Hailstone(27);
|
||||
Text(0, "27's Hailstone length = "); IntOut(0, Len); CrLf(0);
|
||||
|
||||
Text(0, "Sequence = ");
|
||||
for N:= 0 to 3 do [IntOut(0, Seq(N)); ChOut(0, ^ )];
|
||||
Text(0, "... ");
|
||||
for N:= Len-4 to Len-1 do [IntOut(0, Seq(N)); ChOut(0, ^ )];
|
||||
CrLf(0);
|
||||
|
||||
MaxLen:= 0;
|
||||
for N:= 1 to 100_000-1 do
|
||||
[Len:= Hailstone(N);
|
||||
if Len > MaxLen then [MaxLen:= Len; SN:= N]; \save N with max length
|
||||
];
|
||||
IntOut(0, SN); Text(0, "'s Hailstone length = "); IntOut(0, MaxLen);
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue