A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
14
Task/Hailstone-sequence/0DESCRIPTION
Normal file
14
Task/Hailstone-sequence/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
The Hailstone sequence of numbers can be generated from a starting positive integer, n by:
|
||||
* If n is 1 then the sequence ends.
|
||||
* If n is even then the next n of the sequence <code>= n/2</code>
|
||||
* If n is odd then the next n of the sequence <code>= (3 * n) + 1</code>
|
||||
|
||||
The (unproven), [[wp:Collatz conjecture|Collatz conjecture]] is that the hailstone sequence for any starting number always terminates.
|
||||
|
||||
'''Task Description:'''
|
||||
# Create a routine to generate the hailstone sequence for a number.
|
||||
# Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with <code>27, 82, 41, 124</code> and ending with <code>8, 4, 2, 1</code>
|
||||
# Show the number less than 100,000 which has the longest hailstone sequence together with that sequence's length.<br> (But don't show the actual sequence!)
|
||||
|
||||
'''See Also:'''<br>
|
||||
* [http://xkcd.com/710 xkcd] (humourous).
|
||||
16
Task/Hailstone-sequence/ACL2/hailstone-sequence.acl2
Normal file
16
Task/Hailstone-sequence/ACL2/hailstone-sequence.acl2
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
(defun hailstone (len)
|
||||
(loop for x = len
|
||||
then (if (evenp x)
|
||||
(/ x 2)
|
||||
(+ 1 (* 3 x)))
|
||||
collect x until (= x 1)))
|
||||
|
||||
;; Must be tail recursive
|
||||
(defun max-hailstone-start (limit mx curr)
|
||||
(declare (xargs :mode :program))
|
||||
(if (zp limit)
|
||||
(mv mx curr)
|
||||
(let ((new-mx (len (hailstone limit))))
|
||||
(if (> new-mx mx)
|
||||
(max-hailstone-start (1- limit) new-mx limit)
|
||||
(max-hailstone-start (1- limit) mx curr)))))
|
||||
47
Task/Hailstone-sequence/ALGOL-68/hailstone-sequence.alg
Normal file
47
Task/Hailstone-sequence/ALGOL-68/hailstone-sequence.alg
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
MODE LINT = # LONG ... # INT;
|
||||
|
||||
PROC hailstone = (INT in n, REF[]LINT array)INT:
|
||||
(
|
||||
INT hs := 1;
|
||||
INT index := 0;
|
||||
LINT n := in n;
|
||||
|
||||
WHILE n /= 1 DO
|
||||
hs +:= 1;
|
||||
IF array ISNT REF[]LINT(NIL) THEN array[index +:= 1] := n FI;
|
||||
n := IF ODD n THEN 3*n+1 ELSE n OVER 2 FI
|
||||
OD;
|
||||
IF array ISNT REF[]LINT(NIL) THEN array[index +:= 1] := n FI;
|
||||
hs
|
||||
);
|
||||
|
||||
main:
|
||||
(
|
||||
INT j, hmax := 0;
|
||||
INT jatmax, n;
|
||||
INT border = 4;
|
||||
|
||||
FOR j TO 100000-1 DO
|
||||
n := hailstone(j, NIL);
|
||||
IF hmax < n THEN
|
||||
hmax := n;
|
||||
jatmax := j
|
||||
FI
|
||||
OD;
|
||||
|
||||
[2]INT test := (27, jatmax);
|
||||
FOR key TO UPB test DO
|
||||
INT val = test[key];
|
||||
n := hailstone(val, NIL);
|
||||
[n]LINT array;
|
||||
n := hailstone(val, array);
|
||||
|
||||
printf(($"[ "n(border)(g(0)", ")" ..."n(border)(", "g(0))"] len="g(0)l$,
|
||||
array[:border], array[n-border+1:], n))
|
||||
#;free(array) #
|
||||
OD;
|
||||
printf(($"Max "g(0)" at j="g(0)l$, hmax, jatmax))
|
||||
# ELLA Algol68RS:
|
||||
print(("Max",hmax," at j=",jatmax, new line))
|
||||
#
|
||||
)
|
||||
9
Task/Hailstone-sequence/APL/hailstone-sequence-1.apl
Normal file
9
Task/Hailstone-sequence/APL/hailstone-sequence-1.apl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
seq←hailstone n;next
|
||||
⍝ Returns the hailstone sequence for a given number
|
||||
|
||||
seq←n ⍝ Init the sequence
|
||||
:While n≠1
|
||||
next←(n÷2) (1+3×n) ⍝ Compute both possibilities
|
||||
n←next[1+2|n] ⍝ Pick the appropriate next step
|
||||
seq,←n ⍝ Append that to the sequence
|
||||
:EndWhile
|
||||
8
Task/Hailstone-sequence/APL/hailstone-sequence-2.apl
Normal file
8
Task/Hailstone-sequence/APL/hailstone-sequence-2.apl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
5↑hailstone 27
|
||||
27 82 41 124 62
|
||||
¯5↑hailstone 27
|
||||
16 8 4 2 1
|
||||
⍴hailstone 27
|
||||
112
|
||||
1↑{⍵[⍒↑(⍴∘hailstone)¨⍵]}⍳100000
|
||||
77031
|
||||
39
Task/Hailstone-sequence/Ada/hailstone-sequence-1.ada
Normal file
39
Task/Hailstone-sequence/Ada/hailstone-sequence-1.ada
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
procedure hailstone is
|
||||
type int_arr is array(Positive range <>) of Integer;
|
||||
type int_arr_pt is access all int_arr;
|
||||
|
||||
function hailstones(num:Integer; pt:int_arr_pt) return Integer is
|
||||
stones : Integer := 1;
|
||||
n : Integer := num;
|
||||
begin
|
||||
if pt /= null then pt(1) := num; end if;
|
||||
while (n/=1) loop
|
||||
stones := stones + 1;
|
||||
if n mod 2 = 0 then n := n/2;
|
||||
else n := (3*n)+1;
|
||||
end if;
|
||||
if pt /= null then pt(stones) := n; end if;
|
||||
end loop;
|
||||
return stones;
|
||||
end hailstones;
|
||||
|
||||
nmax,stonemax,stones : Integer := 0;
|
||||
list : int_arr_pt;
|
||||
begin
|
||||
stones := hailstones(27,null);
|
||||
list := new int_arr(1..stones);
|
||||
stones := hailstones(27,list);
|
||||
put(" 27: "&Integer'Image(stones)); new_line;
|
||||
for n in 1..4 loop put(Integer'Image(list(n))); end loop;
|
||||
put(" .... ");
|
||||
for n in stones-3..stones loop put(Integer'Image(list(n))); end loop;
|
||||
new_line;
|
||||
for n in 1..100000 loop
|
||||
stones := hailstones(n,null);
|
||||
if stones>stonemax then
|
||||
nmax := n; stonemax := stones;
|
||||
end if;
|
||||
end loop;
|
||||
put_line(Integer'Image(nmax)&" max @ n= "&Integer'Image(stonemax));
|
||||
end hailstone;
|
||||
4
Task/Hailstone-sequence/Ada/hailstone-sequence-2.ada
Normal file
4
Task/Hailstone-sequence/Ada/hailstone-sequence-2.ada
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
package Hailstones is
|
||||
type Integer_Sequence is array(Positive range <>) of Integer;
|
||||
function Create_Sequence (N : Positive) return Integer_Sequence;
|
||||
end Hailstones;
|
||||
15
Task/Hailstone-sequence/Ada/hailstone-sequence-3.ada
Normal file
15
Task/Hailstone-sequence/Ada/hailstone-sequence-3.ada
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package body Hailstones is
|
||||
function Create_Sequence (N : Positive) return Integer_Sequence is
|
||||
begin
|
||||
if N = 1 then
|
||||
-- terminate
|
||||
return (1 => N);
|
||||
elsif N mod 2 = 0 then
|
||||
-- even
|
||||
return (1 => N) & Create_Sequence (N / 2);
|
||||
else
|
||||
-- odd
|
||||
return (1 => N) & Create_Sequence (3 * N + 1);
|
||||
end if;
|
||||
end Create_Sequence;
|
||||
end Hailstones;
|
||||
43
Task/Hailstone-sequence/Ada/hailstone-sequence-4.ada
Normal file
43
Task/Hailstone-sequence/Ada/hailstone-sequence-4.ada
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
with Ada.Text_IO;
|
||||
with Hailstones;
|
||||
|
||||
procedure Main is
|
||||
package Integer_IO is new Ada.Text_IO.Integer_IO (Integer);
|
||||
|
||||
procedure Print_Sequence (X : Hailstones.Integer_Sequence) is
|
||||
begin
|
||||
for I in X'Range loop
|
||||
Integer_IO.Put (Item => X (I), Width => 0);
|
||||
if I < X'Last then
|
||||
Ada.Text_IO.Put (", ");
|
||||
end if;
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
end Print_Sequence;
|
||||
|
||||
Hailstone_27 : constant Hailstones.Integer_Sequence :=
|
||||
Hailstones.Create_Sequence (N => 27);
|
||||
|
||||
begin
|
||||
Ada.Text_IO.Put_Line ("Length of 27:" & Integer'Image (Hailstone_27'Length));
|
||||
Ada.Text_IO.Put ("First four: ");
|
||||
Print_Sequence (Hailstone_27 (Hailstone_27'First .. Hailstone_27'First + 3));
|
||||
Ada.Text_IO.Put ("Last four: ");
|
||||
Print_Sequence (Hailstone_27 (Hailstone_27'Last - 3 .. Hailstone_27'Last));
|
||||
|
||||
declare
|
||||
Longest_Length : Natural := 0;
|
||||
Longest_N : Positive;
|
||||
Length : Natural;
|
||||
begin
|
||||
for I in 1 .. 99_999 loop
|
||||
Length := Hailstones.Create_Sequence (N => I)'Length;
|
||||
if Length > Longest_Length then
|
||||
Longest_Length := Length;
|
||||
Longest_N := I;
|
||||
end if;
|
||||
end loop;
|
||||
Ada.Text_IO.Put_Line ("Longest length is" & Integer'Image (Longest_Length));
|
||||
Ada.Text_IO.Put_Line ("with N =" & Integer'Image (Longest_N));
|
||||
end;
|
||||
end Main;
|
||||
40
Task/Hailstone-sequence/AutoHotkey/hailstone-sequence.ahk
Normal file
40
Task/Hailstone-sequence/AutoHotkey/hailstone-sequence.ahk
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
; Submitted by MasterFocus --- http://tiny.cc/iTunis
|
||||
|
||||
; [1] Generate the Hailstone Seq. for a number
|
||||
|
||||
List := varNum := 7 ; starting number is 7, not counting elements
|
||||
While ( varNum > 1 )
|
||||
List .= ", " ( varNum := ( Mod(varNum,2) ? (varNum*3)+1 : varNum//2 ) )
|
||||
MsgBox % List
|
||||
|
||||
; [2] Seq. for starting number 27 has 112 elements
|
||||
|
||||
Count := 1, List := varNum := 27 ; starting number is 27, counting elements
|
||||
While ( varNum > 1 )
|
||||
Count++ , List .= ", " ( varNum := ( Mod(varNum,2) ? (varNum*3)+1 : varNum//2 ) )
|
||||
MsgBox % "Sequence:`n" List "`n`nCount: " Count
|
||||
|
||||
; [3] Find number<100000 with longest seq. and show both values
|
||||
|
||||
MaxNum := Max := 0 ; reset the Maximum variables
|
||||
TimesToLoop := 100000 ; limit number here is 100000
|
||||
Offset := 70000 ; offset - use 0 to process from 0 to 100000
|
||||
Loop, %TimesToLoop%
|
||||
{
|
||||
If ( TimesToLoop < ( varNum := Index := A_Index+Offset ) )
|
||||
Break
|
||||
text := "Processing...`n-------------------`n"
|
||||
text .= "Current starting number: " Index "`n"
|
||||
text .= "Current sequence count: " Count
|
||||
text .= "`n-------------------`n"
|
||||
text .= "Maximum starting number: " MaxNum "`n"
|
||||
text .= "Maximum sequence count: " Max " <<" ; text split to avoid long code lines
|
||||
ToolTip, %text%
|
||||
Count := 1 ; going to count the elements, but no "List" required
|
||||
While ( varNum > 1 )
|
||||
Count++ , varNum := ( Mod(varNum,2) ? (varNum*3)+1 : varNum//2 )
|
||||
If ( Count > Max )
|
||||
Max := Count , MaxNum := Index ; set the new maximum values, if necessary
|
||||
}
|
||||
ToolTip
|
||||
MsgBox % "Number: " MaxNum "`nCount: " Max
|
||||
28
Task/Hailstone-sequence/AutoIt/hailstone-sequence.autoit
Normal file
28
Task/Hailstone-sequence/AutoIt/hailstone-sequence.autoit
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
$Hail = Hailstone(27)
|
||||
ConsoleWrite("Sequence-Lenght: "&$Hail&@CRLF)
|
||||
$Big = -1
|
||||
$Sequenzlenght = -1
|
||||
For $I = 1 To 100000
|
||||
$Hail = Hailstone($i, False)
|
||||
If Number($Hail) > $Sequenzlenght Then
|
||||
$Sequenzlenght = Number($Hail)
|
||||
$Big = $i
|
||||
EndIf
|
||||
Next
|
||||
ConsoleWrite("Longest Sequence : "&$Sequenzlenght&" from number "&$Big&@CRLF)
|
||||
Func Hailstone($int, $sequence = True)
|
||||
$Counter = 0
|
||||
While True
|
||||
$Counter += 1
|
||||
If $sequence = True Then ConsoleWrite($int & ",")
|
||||
If $int = 1 Then ExitLoop
|
||||
If Not Mod($int, 2) Then
|
||||
$int = $int / 2
|
||||
Else
|
||||
$int = 3 * $int + 1
|
||||
EndIf
|
||||
If Not Mod($Counter, 25) AND $sequence = True Then ConsoleWrite(@CRLF)
|
||||
WEnd
|
||||
If $sequence = True Then ConsoleWrite(@CRLF)
|
||||
Return $Counter
|
||||
EndFunc ;==>Hailstone
|
||||
23
Task/Hailstone-sequence/BBC-BASIC/hailstone-sequence.bbc
Normal file
23
Task/Hailstone-sequence/BBC-BASIC/hailstone-sequence.bbc
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
seqlen% = FNhailstone(27, TRUE)
|
||||
PRINT '"Sequence length = "; seqlen%
|
||||
maxlen% = 0
|
||||
FOR number% = 2 TO 100000
|
||||
seqlen% = FNhailstone(number%, FALSE)
|
||||
IF seqlen% > maxlen% THEN
|
||||
maxlen% = seqlen%
|
||||
maxnum% = number%
|
||||
ENDIF
|
||||
NEXT
|
||||
PRINT "The number with the longest hailstone sequence is " ; maxnum%
|
||||
PRINT "Its sequence length is " ; maxlen%
|
||||
END
|
||||
|
||||
DEF FNhailstone(N%, S%)
|
||||
LOCAL L%
|
||||
IF S% THEN PRINT N%;
|
||||
WHILE N% <> 1
|
||||
IF N% AND 1 THEN N% = 3 * N% + 1 ELSE N% DIV= 2
|
||||
IF S% THEN PRINT N%;
|
||||
L% += 1
|
||||
ENDWHILE
|
||||
= L% + 1
|
||||
31
Task/Hailstone-sequence/Batch-File/hailstone-sequence-1.bat
Normal file
31
Task/Hailstone-sequence/Batch-File/hailstone-sequence-1.bat
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
if "%1" equ "" goto :eof
|
||||
call :hailstone %1 seq cnt
|
||||
echo %seq%
|
||||
goto :eof
|
||||
|
||||
:hailstone
|
||||
set num=%1
|
||||
set %2=%1
|
||||
|
||||
:loop
|
||||
if %num% equ 1 goto :eof
|
||||
call :iseven %num% res
|
||||
if %res% equ T goto divideby2
|
||||
set /a num = (3 * num) + 1
|
||||
set %2=!%2! %num%
|
||||
goto loop
|
||||
:divideby2
|
||||
set /a num = num / 2
|
||||
set %2=!%2! %num%
|
||||
goto loop
|
||||
|
||||
:iseven
|
||||
set /a tmp = %1 %% 2
|
||||
if %tmp% equ 1 (
|
||||
set %2=F
|
||||
) else (
|
||||
set %2=T
|
||||
)
|
||||
goto :eof
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
>hailstone.cmd 20
|
||||
20 10 5 16 8 4 2 1
|
||||
4
Task/Hailstone-sequence/Befunge/hailstone-sequence.bf
Normal file
4
Task/Hailstone-sequence/Befunge/hailstone-sequence.bf
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
&>:.:1-|
|
||||
>3*^ @
|
||||
|%2: <
|
||||
v>2/>+
|
||||
43
Task/Hailstone-sequence/Bracmat/hailstone-sequence.bracmat
Normal file
43
Task/Hailstone-sequence/Bracmat/hailstone-sequence.bracmat
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
(
|
||||
( hailstone
|
||||
= L len
|
||||
. !arg:?L
|
||||
& whl
|
||||
' ( !arg:~1
|
||||
& (!arg*1/2:~/|3*!arg+1):?arg
|
||||
& !arg !L:?L
|
||||
)
|
||||
& (!L:? [?len&!len.!L)
|
||||
)
|
||||
& ( reverse
|
||||
= L e
|
||||
. :?L
|
||||
& whl'(!arg:%?e ?arg&!e !L:?L)
|
||||
& !L
|
||||
)
|
||||
& hailstone$27:(?len.?list)
|
||||
& reverse$!list:?first4 [4 ? [-5 ?last4
|
||||
& put$"Hailstone sequence starting with "
|
||||
& put$!first4
|
||||
& put$(str$(" has " !len " elements and ends with "))
|
||||
& put$(!last4 \n)
|
||||
& 1:?N
|
||||
& 0:?max:?Nmax
|
||||
& whl
|
||||
' ( !N+1:<100000:?N
|
||||
& hailstone$!N
|
||||
: ( >!max:?max&!N:?Nmax
|
||||
| ?
|
||||
. ?
|
||||
)
|
||||
)
|
||||
& out
|
||||
$ ( str
|
||||
$ ( "The number <100000 with the longest hailstone sequence is "
|
||||
!Nmax
|
||||
" with "
|
||||
!max
|
||||
" elements."
|
||||
)
|
||||
)
|
||||
);
|
||||
12
Task/Hailstone-sequence/Brainf---/hailstone-sequence-1.bf
Normal file
12
Task/Hailstone-sequence/Brainf---/hailstone-sequence-1.bf
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
>,[
|
||||
[
|
||||
----------[
|
||||
>>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<]
|
||||
++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],<
|
||||
]>
|
||||
]>>>++>+>>[
|
||||
<<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>]
|
||||
>[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]]
|
||||
>[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>>
|
||||
]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<,
|
||||
]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
27
|
||||
111
|
||||
33
Task/Hailstone-sequence/Brat/hailstone-sequence.brat
Normal file
33
Task/Hailstone-sequence/Brat/hailstone-sequence.brat
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
hailstone = { num |
|
||||
sequence = [num]
|
||||
while { num != 1 }
|
||||
{ true? num % 2 == 0
|
||||
{ num = num / 2 }
|
||||
{ num = num * 3 + 1 }
|
||||
sequence << num
|
||||
}
|
||||
|
||||
sequence
|
||||
}
|
||||
|
||||
#Check sequence for 27
|
||||
seq = hailstone 27
|
||||
true? (seq[0,3] == [27 82 41 124] && seq[-1, -4] == [8 4 2 1])
|
||||
{ p "Sequence for 27 is correct" }
|
||||
{ p "Sequence for 27 is not correct!" }
|
||||
|
||||
#Find longest sequence for numbers < 100,000
|
||||
longest = [number: 0 length: 0]
|
||||
|
||||
1.to 99999 { index |
|
||||
seq = hailstone index
|
||||
true? seq.length > longest[:length]
|
||||
{ longest[:length] = seq.length
|
||||
longest[:number] = index
|
||||
p "Longest so far: #{index} @ #{longest[:length]} elements"
|
||||
}
|
||||
|
||||
index = index + 1
|
||||
}
|
||||
|
||||
p "Longest was starting from #{longest[:number]} and was of length #{longest[:length]}"
|
||||
2
Task/Hailstone-sequence/Burlesque/hailstone-sequence.blq
Normal file
2
Task/Hailstone-sequence/Burlesque/hailstone-sequence.blq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
blsq ) 27{^^^^2.%{3.*1.+}\/{2./}\/ie}{1!=}w!bx{\/+]}{\/isn!}w!L[
|
||||
112
|
||||
50
Task/Hailstone-sequence/C++/hailstone-sequence.cpp
Normal file
50
Task/Hailstone-sequence/C++/hailstone-sequence.cpp
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
|
||||
std::vector<int> hailstone(int i)
|
||||
{
|
||||
std::vector<int> v;
|
||||
while(true){
|
||||
v.push_back(i);
|
||||
if (1 == i) break;
|
||||
i = (i % 2) ? (3 * i + 1) : (i / 2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
std::pair<int,int> find_longest_hailstone_seq(int n)
|
||||
{
|
||||
std::pair<int, int> maxseq(0, 0);
|
||||
int l;
|
||||
for(int i = 1; i < n; ++i){
|
||||
l = hailstone(i).size();
|
||||
if (l > maxseq.second) maxseq = std::make_pair(i, l);
|
||||
}
|
||||
return maxseq;
|
||||
}
|
||||
|
||||
int main () {
|
||||
|
||||
// Use the routine to show that the hailstone sequence for the number 27
|
||||
std::vector<int> h27;
|
||||
h27 = hailstone(27);
|
||||
// has 112 elements
|
||||
int l = h27.size();
|
||||
std::cout << "length of hailstone(27) is " << l;
|
||||
// starting with 27, 82, 41, 124 and
|
||||
std::cout << " first four elements of hailstone(27) are ";
|
||||
std::cout << h27[0] << " " << h27[1] << " "
|
||||
<< h27[2] << " " << h27[3] << std::endl;
|
||||
// ending with 8, 4, 2, 1
|
||||
std::cout << " last four elements of hailstone(27) are "
|
||||
<< h27[l-4] << " " << h27[l-3] << " "
|
||||
<< h27[l-2] << " " << h27[l-1] << std::endl;
|
||||
|
||||
std::pair<int,int> m = find_longest_hailstone_seq(100000);
|
||||
|
||||
std::cout << "the longest hailstone sequence under 100,000 is " << m.first
|
||||
<< " with " << m.second << " elements." <<std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
41
Task/Hailstone-sequence/C/hailstone-sequence-1.c
Normal file
41
Task/Hailstone-sequence/C/hailstone-sequence-1.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int hailstone(int n, int *arry)
|
||||
{
|
||||
int hs = 1;
|
||||
|
||||
while (n!=1) {
|
||||
hs++;
|
||||
if (arry) *arry++ = n;
|
||||
n = (n&1) ? (3*n+1) : (n/2);
|
||||
}
|
||||
if (arry) *arry++ = n;
|
||||
return hs;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int j, hmax = 0;
|
||||
int jatmax, n;
|
||||
int *arry;
|
||||
|
||||
for (j=1; j<100000; j++) {
|
||||
n = hailstone(j, NULL);
|
||||
if (hmax < n) {
|
||||
hmax = n;
|
||||
jatmax = j;
|
||||
}
|
||||
}
|
||||
n = hailstone(27, NULL);
|
||||
arry = malloc(n*sizeof(int));
|
||||
n = hailstone(27, arry);
|
||||
|
||||
printf("[ %d, %d, %d, %d, ...., %d, %d, %d, %d] len=%d\n",
|
||||
arry[0],arry[1],arry[2],arry[3],
|
||||
arry[n-4], arry[n-3], arry[n-2], arry[n-1], n);
|
||||
printf("Max %d at j= %d\n", hmax, jatmax);
|
||||
free(arry);
|
||||
|
||||
return 0;
|
||||
}
|
||||
31
Task/Hailstone-sequence/C/hailstone-sequence-2.c
Normal file
31
Task/Hailstone-sequence/C/hailstone-sequence-2.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define N 10000000
|
||||
#define CS N /* cache size */
|
||||
|
||||
typedef unsigned long ulong;
|
||||
ulong cache[CS] = {0};
|
||||
|
||||
ulong hailstone(ulong n)
|
||||
{
|
||||
int x;
|
||||
if (n == 1) return 1;
|
||||
if (n < CS && cache[n]) return cache[n];
|
||||
|
||||
x = 1 + hailstone((n & 1) ? 3 * n + 1 : n / 2);
|
||||
if (n < CS) cache[n] = x;
|
||||
return x;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, l, max = 0, mi;
|
||||
for (i = 1; i < N; i++) {
|
||||
if ((l = hailstone(i)) > max) {
|
||||
max = l;
|
||||
mi = i;
|
||||
}
|
||||
}
|
||||
printf("max below %d: %d, length %d\n", N, mi, max);
|
||||
return 0;
|
||||
}
|
||||
76
Task/Hailstone-sequence/CLIPS/hailstone-sequence.clips
Normal file
76
Task/Hailstone-sequence/CLIPS/hailstone-sequence.clips
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
(deftemplate longest
|
||||
(slot bound) ; upper bound for the range of values to check
|
||||
(slot next (default 2)) ; next value that needs to be checked
|
||||
(slot start (default 1)) ; starting value of longest sequence
|
||||
(slot len (default 1)) ; length of longest sequence
|
||||
)
|
||||
|
||||
(deffacts startup
|
||||
(query 27)
|
||||
(longest (bound 100000))
|
||||
)
|
||||
|
||||
(deffunction hailstone-next
|
||||
(?n)
|
||||
(if (evenp ?n)
|
||||
then (div ?n 2)
|
||||
else (+ (* 3 ?n) 1)
|
||||
)
|
||||
)
|
||||
|
||||
(defrule extend-sequence
|
||||
?hail <- (hailstone $?sequence ?tail&:(> ?tail 1))
|
||||
=>
|
||||
(retract ?hail)
|
||||
(assert (hailstone ?sequence ?tail (hailstone-next ?tail)))
|
||||
)
|
||||
|
||||
(defrule start-query
|
||||
(query ?num)
|
||||
=>
|
||||
(assert (hailstone ?num))
|
||||
)
|
||||
|
||||
(defrule result-query
|
||||
(query ?num)
|
||||
(hailstone ?num $?sequence 1)
|
||||
=>
|
||||
(bind ?sequence (create$ ?num ?sequence 1))
|
||||
(printout t "Hailstone sequence starting with " ?num ":" crlf)
|
||||
(bind ?len (length ?sequence))
|
||||
(printout t " Length: " ?len crlf)
|
||||
(printout t " First four: " (implode$ (subseq$ ?sequence 1 4)) crlf)
|
||||
(printout t " Last four: " (implode$ (subseq$ ?sequence (- ?len 3) ?len)) crlf)
|
||||
(printout t crlf)
|
||||
)
|
||||
|
||||
(defrule longest-create-next-hailstone
|
||||
(longest (bound ?bound) (next ?next))
|
||||
(test (<= ?next ?bound))
|
||||
(not (hailstone ?next $?))
|
||||
=>
|
||||
(assert (hailstone ?next))
|
||||
)
|
||||
|
||||
(defrule longest-check-next-hailstone
|
||||
?longest <- (longest (bound ?bound) (next ?next) (start ?start) (len ?len))
|
||||
(test (<= ?next ?bound))
|
||||
?hailstone <- (hailstone ?next $?sequence 1)
|
||||
=>
|
||||
(retract ?hailstone)
|
||||
(bind ?thislen (+ 2 (length ?sequence)))
|
||||
(if (> ?thislen ?len) then
|
||||
(modify ?longest (start ?next) (len ?thislen) (next (+ ?next 1)))
|
||||
else
|
||||
(modify ?longest (next (+ ?next 1)))
|
||||
)
|
||||
)
|
||||
|
||||
(defrule longest-finished
|
||||
(longest (bound ?bound) (next ?next) (start ?start) (len ?len))
|
||||
(test (> ?next ?bound))
|
||||
=>
|
||||
(printout t "The number less than " ?bound " that has the largest hailstone" crlf)
|
||||
(printout t "sequence is " ?start " with a length of " ?len "." crlf)
|
||||
(printout t crlf)
|
||||
)
|
||||
17
Task/Hailstone-sequence/Clojure/hailstone-sequence.clj
Normal file
17
Task/Hailstone-sequence/Clojure/hailstone-sequence.clj
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
(defn hailstone-seq [n]
|
||||
(:pre [(pos? n)])
|
||||
(lazy-seq
|
||||
(cond (= n 1) '(1)
|
||||
(even? n) (cons n (hailstone-seq (/ n 2)))
|
||||
:else (cons n (hailstone-seq (+ (* n 3) 1))))))
|
||||
|
||||
(def hseq27 (hailstone-seq 27))
|
||||
(assert (= (count hseq27) 112))
|
||||
(assert (= (take 4 hseq27) [27 82 41 124]))
|
||||
(assert (= (drop 108 hseq27) [8 4 2 1]))
|
||||
|
||||
(let [{max-i :num, max-len :len}
|
||||
(reduce #(max-key :len %1 %2)
|
||||
(for [i (range 1 100000)]
|
||||
{:num i, :len (count (hailstone-seq i))}))]
|
||||
(println "Maximum length" max-len "was found for hailstone(" max-i ")."))
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
hailstone = (n) ->
|
||||
if n is 1
|
||||
[n]
|
||||
|
||||
else if n % 2 is 0
|
||||
[n].concat hailstone n/2
|
||||
|
||||
else
|
||||
[n].concat hailstone (3*n) + 1
|
||||
|
||||
h27 = hailstone 27
|
||||
console.log "hailstone(27) = #{h27[0..3]} ... #{h27[-4..]} (length: #{h27.length})"
|
||||
|
||||
maxlength = 0
|
||||
maxnums = []
|
||||
|
||||
for i in [1..100000]
|
||||
seq = hailstone i
|
||||
|
||||
if seq.length is maxlength
|
||||
maxnums.push i
|
||||
else if seq.length > maxlength
|
||||
maxlength = seq.length
|
||||
maxnums = [i]
|
||||
|
||||
console.log "Max length: #{maxlength}; numbers generating sequences of this length: #{maxnums}"
|
||||
11
Task/Hailstone-sequence/Common-Lisp/hailstone-sequence.lisp
Normal file
11
Task/Hailstone-sequence/Common-Lisp/hailstone-sequence.lisp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(defun hailstone (n)
|
||||
(cond ((= n 1) '(1))
|
||||
((evenp n) (cons n (hailstone (/ n 2))))
|
||||
(t (cons n (hailstone (+ (* 3 n) 1))))))
|
||||
|
||||
(defun longest (n)
|
||||
(let ((k 0) (l 0))
|
||||
(loop for i from 1 below n do
|
||||
(let ((len (length (hailstone i))))
|
||||
(when (> len l) (setq l len k i)))
|
||||
finally (format t "Longest hailstone sequence under ~A for ~A, having length ~A." n k l))))
|
||||
22
Task/Hailstone-sequence/D/hailstone-sequence-1.d
Normal file
22
Task/Hailstone-sequence/D/hailstone-sequence-1.d
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import std.stdio, std.algorithm, std.range, std.typecons;
|
||||
|
||||
auto hailstone(int n) pure nothrow {
|
||||
auto result = [n];
|
||||
while (n != 1) {
|
||||
n = n & 1 ? n*3 + 1 : n/2;
|
||||
result ~= n;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum M = 27;
|
||||
auto h = hailstone(M);
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$-4 .. $]);
|
||||
writeln("length hailstone(", M, ")= ", h.length);
|
||||
|
||||
enum N = 100_000;
|
||||
auto s = iota(1, N).map!(i => tuple(hailstone(i).length, i))();
|
||||
auto p = reduce!max(s);
|
||||
writeln("Longest sequence in [1,", N, "]= ",p[1]," with len ",p[0]);
|
||||
}
|
||||
20
Task/Hailstone-sequence/D/hailstone-sequence-2.d
Normal file
20
Task/Hailstone-sequence/D/hailstone-sequence-2.d
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import std.stdio, std.algorithm, std.range, std.typecons;
|
||||
|
||||
struct Hail {
|
||||
int n;
|
||||
bool empty() { return n == 0; }
|
||||
int front() { return n; }
|
||||
void popFront() { n = n == 1 ? 0 : (n & 1 ? n*3 + 1 : n/2); }
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum M = 27;
|
||||
auto h = array(Hail(M));
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$-4 .. $]);
|
||||
writeln("length hailstone(", M, ")= ", h.length);
|
||||
|
||||
enum N = 100_000;
|
||||
auto s = map!(i => tuple(walkLength(Hail(i)), i))(iota(1, N));
|
||||
auto p = reduce!max(s);
|
||||
writeln("Longest sequence in [1,", N, "]= ",p[1]," with len ",p[0]);
|
||||
}
|
||||
52
Task/Hailstone-sequence/Dart/hailstone-sequence.dart
Normal file
52
Task/Hailstone-sequence/Dart/hailstone-sequence.dart
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
List<int> hailstone(int n) {
|
||||
if(n<=0) {
|
||||
throw new IllegalArgumentException("start value must be >=1)");
|
||||
}
|
||||
Queue<int> seq=new Queue<int>();
|
||||
seq.add(n);
|
||||
while(n!=1) {
|
||||
n=n%2==0?(n/2).toInt():3*n+1;
|
||||
seq.add(n);
|
||||
}
|
||||
return new List<int>.from(seq);
|
||||
}
|
||||
|
||||
// apparently List is missing toString()
|
||||
String iterableToString(Iterable seq) {
|
||||
String str="[";
|
||||
Iterator i=seq.iterator();
|
||||
while(i.hasNext()) {
|
||||
str+=i.next();
|
||||
if(i.hasNext()) {
|
||||
str+=",";
|
||||
}
|
||||
}
|
||||
return str+"]";
|
||||
}
|
||||
|
||||
main() {
|
||||
for(int i=1;i<=10;i++) {
|
||||
print("h($i)="+iterableToString(hailstone(i)));
|
||||
}
|
||||
List<int> h27=hailstone(27);
|
||||
List<int> first4=h27.getRange(0,4);
|
||||
print("first 4 elements of h(27): "+iterableToString(first4));
|
||||
Expect.listEquals([27,82,41,124],first4);
|
||||
|
||||
List<int> last4=h27.getRange(h27.length-4,4);
|
||||
print("last 4 elements of h(27): "+iterableToString(last4));
|
||||
Expect.listEquals([8,4,2,1],last4);
|
||||
|
||||
print("length of sequence h(27): "+h27.length);
|
||||
Expect.equals(112,h27.length);
|
||||
|
||||
int seq,max=0;
|
||||
for(int i=1;i<=100000;i++) {
|
||||
List<int> h=hailstone(i);
|
||||
if(h.length>max) {
|
||||
max=h.length;
|
||||
seq=i;
|
||||
}
|
||||
}
|
||||
print("up to 100000 the sequence h($seq) has the largest length ($max)");
|
||||
}
|
||||
5
Task/Hailstone-sequence/Dc/hailstone-sequence-1.dc
Normal file
5
Task/Hailstone-sequence/Dc/hailstone-sequence-1.dc
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
27
|
||||
[[--: ]nzpq]sq
|
||||
[d 2/ p]se
|
||||
[d 3*1+ p]so
|
||||
[d2% 0=e d1=q d2% 1=o d1=q lxx]dsxx
|
||||
8
Task/Hailstone-sequence/Dc/hailstone-sequence-2.dc
Normal file
8
Task/Hailstone-sequence/Dc/hailstone-sequence-2.dc
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
0dsLsT1st
|
||||
[dsLltsT]sM
|
||||
[[zdlL<M q]sq
|
||||
[d 2/]se
|
||||
[d 3*1+ ]so
|
||||
[d2% 0=e d1=q d2% 1=o d1=q lxx]dsxx]ss
|
||||
[lt1+dstlsxc lt100000>l]dslx
|
||||
lTn[:]nlLp
|
||||
57
Task/Hailstone-sequence/Delphi/hailstone-sequence.delphi
Normal file
57
Task/Hailstone-sequence/Delphi/hailstone-sequence.delphi
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
program ShowHailstoneSequence;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses SysUtils, Generics.Collections;
|
||||
|
||||
procedure GetHailstoneSequence(aStartingNumber: Integer; aHailstoneList: TList<Integer>);
|
||||
var
|
||||
n: Integer;
|
||||
begin
|
||||
aHailstoneList.Clear;
|
||||
aHailstoneList.Add(aStartingNumber);
|
||||
n := aStartingNumber;
|
||||
|
||||
while n <> 1 do
|
||||
begin
|
||||
if Odd(n) then
|
||||
n := (3 * n) + 1
|
||||
else
|
||||
n := n div 2;
|
||||
aHailstoneList.Add(n);
|
||||
end;
|
||||
end;
|
||||
|
||||
var
|
||||
i: Integer;
|
||||
lList: TList<Integer>;
|
||||
lMaxSequence: Integer;
|
||||
lMaxLength: Integer;
|
||||
begin
|
||||
lList := TList<Integer>.Create;
|
||||
try
|
||||
GetHailstoneSequence(27, lList);
|
||||
Writeln(Format('27: %d elements', [lList.Count]));
|
||||
Writeln(Format('[%d,%d,%d,%d ... %d,%d,%d,%d]',
|
||||
[lList[0], lList[1], lList[2], lList[3],
|
||||
lList[lList.Count - 4], lList[lList.Count - 3], lList[lList.Count - 2], lList[lList.Count - 1]]));
|
||||
Writeln;
|
||||
|
||||
lMaxSequence := 0;
|
||||
lMaxLength := 0;
|
||||
for i := 1 to 100000 do
|
||||
begin
|
||||
GetHailstoneSequence(i, lList);
|
||||
if lList.Count > lMaxLength then
|
||||
begin
|
||||
lMaxSequence := i;
|
||||
lMaxLength := lList.Count;
|
||||
end;
|
||||
end;
|
||||
Writeln(Format('Longest sequence under 100,000: %d with %d elements', [lMaxSequence, lMaxLength]));
|
||||
finally
|
||||
lList.Free;
|
||||
end;
|
||||
|
||||
Readln;
|
||||
end.
|
||||
24
Task/Hailstone-sequence/Erlang/hailstone-sequence.erl
Normal file
24
Task/Hailstone-sequence/Erlang/hailstone-sequence.erl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
-module(hailstone).
|
||||
-import(io).
|
||||
-export([main/0]).
|
||||
|
||||
hailstone(1) -> [1];
|
||||
hailstone(N) when N band 1 == 1 -> [N|hailstone(N * 3 + 1)];
|
||||
hailstone(N) when N band 1 == 0 -> [N|hailstone(N div 2)].
|
||||
|
||||
max_length(Start, Stop) ->
|
||||
F = fun (N) -> {length(hailstone(N)), N} end,
|
||||
Lengths = lists:map(F, lists:seq(Start, Stop)),
|
||||
lists:max(Lengths).
|
||||
|
||||
main() ->
|
||||
io:format("hailstone(4): ~w~n", [hailstone(4)]),
|
||||
Seq27 = hailstone(27),
|
||||
io:format("hailstone(27) length: ~B~n", [length(Seq27)]),
|
||||
io:format("hailstone(27) first 4: ~w~n",
|
||||
[lists:sublist(Seq27, 4)]),
|
||||
io:format("hailstone(27) last 4: ~w~n",
|
||||
[lists:nthtail(length(Seq27) - 4, Seq27)]),
|
||||
io:format("finding maximum hailstone(N) length for 1 <= N <= 100000..."),
|
||||
{Length, N} = max_length(1, 100000),
|
||||
io:format(" done.~nhailstone(~B) length: ~B~n", [N, Length]).
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
>function hailstone (n) ...
|
||||
$ v=[n];
|
||||
$ repeat
|
||||
$ if mod(n,2) then n=3*n+1;
|
||||
$ else n=n/2;
|
||||
$ endif;
|
||||
$ v=v|n;
|
||||
$ until n==1;
|
||||
$ end;
|
||||
$ return v;
|
||||
$ endfunction
|
||||
>hailstone(27), length(%)
|
||||
[ 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 ]
|
||||
112
|
||||
>function hailstonelength (n) ...
|
||||
$ v=zeros(1,n);
|
||||
$ v[1]=4; v[2]=2;
|
||||
$ loop 3 to n;
|
||||
$ count=1;
|
||||
$ n=#;
|
||||
$ repeat
|
||||
$ if mod(n,2) then n=3*n+1;
|
||||
$ else n=n/2;
|
||||
$ endif;
|
||||
$ if n<=cols(v) and v[n] then
|
||||
$ v[#]=v[n]+count;
|
||||
$ break;
|
||||
$ endif;
|
||||
$ count=count+1;
|
||||
$ end;
|
||||
$ end;
|
||||
$ return v;
|
||||
$ endfunction
|
||||
>h=hailstonelength(100000);
|
||||
>ex=extrema(h); ex[3], ex[4]
|
||||
351
|
||||
77031
|
||||
46
Task/Hailstone-sequence/Euphoria/hailstone-sequence.euphoria
Normal file
46
Task/Hailstone-sequence/Euphoria/hailstone-sequence.euphoria
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
function hailstone(atom n)
|
||||
sequence s
|
||||
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
|
||||
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
|
||||
s = hailstone(27)
|
||||
puts(1,"hailstone(27) =\n")
|
||||
? s
|
||||
printf(1,"len = %d\n\n",length(s))
|
||||
|
||||
integer max,imax,count
|
||||
max = 0
|
||||
for i = 2 to 1e5-1 do
|
||||
count = hailstone_count(i)
|
||||
if count > max then
|
||||
max = count
|
||||
imax = i
|
||||
end if
|
||||
end for
|
||||
|
||||
printf(1,"The longest hailstone sequence under 100,000 is %d with %d elements.\n",
|
||||
{imax,max})
|
||||
8
Task/Hailstone-sequence/FALSE/hailstone-sequence.false
Normal file
8
Task/Hailstone-sequence/FALSE/hailstone-sequence.false
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[$1&$[%3*1+0~]?~[2/]?]n:
|
||||
[[$." "$1>][n;!]#%]s:
|
||||
[1\[$1>][\1+\n;!]#%]c:
|
||||
27s;! 27c;!."
|
||||
"
|
||||
0m:0f:
|
||||
1[$100000\>][$c;!$m;>[m:$f:0]?%1+]#%
|
||||
f;." has hailstone sequence length "m;.
|
||||
31
Task/Hailstone-sequence/Factor/hailstone-sequence.factor
Normal file
31
Task/Hailstone-sequence/Factor/hailstone-sequence.factor
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
! rosetta/hailstone/hailstone.factor
|
||||
USING: arrays io kernel math math.ranges prettyprint sequences vectors ;
|
||||
IN: rosetta.hailstone
|
||||
|
||||
: hailstone ( n -- seq )
|
||||
[ 1vector ] keep
|
||||
[ dup 1 number= ]
|
||||
[
|
||||
dup even? [ 2 / ] [ 3 * 1 + ] if
|
||||
2dup swap push
|
||||
] until
|
||||
drop ;
|
||||
|
||||
<PRIVATE
|
||||
: main ( -- )
|
||||
27 hailstone dup dup
|
||||
"The hailstone sequence from 27:" print
|
||||
" has length " write length .
|
||||
" starts with " write 4 head [ unparse ] map ", " join print
|
||||
" ends with " write 4 tail* [ unparse ] map ", " join print
|
||||
|
||||
! Maps n => { length n }, and reduces to longest Hailstone sequence.
|
||||
1 100000 [a,b)
|
||||
[ [ hailstone length ] keep 2array ]
|
||||
[ [ [ first ] bi@ > ] most ] map-reduce
|
||||
first2
|
||||
"The hailstone sequence from " write pprint
|
||||
" has length " write pprint "." print ;
|
||||
PRIVATE>
|
||||
|
||||
MAIN: main
|
||||
19
Task/Hailstone-sequence/Forth/hailstone-sequence.fth
Normal file
19
Task/Hailstone-sequence/Forth/hailstone-sequence.fth
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
: hail-next ( n -- n )
|
||||
dup 1 and if 3 * 1+ else 2/ then ;
|
||||
: .hail ( n -- )
|
||||
begin dup . dup 1 > while hail-next repeat drop ;
|
||||
: hail-len ( n -- n )
|
||||
1 begin over 1 > while swap hail-next swap 1+ repeat nip ;
|
||||
|
||||
27 hail-len . cr
|
||||
27 .hail cr
|
||||
|
||||
: longest-hail ( max -- )
|
||||
0 0 rot 1+ 1 do ( n length )
|
||||
i hail-len 2dup < if
|
||||
nip nip i swap
|
||||
else drop then
|
||||
loop
|
||||
swap . ." has hailstone sequence length " . ;
|
||||
|
||||
100000 longest-hail
|
||||
49
Task/Hailstone-sequence/Fortran/hailstone-sequence.f
Normal file
49
Task/Hailstone-sequence/Fortran/hailstone-sequence.f
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
program Hailstone
|
||||
implicit none
|
||||
|
||||
integer :: i, maxn
|
||||
integer :: maxseqlen = 0, seqlen
|
||||
integer, allocatable :: seq(:)
|
||||
|
||||
call hs(27, seqlen)
|
||||
allocate(seq(seqlen))
|
||||
call hs(27, seqlen, seq)
|
||||
write(*,"(a,i0,a)") "Hailstone sequence for 27 has ", seqlen, " elements"
|
||||
write(*,"(a,4(i0,a),3(i0,a),i0)") "Sequence = ", seq(1), ", ", seq(2), ", ", seq(3), ", ", seq(4), " ...., ", &
|
||||
seq(seqlen-3), ", ", seq(seqlen-2), ", ", seq(seqlen-1), ", ", seq(seqlen)
|
||||
|
||||
do i = 1, 99999
|
||||
call hs(i, seqlen)
|
||||
if (seqlen > maxseqlen) then
|
||||
maxseqlen = seqlen
|
||||
maxn = i
|
||||
end if
|
||||
end do
|
||||
write(*,*)
|
||||
write(*,"(a,i0,a,i0,a)") "Longest sequence under 100000 is for ", maxn, " with ", maxseqlen, " elements"
|
||||
|
||||
deallocate(seq)
|
||||
|
||||
contains
|
||||
|
||||
subroutine hs(number, length, seqArray)
|
||||
integer, intent(in) :: number
|
||||
integer, intent(out) :: length
|
||||
integer, optional, intent(inout) :: seqArray(:)
|
||||
integer :: n
|
||||
|
||||
n = number
|
||||
length = 1
|
||||
if(present(seqArray)) seqArray(1) = n
|
||||
do while(n /= 1)
|
||||
if(mod(n,2) == 0) then
|
||||
n = n / 2
|
||||
else
|
||||
n = n * 3 + 1
|
||||
end if
|
||||
length = length + 1
|
||||
if(present(seqArray)) seqArray(length) = n
|
||||
end do
|
||||
end subroutine
|
||||
|
||||
end program
|
||||
19
Task/Hailstone-sequence/Frege/hailstone-sequence.frege
Normal file
19
Task/Hailstone-sequence/Frege/hailstone-sequence.frege
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
module Hailstone where
|
||||
|
||||
import Data.List (maximumBy)
|
||||
|
||||
hailstone :: Int -> [Int]
|
||||
hailstone 1 = [1]
|
||||
hailstone n | even n = n : hailstone (n `div` 2)
|
||||
| otherwise = n : hailstone (n * 3 + 1)
|
||||
|
||||
withResult :: (t -> t1) -> t -> (t1, t)
|
||||
withResult f x = (f x, x)
|
||||
|
||||
main _ = do
|
||||
let h27 = hailstone 27
|
||||
printStrLn $ show $ length h27
|
||||
let h4 = show $ take 4 h27
|
||||
let t4 = show $ drop (length h27 - 4) h27
|
||||
printStrLn ("hailstone 27: " ++ h4 ++ " ... " ++ t4)
|
||||
printStrLn $ show $ maximumBy (comparing fst) $ map (withResult (length . hailstone)) (1..100000)
|
||||
60
Task/Hailstone-sequence/GAP/hailstone-sequence.gap
Normal file
60
Task/Hailstone-sequence/GAP/hailstone-sequence.gap
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
CollatzSequence := function(n)
|
||||
local v;
|
||||
v := [ n ];
|
||||
while n > 1 do
|
||||
if IsEvenInt(n) then
|
||||
n := QuoInt(n, 2);
|
||||
else
|
||||
n := 3*n + 1;
|
||||
fi;
|
||||
Add(v, n);
|
||||
od;
|
||||
return v;
|
||||
end;
|
||||
|
||||
CollatzLength := function(n)
|
||||
local m;
|
||||
m := 1;
|
||||
while n > 1 do
|
||||
if IsEvenInt(n) then
|
||||
n := QuoInt(n, 2);
|
||||
else
|
||||
n := 3*n + 1;
|
||||
fi;
|
||||
m := m + 1;
|
||||
od;
|
||||
return m;
|
||||
end;
|
||||
|
||||
CollatzMax := function(a, b)
|
||||
local n, len, nmax, lmax;
|
||||
lmax := 0;
|
||||
for n in [a .. b] do
|
||||
len := CollatzLength(n);
|
||||
if len > lmax then
|
||||
nmax := n;
|
||||
lmax := len;
|
||||
fi;
|
||||
od;
|
||||
return [ nmax, lmax ];
|
||||
end;
|
||||
|
||||
CollatzSequence(27);
|
||||
# [ 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 ]
|
||||
CollatzLength(27);
|
||||
# 112
|
||||
|
||||
CollatzMax(1, 100);
|
||||
# [ 97, 119 ]
|
||||
CollatzMax(1, 1000);
|
||||
# [ 871, 179 ]
|
||||
CollatzMax(1, 10000);
|
||||
# [ 6171, 262 ]
|
||||
CollatzMax(1, 100000);
|
||||
# [ 77031, 351 ]
|
||||
CollatzMax(1, 1000000);
|
||||
# [ 837799, 525 ]
|
||||
36
Task/Hailstone-sequence/Go/hailstone-sequence-1.go
Normal file
36
Task/Hailstone-sequence/Go/hailstone-sequence-1.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func hs(n int, seq *[]int) {
|
||||
s := append((*seq)[:0], n)
|
||||
for n > 1 {
|
||||
if n&1 == 0 {
|
||||
n = n / 2
|
||||
} else {
|
||||
n = 3*n + 1
|
||||
}
|
||||
s = append(s, n)
|
||||
}
|
||||
*seq = s
|
||||
}
|
||||
|
||||
func main() {
|
||||
var seq []int
|
||||
|
||||
hs(27, &seq)
|
||||
fmt.Printf("hs(27): %d elements: [%d %d %d %d ... %d %d %d %d]\n",
|
||||
len(seq), seq[0], seq[1], seq[2], seq[3],
|
||||
seq[len(seq)-4], seq[len(seq)-3], seq[len(seq)-2], seq[len(seq)-1])
|
||||
|
||||
var maxN, maxLen int
|
||||
for n := 1; n < 100000; n++ {
|
||||
// reusing seq reduces garbage
|
||||
hs(n, &seq)
|
||||
if len(seq) > maxLen {
|
||||
maxN = n
|
||||
maxLen = len(seq)
|
||||
}
|
||||
}
|
||||
fmt.Printf("hs(%d): %d elements\n", maxN, maxLen)
|
||||
}
|
||||
68
Task/Hailstone-sequence/Go/hailstone-sequence-2.go
Normal file
68
Task/Hailstone-sequence/Go/hailstone-sequence-2.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Task 1 implemented with a generator. Calling newHg will "create
|
||||
// a routine to generate the hailstone sequence for a number."
|
||||
func newHg(n int) func() int {
|
||||
return func() (n0 int) {
|
||||
n0 = n
|
||||
if n&1 == 0 {
|
||||
n = n / 2
|
||||
} else {
|
||||
n = 3*n + 1
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// make generator for sequence starting at 27
|
||||
hg := newHg(27)
|
||||
// save first four elements for printing later
|
||||
s1, s2, s3, s4 := hg(), hg(), hg(), hg()
|
||||
// load next four elements in variables to use as shift register.
|
||||
e4, e3, e2, e1 := hg(), hg(), hg(), hg()
|
||||
// 4+4= 8 that we've generated so far
|
||||
ec := 8
|
||||
// until we get to 1, generate another value, shift, and increment.
|
||||
// note that intermediate elements--those shifted off--are not saved.
|
||||
for e1 > 1 {
|
||||
e4, e3, e2, e1 = e3, e2, e1, hg()
|
||||
ec++
|
||||
}
|
||||
// Complete task 2:
|
||||
fmt.Printf("hs(27): %d elements: [%d %d %d %d ... %d %d %d %d]\n",
|
||||
ec, s1, s2, s3, s4, e4, e3, e2, e1)
|
||||
|
||||
// Task 3: strategy is to not store sequences, but just the length
|
||||
// of each sequence. as soon as the sequence we're currently working on
|
||||
// dips into the range that we've already computed, we short-circuit
|
||||
// to the end by adding the that known length to whatever length
|
||||
// we've accumulated so far.
|
||||
|
||||
var nMaxLen int // variable holds n with max length encounted so far
|
||||
// slice holds sequence length for each n as it is computed
|
||||
var computedLen [1e5]int
|
||||
computedLen[1] = 1
|
||||
for n := 2; n < 1e5; n++ {
|
||||
var ele, lSum int
|
||||
for hg := newHg(n); ; lSum++ {
|
||||
ele = hg()
|
||||
// as soon as we get an element in the range we have already
|
||||
// computed, we're done...
|
||||
if ele < n {
|
||||
break
|
||||
}
|
||||
}
|
||||
// just add the sequence length already computed from this point.
|
||||
lSum += computedLen[ele]
|
||||
// save the sequence length for this n
|
||||
computedLen[n] = lSum
|
||||
// and note if it's the maximum so far
|
||||
if lSum > computedLen[nMaxLen] {
|
||||
nMaxLen = n
|
||||
}
|
||||
}
|
||||
fmt.Printf("hs(%d): %d elements\n", nMaxLen, computedLen[nMaxLen])
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
def hailstone = { long start ->
|
||||
def sequence = []
|
||||
while (start != 1) {
|
||||
sequence << start
|
||||
start = (start % 2l == 0l) ? start / 2l : 3l * start + 1l
|
||||
}
|
||||
sequence << start
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def sequence = hailstone(27)
|
||||
assert sequence.size() == 112
|
||||
assert sequence[0..3] == [27, 82, 41, 124]
|
||||
assert sequence[-4..-1] == [8, 4, 2, 1]
|
||||
|
||||
def results = (1..100000).collect { [n:it, size:hailstone(it).size()] }.max { it.size }
|
||||
println results
|
||||
19
Task/Hailstone-sequence/Haskell/hailstone-sequence.hs
Normal file
19
Task/Hailstone-sequence/Haskell/hailstone-sequence.hs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import Data.List (maximumBy)
|
||||
import Data.Ord (comparing)
|
||||
|
||||
hailstone :: Int -> [Int]
|
||||
hailstone 1 = [1]
|
||||
hailstone n | even n = n : hailstone (n `div` 2)
|
||||
| otherwise = n : hailstone (n * 3 + 1)
|
||||
|
||||
withResult :: (t -> t1) -> t -> (t1, t)
|
||||
withResult f x = (f x, x)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
let h27 = hailstone 27
|
||||
print $ length h27
|
||||
let h4 = show $ take 4 h27
|
||||
let t4 = show $ drop (length h27 - 4) h27
|
||||
putStrLn ("hailstone 27: " ++ h4 ++ " ... " ++ t4)
|
||||
print $ maximumBy (comparing fst) $ map (withResult (length . hailstone)) [1..100000]
|
||||
34
Task/Hailstone-sequence/HicEst/hailstone-sequence.hicest
Normal file
34
Task/Hailstone-sequence/HicEst/hailstone-sequence.hicest
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
DIMENSION stones(1000)
|
||||
|
||||
H27 = hailstone(27)
|
||||
ALIAS(stones,1, first4,4)
|
||||
ALIAS(stones,H27-3, last4,4)
|
||||
WRITE(ClipBoard, Name) H27, first4, "...", last4
|
||||
|
||||
longest_sequence = 0
|
||||
DO try = 1, 1E5
|
||||
elements = hailstone(try)
|
||||
IF(elements >= longest_sequence) THEN
|
||||
number = try
|
||||
longest_sequence = elements
|
||||
WRITE(StatusBar, Name) number, longest_sequence
|
||||
ENDIF
|
||||
ENDDO
|
||||
WRITE(ClipBoard, Name) number, longest_sequence
|
||||
END
|
||||
|
||||
FUNCTION hailstone( n )
|
||||
USE : stones
|
||||
|
||||
stones(1) = n
|
||||
DO i = 1, LEN(stones)
|
||||
IF(stones(i) == 1) THEN
|
||||
hailstone = i
|
||||
RETURN
|
||||
ELSEIF( MOD(stones(i),2) ) THEN
|
||||
stones(i+1) = 3*stones(i) + 1
|
||||
ELSE
|
||||
stones(i+1) = stones(i) / 2
|
||||
ENDIF
|
||||
ENDDO
|
||||
END
|
||||
7
Task/Hailstone-sequence/Icon/hailstone-sequence-1.icon
Normal file
7
Task/Hailstone-sequence/Icon/hailstone-sequence-1.icon
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
procedure hailstone(n)
|
||||
while n > 1 do {
|
||||
suspend n
|
||||
n := if n%2 = 0 then n/2 else 3*n+1
|
||||
}
|
||||
suspend 1
|
||||
end
|
||||
4
Task/Hailstone-sequence/Icon/hailstone-sequence-2.icon
Normal file
4
Task/Hailstone-sequence/Icon/hailstone-sequence-2.icon
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
procedure main(args)
|
||||
n := integer(!args) | 27
|
||||
every writes(" ",hailstone(n))
|
||||
end
|
||||
9
Task/Hailstone-sequence/Icon/hailstone-sequence-3.icon
Normal file
9
Task/Hailstone-sequence/Icon/hailstone-sequence-3.icon
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
procedure hailstone(n)
|
||||
static cache
|
||||
initial {
|
||||
cache := table()
|
||||
cache[1] := [1]
|
||||
}
|
||||
/cache[n] := [n] ||| hailstone(if n%2 = 0 then n/2 else 3*n+1)
|
||||
return cache[n]
|
||||
end
|
||||
23
Task/Hailstone-sequence/Icon/hailstone-sequence-4.icon
Normal file
23
Task/Hailstone-sequence/Icon/hailstone-sequence-4.icon
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
procedure main(args)
|
||||
n := integer(!args) | 27
|
||||
task2(n)
|
||||
write()
|
||||
task3()
|
||||
end
|
||||
|
||||
procedure task2(n)
|
||||
count := 0
|
||||
every writes(" ",right(!(sequence := hailstone(n)),5)) do
|
||||
if (count +:= 1) % 15 = 0 then write()
|
||||
write()
|
||||
write(*sequence," value",(*sequence=1,"")|"s"," in the sequence.")
|
||||
end
|
||||
|
||||
procedure task3()
|
||||
maxHS := 0
|
||||
every n := 1 to 100000 do {
|
||||
count := *hailstone(n)
|
||||
if maxHS <:= count then maxN := n
|
||||
}
|
||||
write(maxN," has a sequence of ",maxHS," values")
|
||||
end
|
||||
53
Task/Hailstone-sequence/Inform-7/hailstone-sequence.inf
Normal file
53
Task/Hailstone-sequence/Inform-7/hailstone-sequence.inf
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
Home is a room.
|
||||
|
||||
To decide which list of numbers is the hailstone sequence for (N - number):
|
||||
let result be a list of numbers;
|
||||
add N to result;
|
||||
while N is not 1:
|
||||
if N is even, let N be N / 2;
|
||||
otherwise let N be (3 * N) + 1;
|
||||
add N to result;
|
||||
decide on result.
|
||||
|
||||
Hailstone length cache relates various numbers to one number.
|
||||
|
||||
To decide which number is the hailstone sequence length for (N - number):
|
||||
let ON be N;
|
||||
let length so far be 0;
|
||||
while N is not 1:
|
||||
if N relates to a number by the hailstone length cache relation:
|
||||
let result be length so far plus the number to which N relates by the hailstone length cache relation;
|
||||
now the hailstone length cache relation relates ON to result;
|
||||
decide on result;
|
||||
if N is even, let N be N / 2;
|
||||
otherwise let N be (3 * N) + 1;
|
||||
increment length so far;
|
||||
let result be length so far plus 1;
|
||||
now the hailstone length cache relation relates ON to result;
|
||||
decide on result.
|
||||
|
||||
To say first and last (N - number) entry/entries in (L - list of values of kind K):
|
||||
let length be the number of entries in L;
|
||||
if length <= N * 2:
|
||||
say L;
|
||||
else:
|
||||
repeat with M running from 1 to N:
|
||||
if M > 1, say ", ";
|
||||
say entry M in L;
|
||||
say " ... ";
|
||||
repeat with M running from length - N + 1 to length:
|
||||
say entry M in L;
|
||||
if M < length, say ", ".
|
||||
|
||||
When play begins:
|
||||
let H27 be the hailstone sequence for 27;
|
||||
say "Hailstone sequence for 27 has [number of entries in H27] element[s]: [first and last 4 entries in H27].";
|
||||
let best length be 0;
|
||||
let best number be 0;
|
||||
repeat with N running from 1 to 99999:
|
||||
let L be the hailstone sequence length for N;
|
||||
if L > best length:
|
||||
let best length be L;
|
||||
let best number be N;
|
||||
say "The number under 100,000 with the longest hailstone sequence is [best number] with [best length] element[s].";
|
||||
end the story.
|
||||
5
Task/Hailstone-sequence/Ioke/hailstone-sequence.ioke
Normal file
5
Task/Hailstone-sequence/Ioke/hailstone-sequence.ioke
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
collatz = method(n,
|
||||
n println
|
||||
unless(n <= 1,
|
||||
if(n even?, collatz(n / 2), collatz(n * 3 + 1)))
|
||||
)
|
||||
1
Task/Hailstone-sequence/J/hailstone-sequence-1.j
Normal file
1
Task/Hailstone-sequence/J/hailstone-sequence-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
hailseq=: -:`(1 3&p.)@.(2&|) ^:(1 ~: ]) ^:a:"0
|
||||
7
Task/Hailstone-sequence/J/hailstone-sequence-2.j
Normal file
7
Task/Hailstone-sequence/J/hailstone-sequence-2.j
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# hailseq 27 NB. sequence length
|
||||
112
|
||||
4 _4 {."0 1 hailseq 27 NB. first & last 4 numbers in sequence
|
||||
27 82 41 124
|
||||
8 4 2 1
|
||||
(>:@(i. >./) , >./) #@hailseq }.i. 1e5 NB. number < 100000 with max seq length & its seq length
|
||||
77031 351
|
||||
96
Task/Hailstone-sequence/Java/hailstone-sequence.java
Normal file
96
Task/Hailstone-sequence/Java/hailstone-sequence.java
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class Hailstone {
|
||||
|
||||
public static List<Long> getHailstoneSequence(long n) {
|
||||
if (n <= 0)
|
||||
throw new IllegalArgumentException("Invalid starting sequence number");
|
||||
List<Long> list = new ArrayList<Long>();
|
||||
list.add(Long.valueOf(n));
|
||||
while (n != 1) {
|
||||
if ((n & 1) == 0)
|
||||
n = n / 2;
|
||||
else
|
||||
n = 3 * n + 1;
|
||||
list.add(Long.valueOf(n));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Long> sequence27 = getHailstoneSequence(27);
|
||||
System.out.println("Sequence for 27 has " + sequence27.size() + " elements: " + sequence27);
|
||||
|
||||
long MAX = 100000;
|
||||
// Simple way
|
||||
{
|
||||
long highestNumber = 1;
|
||||
int highestCount = 1;
|
||||
for (long i = 2; i < MAX; i++) {
|
||||
int count = getHailstoneSequence(i).size();
|
||||
if (count > highestCount) {
|
||||
highestCount = count;
|
||||
highestNumber = i;
|
||||
}
|
||||
}
|
||||
System.out.println("Method 1, number " + highestNumber + " has the longest sequence, with a length of " + highestCount);
|
||||
}
|
||||
|
||||
// More memory efficient way
|
||||
{
|
||||
long highestNumber = 1;
|
||||
int highestCount = 1;
|
||||
for (long i = 2; i < MAX; i++) {
|
||||
int count = 1;
|
||||
long n = i;
|
||||
while (n != 1) {
|
||||
if ((n & 1) == 0)
|
||||
n = n / 2;
|
||||
else
|
||||
n = 3 * n + 1;
|
||||
count++;
|
||||
}
|
||||
if (count > highestCount) {
|
||||
highestCount = count;
|
||||
highestNumber = i;
|
||||
}
|
||||
}
|
||||
System.out.println("Method 2, number " + highestNumber + " has the longest sequence, with a length of " + highestCount);
|
||||
}
|
||||
|
||||
// Efficient for analyzing all sequences
|
||||
{
|
||||
long highestNumber = 1;
|
||||
long highestCount = 1;
|
||||
Map<Long, Integer> sequenceMap = new HashMap<Long, Integer>();
|
||||
sequenceMap.put(Long.valueOf(1), Integer.valueOf(1));
|
||||
|
||||
List<Long> currentList = new ArrayList<Long>();
|
||||
for (long i = 2; i < MAX; i++) {
|
||||
currentList.clear();
|
||||
Long n = Long.valueOf(i);
|
||||
Integer count = null;
|
||||
while ((count = sequenceMap.get(n)) == null) {
|
||||
currentList.add(n);
|
||||
long nValue = n.longValue();
|
||||
if ((nValue & 1) == 0)
|
||||
n = Long.valueOf(nValue / 2);
|
||||
else
|
||||
n = Long.valueOf(3 * nValue + 1);
|
||||
}
|
||||
int curCount = count.intValue();
|
||||
for (int j = currentList.size() - 1; j >= 0; j--)
|
||||
sequenceMap.put(currentList.get(j), Integer.valueOf(++curCount));
|
||||
if (curCount > highestCount) {
|
||||
highestCount = curCount;
|
||||
highestNumber = i;
|
||||
}
|
||||
}
|
||||
System.out.println("Method 3, number " + highestNumber + " has the longest sequence, with a length of " + highestCount);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
23
Task/Hailstone-sequence/JavaScript/hailstone-sequence.js
Normal file
23
Task/Hailstone-sequence/JavaScript/hailstone-sequence.js
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
function hailstone (n) {
|
||||
var seq = [n];
|
||||
while (n > 1) {
|
||||
n = n % 2 ? 3 * n + 1 : n / 2;
|
||||
seq.push(n);
|
||||
}
|
||||
return seq;
|
||||
}
|
||||
|
||||
// task 2: verify the sequence for n = 27
|
||||
var h = hailstone(27), hLen = h.length;
|
||||
print("sequence 27 is (" + h.slice(0, 4).join(", ") + " ... "
|
||||
+ h.slice(hLen - 4, hLen).join(", ") + "). length: " + hLen);
|
||||
|
||||
// task 3: find the longest sequence for n < 100000
|
||||
for (var n, max = 0, i = 100000; --i;) {
|
||||
var seq = hailstone(i), sLen = seq.length;
|
||||
if (sLen > max) {
|
||||
n = i;
|
||||
max = sLen;
|
||||
}
|
||||
}
|
||||
print("longest sequence: " + max + " numbers for starting point " + n);
|
||||
8
Task/Hailstone-sequence/Julia/hailstone-sequence.julia
Normal file
8
Task/Hailstone-sequence/Julia/hailstone-sequence.julia
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
function hailstone(n)
|
||||
seq = [n]
|
||||
while n>1
|
||||
n = n % 2 == 0 ? n/2 : 3n + 1
|
||||
push!(seq,n)
|
||||
end
|
||||
return seq
|
||||
end
|
||||
12
Task/Hailstone-sequence/K/hailstone-sequence.k
Normal file
12
Task/Hailstone-sequence/K/hailstone-sequence.k
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
hail: (1<){:[x!2;1+3*x;_ x%2]}\
|
||||
seqn: hail 27
|
||||
|
||||
#seqn
|
||||
112
|
||||
4#seqn
|
||||
27 82 41 124
|
||||
-4#seqn
|
||||
8 4 2 1
|
||||
|
||||
{m,x@s?m:|/s:{#hail x}'x}{x@&x!2}!:1e5
|
||||
351 77031
|
||||
49
Task/Hailstone-sequence/LOLCODE/hailstone-sequence.lol
Normal file
49
Task/Hailstone-sequence/LOLCODE/hailstone-sequence.lol
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
HAI 1.3
|
||||
|
||||
HOW IZ I hailin YR stone
|
||||
I HAS A sequence ITZ A BUKKIT
|
||||
sequence HAS A length ITZ 1
|
||||
sequence HAS A SRS 0 ITZ stone
|
||||
|
||||
IM IN YR stoner
|
||||
BOTH SAEM stone AN 1, O RLY?
|
||||
YA RLY, FOUND YR sequence
|
||||
OIC
|
||||
|
||||
MOD OF stone AN 2, O RLY?
|
||||
YA RLY, stone R SUM OF PRODUKT OF stone AN 3 AN 1
|
||||
NO WAI, stone R QUOSHUNT OF stone AN 2
|
||||
OIC
|
||||
|
||||
sequence HAS A SRS sequence'Z length ITZ stone
|
||||
sequence'Z length R SUM OF sequence'Z length AN 1
|
||||
IM OUTTA YR stoner
|
||||
IF U SAY SO
|
||||
|
||||
I HAS A hail27 ITZ I IZ hailin YR 27 MKAY
|
||||
VISIBLE "hail(27) = "!
|
||||
|
||||
IM IN YR first4 UPPIN YR i TIL BOTH SAEM i AN 4
|
||||
VISIBLE hail27'Z SRS i " "!
|
||||
IM OUTTA YR first4
|
||||
VISIBLE "..."!
|
||||
|
||||
IM IN YR last4 UPPIN YR i TIL BOTH SAEM i AN 4
|
||||
VISIBLE " " hail27'Z SRS SUM OF 108 AN i!
|
||||
IM OUTTA YR last4
|
||||
VISIBLE ", length = " hail27'Z length
|
||||
|
||||
I HAS A max, I HAS A len ITZ 0
|
||||
|
||||
BTW, DIS IZ RLY NOT FAST SO WE ONLY CHEK N IN [75000, 80000)
|
||||
IM IN YR maxer UPPIN YR n TIL BOTH SAEM n AN 5000
|
||||
I HAS A n ITZ SUM OF n AN 75000
|
||||
I HAS A seq ITZ I IZ hailin YR n MKAY
|
||||
BOTH SAEM len AN SMALLR OF len AN seq'Z length, O RLY?
|
||||
YA RLY, max R n, len R seq'Z length
|
||||
OIC
|
||||
IM OUTTA YR maxer
|
||||
|
||||
VISIBLE "len(hail(" max ")) = " len
|
||||
|
||||
KTHXBYE
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
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
|
||||
print ""
|
||||
print "The following is the 'Hailstone Sequence' for your number..."
|
||||
print ""
|
||||
print hailstone
|
||||
while hailstone <> 1
|
||||
if hailstone / 2 = int(hailstone / 2) then hailstone = hailstone / 2 else hailstone = (3 * hailstone) + 1
|
||||
print hailstone
|
||||
wend
|
||||
print ""
|
||||
input "Hit 'Enter' to continue to part 2...";dummy$
|
||||
cls
|
||||
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."
|
||||
print ""
|
||||
print "No. in Seq.","Hailstone Sequence Number for 27"
|
||||
print ""
|
||||
c = 1: hailstone = 27
|
||||
print c, hailstone
|
||||
while hailstone <> 1
|
||||
c = c + 1
|
||||
if hailstone / 2 = int(hailstone / 2) then hailstone = hailstone / 2 else hailstone = (3 * hailstone) + 1
|
||||
print c, hailstone
|
||||
wend
|
||||
print ""
|
||||
input "Hit 'Enter' to continue to part 3...";dummy$
|
||||
cls
|
||||
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 ""
|
||||
print "Calculating result... Please wait... This could take a little while..."
|
||||
print ""
|
||||
print "Percent Done", "Start Number", "Seq. Length", "Maximum Sequence So Far"
|
||||
print ""
|
||||
for cc = 1 to 99999
|
||||
hailstone = cc: c = 1
|
||||
while hailstone <> 1
|
||||
c = c + 1
|
||||
if hailstone / 2 = int(hailstone / 2) then hailstone = hailstone / 2 else hailstone = (3 * hailstone) + 1
|
||||
wend
|
||||
if c > max then max = c: largesthailstone = cc
|
||||
locate 1, 7
|
||||
print " "
|
||||
locate 1, 7
|
||||
print using("###.###", cc / 99999 * 100);"%", cc, c, max
|
||||
scan
|
||||
next cc
|
||||
print ""
|
||||
print "The number less than 100,000 with the longest 'Hailstone Sequence' is "; largesthailstone;". It's sequence length is "; max;"."
|
||||
end
|
||||
23
Task/Hailstone-sequence/Logo/hailstone-sequence.logo
Normal file
23
Task/Hailstone-sequence/Logo/hailstone-sequence.logo
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
to hail.next :n
|
||||
output ifelse equal? 0 modulo :n 2 [:n/2] [3*:n + 1]
|
||||
end
|
||||
|
||||
to hail.seq :n
|
||||
if :n = 1 [output [1]]
|
||||
output fput :n hail.seq hail.next :n
|
||||
end
|
||||
|
||||
show hail.seq 27
|
||||
show count hail.seq 27
|
||||
|
||||
to max.hail :n
|
||||
localmake "max.n 0
|
||||
localmake "max.length 0
|
||||
repeat :n [if greater? count hail.seq repcount :max.length [
|
||||
make "max.n repcount
|
||||
make "max.length count hail.seq repcount
|
||||
] ]
|
||||
(print :max.n [has hailstone sequence length] :max.length)
|
||||
end
|
||||
|
||||
max.hail 100000
|
||||
94
Task/Hailstone-sequence/Logtalk/hailstone-sequence-1.logtalk
Normal file
94
Task/Hailstone-sequence/Logtalk/hailstone-sequence-1.logtalk
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
:- object(hailstone).
|
||||
|
||||
:- public(generate_sequence/2).
|
||||
:- mode(generate_sequence(+natural, -list(natural)), zero_or_one).
|
||||
:- info(generate_sequence/2, [
|
||||
comment is 'Generates the Hailstone sequence that starts with its first argument. Fails if the argument is not a natural number.',
|
||||
argnames is ['Start', 'Sequence']
|
||||
]).
|
||||
|
||||
:- public(write_sequence/1).
|
||||
:- mode(write_sequence(+natural), zero_or_one).
|
||||
:- info(write_sequence/1, [
|
||||
comment is 'Writes to the standard output the Hailstone sequence that starts with its argument. Fails if the argument is not a natural number.',
|
||||
argnames is ['Start']
|
||||
]).
|
||||
|
||||
:- public(sequence_length/2).
|
||||
:- mode(sequence_length(+natural, -natural), zero_or_one).
|
||||
:- info(sequence_length/2, [
|
||||
comment is 'Calculates the length of the Hailstone sequence that starts with its first argument. Fails if the argument is not a natural number.',
|
||||
argnames is ['Start', 'Length']
|
||||
]).
|
||||
|
||||
:- public(longest_sequence/4).
|
||||
:- mode(longest_sequence(+natural, +natural, -natural, -natural), zero_or_one).
|
||||
:- info(longest_sequence/4, [
|
||||
comment is 'Calculates the longest Hailstone sequence in the interval [Start, End]. Fails if the interval is not valid.',
|
||||
argnames is ['Start', 'End', 'N', 'Length']
|
||||
]).
|
||||
|
||||
generate_sequence(Start, Sequence) :-
|
||||
integer(Start),
|
||||
Start >= 1,
|
||||
sequence(Start, Sequence).
|
||||
|
||||
sequence(1, [1]) :-
|
||||
!.
|
||||
sequence(N, [N| Sequence]) :-
|
||||
( N mod 2 =:= 0 ->
|
||||
M is N // 2
|
||||
; M is (3 * N) + 1
|
||||
),
|
||||
sequence(M, Sequence).
|
||||
|
||||
write_sequence(Start) :-
|
||||
integer(Start),
|
||||
Start >= 1,
|
||||
sequence(Start).
|
||||
|
||||
sequence(1) :-
|
||||
!,
|
||||
write(1), nl.
|
||||
sequence(N) :-
|
||||
write(N), write(' '),
|
||||
( N mod 2 =:= 0 ->
|
||||
M is N // 2
|
||||
; M is (3 * N) + 1
|
||||
),
|
||||
sequence(M).
|
||||
|
||||
sequence_length(Start, Length) :-
|
||||
integer(Start),
|
||||
Start >= 1,
|
||||
sequence_length(Start, 1, Length).
|
||||
|
||||
sequence_length(1, Length, Length) :-
|
||||
!.
|
||||
sequence_length(N, Length0, Length) :-
|
||||
Length1 is Length0 + 1,
|
||||
( N mod 2 =:= 0 ->
|
||||
M is N // 2
|
||||
; M is (3 * N) + 1
|
||||
),
|
||||
sequence_length(M, Length1, Length).
|
||||
|
||||
longest_sequence(Start, End, N, Length) :-
|
||||
integer(Start),
|
||||
integer(End),
|
||||
Start >= 1,
|
||||
Start =< End,
|
||||
longest_sequence(Start, End, 1, N, 1, Length).
|
||||
|
||||
longest_sequence(Current, End, N, N, Length, Length) :-
|
||||
Current > End,
|
||||
!.
|
||||
longest_sequence(Current, End, N0, N, Length0, Length) :-
|
||||
sequence_length(Current, 1, CurrentLength),
|
||||
Next is Current + 1,
|
||||
( CurrentLength > Length0 ->
|
||||
longest_sequence(Next, End, Current, N, CurrentLength, Length)
|
||||
; longest_sequence(Next, End, N0, N, Length0, Length)
|
||||
).
|
||||
|
||||
:- end_object.
|
||||
11
Task/Hailstone-sequence/Logtalk/hailstone-sequence-2.logtalk
Normal file
11
Task/Hailstone-sequence/Logtalk/hailstone-sequence-2.logtalk
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
| ?- hailstone::write_sequence(27).
|
||||
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
|
||||
true
|
||||
|
||||
| ?- hailstone::sequence_length(27, Length).
|
||||
Length = 112
|
||||
true
|
||||
|
||||
| ?- hailstone::longest_sequence(1, 100000, N, Length).
|
||||
N = 77031, Length = 351
|
||||
true
|
||||
30
Task/Hailstone-sequence/Lua/hailstone-sequence.lua
Normal file
30
Task/Hailstone-sequence/Lua/hailstone-sequence.lua
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
function hailstone( n, print_numbers )
|
||||
local n_iter = 1
|
||||
|
||||
while n ~= 1 do
|
||||
if print_numbers then print( n ) end
|
||||
if n % 2 == 0 then
|
||||
n = n / 2
|
||||
else
|
||||
n = 3 * n + 1
|
||||
end
|
||||
|
||||
n_iter = n_iter + 1
|
||||
end
|
||||
if print_numbers then print( n ) end
|
||||
|
||||
return n_iter;
|
||||
end
|
||||
|
||||
hailstone( 27, true )
|
||||
|
||||
max_i, max_iter = 0, 0
|
||||
for i = 1, 100000 do
|
||||
num = hailstone( i, false )
|
||||
if num >= max_iter then
|
||||
max_i = i
|
||||
max_iter = num
|
||||
end
|
||||
end
|
||||
|
||||
print( string.format( "Needed %d iterations for the number %d.\n", max_iter, max_i ) )
|
||||
19
Task/Hailstone-sequence/MATLAB/hailstone-sequence-1.m
Normal file
19
Task/Hailstone-sequence/MATLAB/hailstone-sequence-1.m
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
function x = hailstone(n)
|
||||
% iterative definition
|
||||
global VERBOSE;
|
||||
x = 1;
|
||||
while (1)
|
||||
if VERBOSE,
|
||||
printf('%i ',n); % print element
|
||||
end;
|
||||
|
||||
if n==1,
|
||||
return;
|
||||
elseif mod(n,2),
|
||||
n = 3*n+1;
|
||||
else
|
||||
n = n/2;
|
||||
end;
|
||||
x = x + 1;
|
||||
end;
|
||||
end;
|
||||
4
Task/Hailstone-sequence/MATLAB/hailstone-sequence-2.m
Normal file
4
Task/Hailstone-sequence/MATLAB/hailstone-sequence-2.m
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
global VERBOSE;
|
||||
VERBOSE = 1; % display of sequence elements turned on
|
||||
N = hailstone(27); %display sequence
|
||||
printf('\n\n%i\n',N); %
|
||||
8
Task/Hailstone-sequence/MATLAB/hailstone-sequence-3.m
Normal file
8
Task/Hailstone-sequence/MATLAB/hailstone-sequence-3.m
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
global VERBOSE;
|
||||
VERBOSE = 0; % display of sequence elements turned off
|
||||
N = 100000;
|
||||
M = zeros(N,1);
|
||||
for k=1:N,
|
||||
M(k) = hailstone(k); %display sequence
|
||||
end;
|
||||
[maxLength, n] = max(M)
|
||||
6
Task/Hailstone-sequence/MUMPS/hailstone-sequence.mumps
Normal file
6
Task/Hailstone-sequence/MUMPS/hailstone-sequence.mumps
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
hailstone(n) ;
|
||||
If n=1 Quit n
|
||||
If n#2 Quit n_" "_$$hailstone(3*n+1)
|
||||
Quit n_" "_$$hailstone(n\2)
|
||||
Set x=$$hailstone(27) Write !,$Length(x," ")," terms in ",x,!
|
||||
112 terms in 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
|
||||
|
|
@ -0,0 +1 @@
|
|||
HailstoneFP[n_] := Drop[FixedPointList[If[# != 1, Which[Mod[#, 2] == 0, #/2, True, ( 3*# + 1) ], 1] &, n], -1]
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
HailstoneR[1] := {1}
|
||||
HailstoneR[n_Integer] := Prepend[HailstoneR[3 n + 1], n] /; OddQ[n] && n > 0
|
||||
HailstoneR[n_Integer] := Prepend[HailstoneR[n/2], n] /; EvenQ[n] && n > 0
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
Hailstone[n_] :=
|
||||
NestWhileList[Which[Mod[#, 2] == 0, #/2, True, ( 3*# + 1) ] &, n, # != 1 &];
|
||||
c27 = Hailstone@27;
|
||||
Print["Hailstone sequence for n = 27: [", c27[[;; 4]], "...", c27[[-4 ;;]], "]"]
|
||||
Print["Length Hailstone[27] = ", Length@c27]
|
||||
|
||||
longest = -1; comp = 0;
|
||||
Do[temp = Length@Hailstone@i;
|
||||
If[comp < temp, comp = temp; longest = i],
|
||||
{i, 100000}
|
||||
]
|
||||
Print["Longest Hailstone sequence at n = ", longest, "\nwith length = ", comp];
|
||||
15
Task/Hailstone-sequence/Maxima/hailstone-sequence.maxima
Normal file
15
Task/Hailstone-sequence/Maxima/hailstone-sequence.maxima
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
collatz(n) := block([L], L: [n], while n > 1 do
|
||||
(n: if evenp(n) then n/2 else 3*n + 1, L: endcons(n, L)), L)$
|
||||
|
||||
collatz_length(n) := block([m], m: 1, while n > 1 do
|
||||
(n: if evenp(n) then n/2 else 3*n + 1, m: m + 1), m)$
|
||||
|
||||
collatz_max(n) := block([j, m, p], m: 0,
|
||||
for i from 1 thru n do
|
||||
(p: collatz_length(i), if p > m then (m: p, j: i)),
|
||||
[j, m])$
|
||||
|
||||
collatz(27); /* [27, 82, 41, ..., 4, 2, 1] */
|
||||
length(%); /* 112 */
|
||||
collatz_length(27); /* 112 */
|
||||
collatz_max(100000); /* [77031, 351] */
|
||||
73
Task/Hailstone-sequence/Modula-2/hailstone-sequence.mod2
Normal file
73
Task/Hailstone-sequence/Modula-2/hailstone-sequence.mod2
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
MODULE hailst;
|
||||
|
||||
IMPORT InOut;
|
||||
|
||||
CONST maxCard = MAX (CARDINAL) DIV 3;
|
||||
TYPE action = (List, Count, Max);
|
||||
VAR a : CARDINAL;
|
||||
|
||||
PROCEDURE HailStone (start : CARDINAL; type : action) : CARDINAL;
|
||||
|
||||
VAR n, max, count : CARDINAL;
|
||||
|
||||
BEGIN
|
||||
count := 1;
|
||||
n := start;
|
||||
max := n;
|
||||
LOOP
|
||||
IF type = List THEN
|
||||
InOut.WriteCard (n, 12);
|
||||
IF count MOD 6 = 0 THEN InOut.WriteLn 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
|
||||
InOut.WriteString ("Exceeding max value for type CARDINAL at count = ");
|
||||
InOut.WriteCard (count, 10);
|
||||
InOut.WriteString (" for intermediate value ");
|
||||
InOut.WriteCard (n, 10);
|
||||
InOut.WriteString (". Aborting.");
|
||||
HALT
|
||||
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 : CARDINAL);
|
||||
|
||||
VAR val, maxCount, maxVal, cnt : CARDINAL;
|
||||
|
||||
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;
|
||||
InOut.WriteString ("Longest sequence below "); InOut.WriteCard (num, 1);
|
||||
InOut.WriteString (" is "); InOut.WriteCard (HailStone (maxVal, Count), 1);
|
||||
InOut.WriteString (" for n = "); InOut.WriteCard (maxVal, 1);
|
||||
InOut.WriteString (" with an intermediate maximum of ");
|
||||
InOut.WriteCard (HailStone (maxVal, Max), 1);
|
||||
InOut.WriteLn
|
||||
END FindMax;
|
||||
|
||||
BEGIN
|
||||
a := HailStone (27, List);
|
||||
InOut.WriteLn;
|
||||
InOut.WriteString ("Iterations total = "); InOut.WriteCard (HailStone (27, Count), 12);
|
||||
InOut.WriteString (" max value = "); InOut.WriteCard (HailStone (27, Max) , 12);
|
||||
InOut.WriteLn;
|
||||
FindMax (100000);
|
||||
InOut.WriteString ("Done."); InOut.WriteLn
|
||||
END hailst.
|
||||
26
Task/Hailstone-sequence/PHP/hailstone-sequence.php
Normal file
26
Task/Hailstone-sequence/PHP/hailstone-sequence.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
function hailstone($n,$seq=array()){
|
||||
$sequence = $seq;
|
||||
$sequence[] = $n;
|
||||
if($n == 1){
|
||||
return $sequence;
|
||||
}else{
|
||||
$n = ($n%2==0) ? $n/2 : (3*$n)+1;
|
||||
return hailstone($n, $sequence);
|
||||
}
|
||||
}
|
||||
|
||||
$result = hailstone(27);
|
||||
echo count($result) . ' Elements.<br>';
|
||||
echo 'Starting with : ' . implode(",",array_slice($result,0,4)) .' and ending with : ' . implode(",",array_slice($result,count($result)-4)) . '<br>';
|
||||
|
||||
$maxResult = array(0);
|
||||
|
||||
for($i=1;$i<=100000;$i++){
|
||||
$result = count(hailstone($i));
|
||||
if($result > max($maxResult)){
|
||||
$maxResult = array($i=>$result);
|
||||
}
|
||||
}
|
||||
foreach($maxResult as $key => $val){
|
||||
echo 'Number < 100000 with longest Hailstone seq.: ' . $key . ' with length of ' . $val;
|
||||
}
|
||||
37
Task/Hailstone-sequence/Perl/hailstone-sequence-1.pl
Normal file
37
Task/Hailstone-sequence/Perl/hailstone-sequence-1.pl
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
use warnings;
|
||||
use strict;
|
||||
|
||||
my @h = hailstone(27);
|
||||
print "Length of hailstone(27) = " . scalar @h . "\n";
|
||||
print "[" . join(", ", @h[0 .. 3], "...", @h[-4 .. -1]) . "]\n";
|
||||
|
||||
my ($max, $n) = (0, 0);
|
||||
for my $x (1 .. 99_999) {
|
||||
@h = hailstone($x);
|
||||
if (scalar @h > $max) {
|
||||
($max, $n) = (scalar @h, $x);
|
||||
}
|
||||
}
|
||||
|
||||
print "Max length $max was found for hailstone($n) for numbers < 100_000\n";
|
||||
|
||||
|
||||
sub hailstone {
|
||||
my ($n) = @_;
|
||||
|
||||
my @sequence = ($n);
|
||||
|
||||
while ($n > 1) {
|
||||
if ($n % 2 == 0) {
|
||||
$n = int($n / 2);
|
||||
} else {
|
||||
$n = $n * 3 + 1;
|
||||
}
|
||||
|
||||
push @sequence, $n;
|
||||
}
|
||||
|
||||
return @sequence;
|
||||
}
|
||||
15
Task/Hailstone-sequence/Perl/hailstone-sequence-2.pl
Normal file
15
Task/Hailstone-sequence/Perl/hailstone-sequence-2.pl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/perl
|
||||
use strict;
|
||||
|
||||
sub hailstone {
|
||||
@_ = local $_ = shift;
|
||||
push @_, $_ = $_ % 2 ? 3 * $_ + 1 : $_ / 2 while $_ > 1;
|
||||
@_;
|
||||
}
|
||||
|
||||
my @h = hailstone($_ = 27);
|
||||
print "$_: @h[0 .. 3] ... @h[-4 .. -1] (".@h.")\n";
|
||||
|
||||
@h = ();
|
||||
for (1 .. 99_999) { @h = ($_, $h[2]) if ($h[2] = hailstone($_)) > $h[1] }
|
||||
printf "%d: (%d)\n", @h;
|
||||
3
Task/Hailstone-sequence/Perl/hailstone-sequence-3.pl
Normal file
3
Task/Hailstone-sequence/Perl/hailstone-sequence-3.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
sub _{my$_=$_[''];push@_,$_&1?$_+=$_++<<1:($_>>=1)while$_^1;@_}
|
||||
@_=_($_=031^2);print "$_: @_[0..3] ... @_[-4..-1] (".@_.")\n";
|
||||
$_[1]<($_[2]=_($_))and@_=($_,$_[2])for 1..1e5-1;printf "%d: (%d)\n", @_;
|
||||
13
Task/Hailstone-sequence/PicoLisp/hailstone-sequence.l
Normal file
13
Task/Hailstone-sequence/PicoLisp/hailstone-sequence.l
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(de hailstone (N)
|
||||
(make
|
||||
(until (= 1 (link N))
|
||||
(setq N
|
||||
(if (bit? 1 N)
|
||||
(inc (* N 3))
|
||||
(/ N 2) ) ) ) ) )
|
||||
|
||||
(let L (hailstone 27)
|
||||
(println 27 (length L) (head 4 L) '- (tail 4 L)) )
|
||||
|
||||
(let N (maxi '((N) (length (hailstone N))) (range 1 100000))
|
||||
(println N (length (hailstone N))) )
|
||||
3
Task/Hailstone-sequence/Prolog/hailstone-sequence-1.pro
Normal file
3
Task/Hailstone-sequence/Prolog/hailstone-sequence-1.pro
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
hailstone(1,[1]) :- !.
|
||||
hailstone(N,[N|S]) :- 0 is N mod 2, N1 is N / 2, hailstone(N1,S).
|
||||
hailstone(N,[N|S]) :- 1 is N mod 2, N1 is (3 * N) + 1, hailstone(N1, S).
|
||||
4
Task/Hailstone-sequence/Prolog/hailstone-sequence-2.pro
Normal file
4
Task/Hailstone-sequence/Prolog/hailstone-sequence-2.pro
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
hailstone(27,X),
|
||||
length(X,112),
|
||||
append([27, 82, 41, 124], _, X),
|
||||
append(_, [8, 4, 2, 1], X).
|
||||
10
Task/Hailstone-sequence/Prolog/hailstone-sequence-3.pro
Normal file
10
Task/Hailstone-sequence/Prolog/hailstone-sequence-3.pro
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
longestHailstoneSequence(M, Seq, Len) :- longesthailstone(M, 1, 1, Seq, Len).
|
||||
longesthailstone(1, Cn, Cl, Mn, Ml):- Mn = Cn,
|
||||
Ml = Cl.
|
||||
longesthailstone(N, _, Cl, Mn, Ml) :- hailstone(N, X),
|
||||
length(X, L),
|
||||
Cl < L,
|
||||
N1 is N-1,
|
||||
longesthailstone(N1, N, L, Mn, Ml).
|
||||
longesthailstone(N, Cn, Cl, Mn, Ml) :- N1 is N-1,
|
||||
longesthailstone(N1, Cn, Cl, Mn, Ml).
|
||||
1
Task/Hailstone-sequence/Prolog/hailstone-sequence-4.pro
Normal file
1
Task/Hailstone-sequence/Prolog/hailstone-sequence-4.pro
Normal file
|
|
@ -0,0 +1 @@
|
|||
longestHailstoneSequence(100000, Seq, Len).
|
||||
17
Task/Hailstone-sequence/Prolog/hailstone-sequence-5.pro
Normal file
17
Task/Hailstone-sequence/Prolog/hailstone-sequence-5.pro
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
:- use_module(library(chr)).
|
||||
:- chr_option(debug, off).
|
||||
:- chr_option(optimize, full).
|
||||
|
||||
:- chr_constraint collatz/2, hailstone/1, clean/0.
|
||||
|
||||
% to remove all constraints hailstone/1 after computation
|
||||
clean @ clean \ hailstone(_) <=> true.
|
||||
clean @ clean <=> true.
|
||||
|
||||
% compute Collatz number
|
||||
init @ collatz(1,X) <=> X = 1 | true.
|
||||
collatz @ collatz(N, C) <=> (N mod 2 =:= 0 -> C is N / 2; C is 3 * N + 1).
|
||||
|
||||
% Hailstone loop
|
||||
hailstone(1) ==> true.
|
||||
hailstone(N) ==> N \= 1 | collatz(N, H), hailstone(H).
|
||||
6
Task/Hailstone-sequence/Prolog/hailstone-sequence-6.pro
Normal file
6
Task/Hailstone-sequence/Prolog/hailstone-sequence-6.pro
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
task1 :-
|
||||
hailstone(27),
|
||||
findall(X, find_chr_constraint(hailstone(X)), L),
|
||||
clean,
|
||||
% check the requirements
|
||||
( (length(L, 112), append([27, 82, 41, 124 | _], [8,4,2,1], L)) -> writeln(ok); writeln(ko)).
|
||||
22
Task/Hailstone-sequence/Prolog/hailstone-sequence-7.pro
Normal file
22
Task/Hailstone-sequence/Prolog/hailstone-sequence-7.pro
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
longest_sequence :-
|
||||
seq(2, 100000, 1-[1], Len-V),
|
||||
format('For ~w sequence has ~w len ! ~n', [V, Len]).
|
||||
|
||||
|
||||
% walk through 2 to 100000 and compute the length of the sequences
|
||||
% memorize the longest
|
||||
seq(N, Max, Len-V, Len-V) :- N is Max + 1, !.
|
||||
seq(N, Max, CLen - CV, FLen - FV) :-
|
||||
len_seq(N, Len - N),
|
||||
( Len > CLen -> Len1 = Len, V1 = [N]
|
||||
; Len = CLen -> Len1 = Len, V1 = [N | CV]
|
||||
; Len1 = CLen, V1 = CV),
|
||||
N1 is N+1,
|
||||
seq(N1, Max, Len1 - V1, FLen - FV).
|
||||
|
||||
% compute the len of the Hailstone sequence for a number
|
||||
len_seq(N, Len - N) :-
|
||||
hailstone(N),
|
||||
findall(hailstone(X), find_chr_constraint(hailstone(X)), L),
|
||||
length(L, Len),
|
||||
clean.
|
||||
12
Task/Hailstone-sequence/Python/hailstone-sequence.py
Normal file
12
Task/Hailstone-sequence/Python/hailstone-sequence.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
def hailstone(n):
|
||||
seq = [n]
|
||||
while n>1:
|
||||
n = 3*n + 1 if n & 1 else n//2
|
||||
seq.append(n)
|
||||
return seq
|
||||
|
||||
if __name__ == '__main__':
|
||||
h = hailstone(27)
|
||||
assert len(h)==112 and h[:4]==[27, 82, 41, 124] and h[-4:]==[8, 4, 2, 1]
|
||||
print("Maximum length %i was found for hailstone(%i) for numbers <100,000" %
|
||||
max((len(hailstone(i)), i) for i in range(1,100000)))
|
||||
34
Task/Hailstone-sequence/R/hailstone-sequence-1.r
Normal file
34
Task/Hailstone-sequence/R/hailstone-sequence-1.r
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
### PART 1:
|
||||
makeHailstone <- function(n){
|
||||
hseq <- n
|
||||
while (hseq[length(hseq)] > 1){
|
||||
current.value <- hseq[length(hseq)]
|
||||
if (current.value %% 2 == 0){
|
||||
next.value <- current.value / 2
|
||||
} else {
|
||||
next.value <- (3 * current.value) + 1
|
||||
}
|
||||
hseq <- append(hseq, next.value)
|
||||
}
|
||||
return(list(hseq=hseq, seq.length=length(hseq)))
|
||||
}
|
||||
|
||||
### PART 2:
|
||||
twenty.seven <- makeHailstone(27)
|
||||
twenty.seven$hseq
|
||||
twenty.seven$seq.length
|
||||
|
||||
### PART 3:
|
||||
max.length <- 0; lower.bound <- 1; upper.bound <- 100000
|
||||
|
||||
for (index in lower.bound:upper.bound){
|
||||
current.hseq <- makeHailstone(index)
|
||||
if (current.hseq$seq.length > max.length){
|
||||
max.length <- current.hseq$seq.length
|
||||
max.index <- index
|
||||
}
|
||||
}
|
||||
|
||||
cat("Between ", lower.bound, " and ", upper.bound, ", the input of ",
|
||||
max.index, " gives the longest hailstone sequence, which has length ",
|
||||
max.length, ". \n", sep="")
|
||||
15
Task/Hailstone-sequence/R/hailstone-sequence-2.r
Normal file
15
Task/Hailstone-sequence/R/hailstone-sequence-2.r
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
> twenty.seven$hseq
|
||||
[1] 27 82 41 124 62 31 94 47 142 71 214 107 322 161 484
|
||||
[16] 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700
|
||||
[31] 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167
|
||||
[46] 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438
|
||||
[61] 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051
|
||||
[76] 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488
|
||||
[91] 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20
|
||||
[106] 10 5 16 8 4 2 1
|
||||
|
||||
> twenty.seven$seq.length
|
||||
[1] 112
|
||||
|
||||
Between 1 and 1e+05, the input of 77031 gives the longest hailstone sequence,
|
||||
which has length 351.
|
||||
24
Task/Hailstone-sequence/REXX/hailstone-sequence.rexx
Normal file
24
Task/Hailstone-sequence/REXX/hailstone-sequence.rexx
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/*REXX pgm tests a number and a range for hailstone (Collatz) sequences.*/
|
||||
parse arg x .; if x=='' then x=27 /*get the optional first argument*/
|
||||
numeric digits 20
|
||||
$=hailstone(x) /*═════════════task 1════════════*/
|
||||
say x 'has a hailstone sequence of' #hs 'and starts with: ' subword($,1,4),
|
||||
' and ends with:' subword($,#hs-3)
|
||||
say
|
||||
w=0; do j=1 for 99999 /*═════════════task 2════════════*/
|
||||
call hailstone j /*compute the hailstone sequence.*/
|
||||
if #hs<=w then iterate /*Not big 'nuff? Then keep going.*/
|
||||
bigJ=j; w=#hs /*remember what # has biggest HS.*/
|
||||
end /*j*/
|
||||
|
||||
say '(between 1──►99,999) ' bigJ 'has the longest hailstone sequence:' w
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────HAILSTONE subroutine────────────────*/
|
||||
hailstone: procedure expose #hs; parse arg n 1 s /*N & S set to 1st arg*/
|
||||
|
||||
do #hs=1 while n\==1 /*loop while N isn't unity. */
|
||||
if n//2 then n=n*3+1 /*if N is odd, calc: 3*n +1 */
|
||||
else n=n%2 /* " " " even, perform fast ÷ */
|
||||
s=s n /*build a sequence list (append).*/
|
||||
end /*#hs*/
|
||||
return s
|
||||
19
Task/Hailstone-sequence/Racket/hailstone-sequence.rkt
Normal file
19
Task/Hailstone-sequence/Racket/hailstone-sequence.rkt
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#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 h27 (first (hailstone 27)))
|
||||
|
||||
(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))))
|
||||
18
Task/Hailstone-sequence/Ruby/hailstone-sequence-1.rb
Normal file
18
Task/Hailstone-sequence/Ruby/hailstone-sequence-1.rb
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
def hailstone n
|
||||
seq = [n]
|
||||
until n == 1
|
||||
n = (n.even?) ? (n / 2) : (3 * n + 1)
|
||||
seq << n
|
||||
end
|
||||
seq
|
||||
end
|
||||
|
||||
# for n = 27, show sequence length and first and last 4 elements
|
||||
hs27 = hailstone 27
|
||||
p [hs27.length, hs27[0..3], hs27[-4..-1]]
|
||||
|
||||
# find the longest sequence among n less than 100,000
|
||||
n, len = (1 ... 100_000) .collect {|n|
|
||||
[n, hailstone(n).length]} .max_by {|n, len| len}
|
||||
puts "#{n} has a hailstone sequence length of #{len}"
|
||||
puts "the largest number in that sequence is #{hailstone(n).max}"
|
||||
47
Task/Hailstone-sequence/Ruby/hailstone-sequence-2.rb
Normal file
47
Task/Hailstone-sequence/Ruby/hailstone-sequence-2.rb
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
module Hailstone
|
||||
class ListNode
|
||||
include Enumerable
|
||||
attr_reader :value, :size, :succ
|
||||
|
||||
def initialize(value, size, succ=nil)
|
||||
@value, @size, @succ = value, size, succ
|
||||
end
|
||||
|
||||
def each
|
||||
node = self
|
||||
while node
|
||||
yield node.value
|
||||
node = node.succ
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@sequence = {1 => ListNode.new(1, 1)}
|
||||
|
||||
module_function
|
||||
|
||||
def sequence(n)
|
||||
unless @@sequence[n]
|
||||
ary = []
|
||||
m = n
|
||||
until succ = @@sequence[m]
|
||||
ary << m
|
||||
m = (m.even?) ? (m / 2) : (3 * m + 1)
|
||||
end
|
||||
ary.reverse_each do |m|
|
||||
@@sequence[m] = succ = ListNode.new(m, succ.size + 1, succ)
|
||||
end
|
||||
end
|
||||
@@sequence[n]
|
||||
end
|
||||
end
|
||||
|
||||
# for n = 27, show sequence length and first and last 4 elements
|
||||
hs27 = Hailstone.sequence(27).to_a
|
||||
p [hs27.length, hs27[0..3], hs27[-4..-1]]
|
||||
|
||||
# find the longest sequence among n less than 100,000
|
||||
hs_big = (1 ... 100_000) .collect {|n|
|
||||
Hailstone.sequence n}.max_by {|hs| hs.size}
|
||||
puts "#{hs_big.first} has a hailstone sequence length of #{hs_big.size}"
|
||||
puts "the largest number in that sequence is #{hs_big.max}"
|
||||
11
Task/Hailstone-sequence/Scala/hailstone-sequence.scala
Normal file
11
Task/Hailstone-sequence/Scala/hailstone-sequence.scala
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def hailstone(n: Int): Stream[Int] =
|
||||
n #:: {
|
||||
if (n == 1) Stream.empty
|
||||
else hailstone(if (n % 2 == 0) n / 2 else n * 3 + 1)
|
||||
}
|
||||
|
||||
def main(args: Array[String]) {
|
||||
println(hailstone(27).toList)
|
||||
val (n, len) = (1 to 100000).map(n => (n, hailstone(n).length)).maxBy(_._2)
|
||||
println("value=" + n + " len=" + len)
|
||||
}
|
||||
28
Task/Hailstone-sequence/Scheme/hailstone-sequence.ss
Normal file
28
Task/Hailstone-sequence/Scheme/hailstone-sequence.ss
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
(define (collatz n)
|
||||
(if (= n 1) '(1)
|
||||
(cons n (collatz (if (even? n) (/ n 2) (+ 1 (* 3 n)))))))
|
||||
|
||||
(define (collatz-length n)
|
||||
(let aux ((n n) (r 1)) (if (= n 1) r
|
||||
(aux (if (even? n) (/ n 2) (+ 1 (* 3 n))) (+ r 1)))))
|
||||
|
||||
(define (collatz-max a b)
|
||||
(let aux ((i a) (j 0) (k 0))
|
||||
(if (> i b) (list j k)
|
||||
(let ((h (collatz-length i)))
|
||||
(if (> h k) (aux (+ i 1) i h) (aux (+ i 1) j k))))))
|
||||
|
||||
(collatz 27)
|
||||
; (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)
|
||||
|
||||
(collatz-length 27)
|
||||
; 112
|
||||
|
||||
(collatz-max 1 100000)
|
||||
; (77031 351)
|
||||
23
Task/Hailstone-sequence/Smalltalk/hailstone-sequence-1.st
Normal file
23
Task/Hailstone-sequence/Smalltalk/hailstone-sequence-1.st
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
Object subclass: Sequences [
|
||||
Sequences class >> hailstone: n [
|
||||
|seq|
|
||||
seq := OrderedCollection new.
|
||||
seq add: n.
|
||||
(n = 1) ifTrue: [ ^seq ].
|
||||
(n even) ifTrue: [ seq addAll: (Sequences hailstone: (n / 2)) ]
|
||||
ifFalse: [ seq addAll: (Sequences hailstone: ( (3*n) + 1 ) ) ].
|
||||
^seq.
|
||||
]
|
||||
|
||||
Sequences class >> hailstoneCount: n [
|
||||
^ (Sequences hailstoneCount: n num: 1)
|
||||
]
|
||||
|
||||
"this 'version' avoids storing the sequence, it just counts
|
||||
its length - no memoization anyway"
|
||||
Sequences class >> hailstoneCount: n num: m [
|
||||
(n = 1) ifTrue: [ ^m ].
|
||||
(n even) ifTrue: [ ^ Sequences hailstoneCount: (n / 2) num: (m + 1) ]
|
||||
ifFalse: [ ^ Sequences hailstoneCount: ( (3*n) + 1) num: (m + 1) ].
|
||||
]
|
||||
].
|
||||
20
Task/Hailstone-sequence/Smalltalk/hailstone-sequence-2.st
Normal file
20
Task/Hailstone-sequence/Smalltalk/hailstone-sequence-2.st
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
|r|
|
||||
r := Sequences hailstone: 27. "hailstone 'from' 27"
|
||||
(r size) displayNl. "its length"
|
||||
|
||||
"test 'head' ..."
|
||||
( (r first: 4) = #( 27 82 41 124 ) asOrderedCollection ) displayNl.
|
||||
|
||||
"... and 'tail'"
|
||||
( ( (r last: 4 ) ) = #( 8 4 2 1 ) asOrderedCollection) displayNl.
|
||||
|
||||
|longest|
|
||||
longest := OrderedCollection from: #( 1 1 ).
|
||||
2 to: 100000 do: [ :c |
|
||||
|l|
|
||||
l := Sequences hailstoneCount: c.
|
||||
(l > (longest at: 2) ) ifTrue: [ longest replaceFrom: 1 to: 2 with: { c . l } ].
|
||||
].
|
||||
|
||||
('Sequence generator %1, sequence length %2' % { (longest at: 1) . (longest at: 2) })
|
||||
displayNl.
|
||||
19
Task/Hailstone-sequence/Tcl/hailstone-sequence.tcl
Normal file
19
Task/Hailstone-sequence/Tcl/hailstone-sequence.tcl
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
proc hailstone n {
|
||||
while 1 {
|
||||
lappend seq $n
|
||||
if {$n == 1} {return $seq}
|
||||
set n [expr {$n & 1 ? $n*3+1 : $n/2}]
|
||||
}
|
||||
}
|
||||
|
||||
set h27 [hailstone 27]
|
||||
puts "h27 len=[llength $h27]"
|
||||
puts "head4 = [lrange $h27 0 3]"
|
||||
puts "tail4 = [lrange $h27 end-3 end]"
|
||||
|
||||
set maxlen [set max 0]
|
||||
for {set i 1} {$i<100000} {incr i} {
|
||||
set l [llength [hailstone $i]]
|
||||
if {$l>$maxlen} {set maxlen $l;set max $i}
|
||||
}
|
||||
puts "max is $max, with length $maxlen"
|
||||
Loading…
Add table
Add a link
Reference in a new issue