Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
58
Task/Hailstone-sequence/Aime/hailstone-sequence.aime
Normal file
58
Task/Hailstone-sequence/Aime/hailstone-sequence.aime
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
void
|
||||
print_hailstone(integer h)
|
||||
{
|
||||
list l;
|
||||
|
||||
while (h ^ 1) {
|
||||
lb_p_integer(l, h);
|
||||
h = __hold(h & 1, 3 * h + 1, h / 2);
|
||||
}
|
||||
|
||||
o_form("hailstone sequence for ~ is ~1 ~ ~ ~ .. ~ ~ ~ ~, it is ~ long\n",
|
||||
l[0], l[1], l[2], l[3], l[-3], l[-2], l[-1], 1, l_length(l) + 1);
|
||||
}
|
||||
|
||||
void
|
||||
max_hailstone(integer x)
|
||||
{
|
||||
integer e, i, m;
|
||||
record r;
|
||||
|
||||
m = 0;
|
||||
i = 1;
|
||||
while (i < x) {
|
||||
integer h, k, l;
|
||||
|
||||
h = i;
|
||||
l = 1;
|
||||
while (h ^ 1) {
|
||||
if (r_j_integer(k, r, itoa(h))) {
|
||||
l += k;
|
||||
break;
|
||||
} else {
|
||||
l += 1;
|
||||
h = __hold(h & 1, 3 * h + 1, h / 2);
|
||||
}
|
||||
}
|
||||
|
||||
r_f_integer(r, itoa(i), l - 1);
|
||||
|
||||
if (m < l) {
|
||||
m = l;
|
||||
e = i;
|
||||
}
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
o_form("hailstone sequence length for ~ is ~ long\n", e, m);
|
||||
}
|
||||
|
||||
integer
|
||||
main(void)
|
||||
{
|
||||
print_hailstone(27);
|
||||
max_hailstone(100000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,4 +1,18 @@
|
|||
&>:.:1-|
|
||||
>3*^ @
|
||||
|%2: <
|
||||
v>2/>+
|
||||
93*:. v
|
||||
> :2%v >
|
||||
v+1*3_2/
|
||||
>" ",:.v v<
|
||||
<v v-1:< <
|
||||
+1\_$1+v^ \
|
||||
v .,+94<>^>::v
|
||||
>" "03pv :* p
|
||||
v67:" "< 0: 1
|
||||
>p78p25 *^*p0
|
||||
v!-1: <<*^<
|
||||
9$_:0\ ^-^< v
|
||||
v01g00:< 1 4
|
||||
>g"@"*+`v^ <+
|
||||
v01/"@":_ $ ^,
|
||||
>p"@"%00p\$:^.
|
||||
vg01g00 ,+49<
|
||||
>"@"*+.@
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import std.stdio, std.algorithm, std.range, std.typecons;
|
|||
auto hailstone(uint n) pure nothrow {
|
||||
auto result = [n];
|
||||
while (n != 1) {
|
||||
n = n & 1 ? n*3 + 1 : n/2;
|
||||
n = (n & 1) ? (n * 3 + 1) : (n / 2);
|
||||
result ~= n;
|
||||
}
|
||||
return result;
|
||||
|
|
|
|||
|
|
@ -1,23 +1,20 @@
|
|||
import std.stdio, std.algorithm, std.range, std.typecons;
|
||||
import std.stdio, std.algorithm, std.typecons, std.range;
|
||||
|
||||
struct Hailstone {
|
||||
uint n;
|
||||
bool empty() const pure nothrow { return n == 0; }
|
||||
uint front() const pure nothrow { return n; }
|
||||
void popFront() pure nothrow {
|
||||
n = n == 1 ? 0 : (n & 1 ? n*3 + 1 : n/2);
|
||||
}
|
||||
auto hailstone(uint m) pure nothrow @nogc {
|
||||
return m
|
||||
.recurrence!q{ a[n - 1] & 1 ? a[n - 1] * 3 + 1 : a[n - 1]/2}
|
||||
.until!q{ a == 1 }(OpenRight.no);
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum M = 27;
|
||||
immutable h = M.Hailstone.array;
|
||||
immutable h = M.hailstone.array;
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$ - 4 .. $]);
|
||||
writeln("Length hailstone(", M, ")= ", h.length);
|
||||
|
||||
enum N = 100_000;
|
||||
immutable p = iota(1, N)
|
||||
.map!(i => tuple(i.Hailstone.walkLength, i))
|
||||
.map!(i => tuple(i.hailstone.walkLength, i))
|
||||
.reduce!max;
|
||||
writeln("Longest sequence in [1,", N, "]= ",p[1]," with len ",p[0]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,33 +1,23 @@
|
|||
import std.stdio, std.algorithm, std.range, std.typecons;
|
||||
|
||||
struct Hailstone(size_t cacheSize = 500_000) {
|
||||
size_t n;
|
||||
__gshared static size_t[cacheSize] cache;
|
||||
|
||||
bool empty() const pure nothrow { return n == 0; }
|
||||
size_t front() const pure nothrow { return n; }
|
||||
|
||||
void popFront() nothrow {
|
||||
if (n >= cacheSize) {
|
||||
n = n == 1 ? 0 : (n & 1 ? n*3 + 1 : n/2);
|
||||
} else if (cache[n]) {
|
||||
n = cache[n];
|
||||
} else {
|
||||
immutable n2 = n == 1 ? 0 : (n & 1 ? n*3 + 1 : n/2);
|
||||
n = cache[n] = n2;
|
||||
}
|
||||
struct Hailstone {
|
||||
uint n;
|
||||
bool empty() const pure nothrow @nogc { return n == 0; }
|
||||
uint front() const pure nothrow @nogc { return n; }
|
||||
void popFront() pure nothrow @nogc {
|
||||
n = n == 1 ? 0 : (n & 1 ? (n * 3 + 1) : n / 2);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio, std.algorithm, std.range, std.typecons;
|
||||
|
||||
enum M = 27;
|
||||
const h = M.Hailstone!().array;
|
||||
immutable h = M.Hailstone.array;
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$ - 4 .. $]);
|
||||
writeln("Length hailstone(", M, ")= ", h.length);
|
||||
|
||||
enum N = 100_000;
|
||||
immutable p = iota(1, N)
|
||||
.map!(i => tuple(i.Hailstone!().walkLength, i))
|
||||
.map!(i => tuple(i.Hailstone.walkLength, i))
|
||||
.reduce!max;
|
||||
writeln("Longest sequence in [1,", N, "]= ",p[1]," with len ",p[0]);
|
||||
}
|
||||
|
|
|
|||
33
Task/Hailstone-sequence/D/hailstone-sequence-4.d
Normal file
33
Task/Hailstone-sequence/D/hailstone-sequence-4.d
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import std.stdio, std.algorithm, std.range, std.typecons;
|
||||
|
||||
struct Hailstone(size_t cacheSize = 500_000) {
|
||||
size_t n;
|
||||
__gshared static size_t[cacheSize] cache;
|
||||
|
||||
bool empty() const pure nothrow @nogc { return n == 0; }
|
||||
size_t front() const pure nothrow @nogc { return n; }
|
||||
|
||||
void popFront() nothrow {
|
||||
if (n >= cacheSize) {
|
||||
n = n == 1 ? 0 : (n & 1 ? n*3 + 1 : n/2);
|
||||
} else if (cache[n]) {
|
||||
n = cache[n];
|
||||
} else {
|
||||
immutable n2 = n == 1 ? 0 : (n & 1 ? n*3 + 1 : n/2);
|
||||
n = cache[n] = n2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum M = 27;
|
||||
const h = M.Hailstone!().array;
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$ - 4 .. $]);
|
||||
writeln("Length hailstone(", M, ")= ", h.length);
|
||||
|
||||
enum N = 100_000;
|
||||
immutable p = iota(1, N)
|
||||
.map!(i => tuple(i.Hailstone!().walkLength, i))
|
||||
.reduce!max;
|
||||
writeln("Longest sequence in [1,", N, "]= ",p[1]," with len ",p[0]);
|
||||
}
|
||||
24
Task/Hailstone-sequence/D/hailstone-sequence-5.d
Normal file
24
Task/Hailstone-sequence/D/hailstone-sequence-5.d
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import std.stdio, std.algorithm, std.range, std.typecons, std.concurrency;
|
||||
|
||||
auto hailstone(size_t n) {
|
||||
return new Generator!size_t({
|
||||
yield(n);
|
||||
while (n > 1) {
|
||||
n = (n & 1) ? (3 * n + 1) : (n / 2);
|
||||
yield(n);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum M = 27;
|
||||
const h = M.hailstone.array;
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$ - 4 .. $]);
|
||||
writeln("Length hailstone(", M, ")= ", h.length);
|
||||
|
||||
enum N = 100_000;
|
||||
immutable p = iota(1, N)
|
||||
.map!(i => tuple(i.hailstone.walkLength, i))
|
||||
.reduce!max;
|
||||
writeln("Longest sequence in [1,", N, "]= ",p[1]," with len ",p[0]);
|
||||
}
|
||||
|
|
@ -10,10 +10,11 @@ hailstone n | even n = n : hailstone (n `div` 2)
|
|||
withResult :: (t -> t1) -> t -> (t1, t)
|
||||
withResult f x = (f x, x)
|
||||
|
||||
main _ = do
|
||||
main :: IO ()
|
||||
main = do
|
||||
let h27 = hailstone 27
|
||||
printStrLn $ show $ length h27
|
||||
putStrLn $ 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)
|
||||
putStrLn ("hailstone 27: " ++ h4 ++ " ... " ++ t4)
|
||||
putStrLn $ show $ maximumBy (comparing fst) $ map (withResult (length . hailstone)) [1..100000]
|
||||
|
|
|
|||
94
Task/Hailstone-sequence/Pascal/hailstone-sequence.pascal
Normal file
94
Task/Hailstone-sequence/Pascal/hailstone-sequence.pascal
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
program ShowHailstoneSequence;
|
||||
{$IFDEF FPC}
|
||||
{$MODE delphi} //or objfpc
|
||||
{$Else}
|
||||
{$Apptype Console} // for delphi
|
||||
{$ENDIF}
|
||||
|
||||
uses
|
||||
SysUtils;// format
|
||||
type
|
||||
tIntArr = record
|
||||
iaAktPos : integer;
|
||||
iaMaxPos : integer;
|
||||
iaArr : array of integer;
|
||||
end;
|
||||
|
||||
procedure GetHailstoneSequence(aStartingNumber: Integer;var aHailstoneList: tIntArr);
|
||||
var
|
||||
n: UInt64;
|
||||
begin
|
||||
with aHailstoneList do
|
||||
begin
|
||||
iaAktPos := 0;
|
||||
iaArr[iaAktPos] := aStartingNumber;
|
||||
n := aStartingNumber;
|
||||
while n <> 1 do
|
||||
begin
|
||||
if Odd(n) then
|
||||
n := (3 * n) + 1
|
||||
else
|
||||
n := n div 2;
|
||||
inc(iaAktPos);
|
||||
IF iaAktPos>iaMaxPos then
|
||||
Begin
|
||||
iaMaxPos := round(iaMaxPos*1.62)+2;
|
||||
setlength(iaArr,iaMaxPos+1);
|
||||
end;
|
||||
iaArr[iaAktPos] := n;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
var
|
||||
i,Limit: Integer;
|
||||
lList: tIntArr;
|
||||
lMaxSequence: Integer;
|
||||
lMaxLength: Integer;
|
||||
begin
|
||||
try
|
||||
with lList do
|
||||
begin
|
||||
setlength(iaArr,0+1);
|
||||
iaMaxPos := 0;
|
||||
iaAktPos := 0;
|
||||
end;
|
||||
|
||||
GetHailstoneSequence(27, lList);
|
||||
with lList do
|
||||
begin
|
||||
i := iaAktPos+1;
|
||||
Writeln(Format('27: %d elements', [i]));
|
||||
Writeln(Format('[%d,%d,%d,%d ... %d,%d,%d,%d]',
|
||||
[iaArr[0], iaArr[1], iaArr[2], iaArr[3],
|
||||
iaArr[i - 4], iaArr[i - 3], iaArr[i - 2], iaArr[i - 1]]));
|
||||
Writeln;
|
||||
|
||||
lMaxSequence := 0;
|
||||
lMaxLength := 0;
|
||||
limit := 10;
|
||||
for i := 1 to 10000000 do
|
||||
begin
|
||||
GetHailstoneSequence(i, lList);
|
||||
if iaAktPos >= lMaxLength then
|
||||
begin
|
||||
IF i> limit then
|
||||
begin
|
||||
Writeln(Format('Longest sequence under %8d : %7d with %3d elements',
|
||||
[limit,lMaxSequence, lMaxLength]));
|
||||
limit := limit*10;
|
||||
end;
|
||||
lMaxSequence := i;
|
||||
lMaxLength := iaAktPos+1;
|
||||
end;
|
||||
end;
|
||||
Writeln(Format('Longest sequence under %8d : %7d with %3d elements',
|
||||
[limit,lMaxSequence, lMaxLength]));
|
||||
|
||||
end;
|
||||
finally
|
||||
setlength(lList.iaArr,0);
|
||||
end;
|
||||
writeln('game over, wait for >ENTER< ');
|
||||
Readln;
|
||||
end.
|
||||
|
|
@ -1,26 +1,27 @@
|
|||
# Author M. McNabb
|
||||
|
||||
function Get-HailStone {
|
||||
param($n)
|
||||
|
||||
switch($n) {
|
||||
1 {$n;return}
|
||||
{$n % 2 -eq 0} {$n; return Get-Hailstone ($n = $n / 2)}
|
||||
{$n % 2 -ne 0} {$n; return Get-Hailstone ($n = ($n * 3) +1)}
|
||||
}
|
||||
}
|
||||
|
||||
function Get-HailStoneBelowLimit {
|
||||
param($UpperLimit)
|
||||
begin {
|
||||
function Get-HailStone {
|
||||
param($n)
|
||||
switch($n) {
|
||||
1 {$n;return}
|
||||
{$n % 2 -eq 0} {$n; return Get-Hailstone ($n = $n / 2)}
|
||||
{$n % 2 -ne 0} {$n; return Get-Hailstone ($n = ($n * 3) +1)}
|
||||
}
|
||||
param($UpperLimit)
|
||||
|
||||
$Counts = @()
|
||||
|
||||
for ($i = 1; $i -lt $UpperLimit; $i++) {
|
||||
$Object = [pscustomobject]@{
|
||||
'Number' = $i
|
||||
'Count' = (Get-HailStone $i).count
|
||||
}
|
||||
$Counts = @()
|
||||
$Counts += $Object
|
||||
}
|
||||
|
||||
process {
|
||||
for ($i = 1; $i -lt $UpperLimit; $i++) {
|
||||
$Object = [pscustomobject]@{
|
||||
'Number' = $i
|
||||
'Count' = (Get-HailStone $i).count
|
||||
}
|
||||
$Counts += $Object
|
||||
}
|
||||
}
|
||||
end {$Counts}
|
||||
$Counts
|
||||
}
|
||||
|
|
|
|||
34
Task/Hailstone-sequence/R/hailstone-sequence.r
Normal file
34
Task/Hailstone-sequence/R/hailstone-sequence.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="")
|
||||
|
|
@ -8,7 +8,7 @@ say x ' has a hailstone sequence of ' words($)
|
|||
say ' and starts with: ' subword($, 1, 4) " ∙∙∙"
|
||||
say ' and ends with: ∙∙∙' subword($, max(1, words($)-3))
|
||||
say
|
||||
if y==0 then exit /*═══════════════════task 2═════════════════════════*/
|
||||
if y==0 then exit /*═══════════════════task 2═════════════════════════*/
|
||||
w=0; do j=1 for y /*traipse through the numbers. */
|
||||
call hailstone j /*compute the hailstone sequence.*/
|
||||
if #hs<=w then iterate /*Not big 'nuff? Then keep going.*/
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ say x ' has a hailstone sequence of ' words($)
|
|||
say ' and starts with: ' subword($, 1, 4) " ∙∙∙"
|
||||
say ' and ends with: ∙∙∙' subword($, max(1, words($)-3))
|
||||
say
|
||||
if y==0 then exit /*═══════════════════task 2═════════════════════════*/
|
||||
if y==0 then exit /*═══════════════════task 2═════════════════════════*/
|
||||
w=0; do j=1 for y /*loop through all numbers <100k.*/
|
||||
$=hailstone(j) /*compute the hailstone sequence.*/
|
||||
#hs=words($) /*find the length of the sequence*/
|
||||
|
|
@ -20,11 +20,11 @@ exit /*stick a fork in it, we're done.*/
|
|||
/*──────────────────────────────────HAILSTONE subroutine────────────────*/
|
||||
hailstone: procedure expose @.; parse arg n 1 s 1 o /*N,S,O = 1st arg.*/
|
||||
@.1= /*special case for unity. */
|
||||
do forever /*loop while N isn't unity. */
|
||||
if @.n\==0 then do; s=s @.n; leave; end /*been here before?*/
|
||||
do while @.n==0 /*loop while residual is unknown.*/
|
||||
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 /*forever*/
|
||||
end /*while*/
|
||||
s=s @.n /*append to a sequence list. */
|
||||
@.o=subword(s,2) /*memoization for this hailstone.*/
|
||||
return s
|
||||
|
|
|
|||
|
|
@ -7,12 +7,11 @@ def hailstone n
|
|||
seq
|
||||
end
|
||||
|
||||
# for n = 27, show sequence length and first and last 4 elements
|
||||
puts "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}"
|
||||
n = (1 ... 100_000).max_by{|n| hailstone(n).length}
|
||||
puts "#{n} has a hailstone sequence length of #{hailstone(n).length}"
|
||||
puts "the largest number in that sequence is #{hailstone(n).max}"
|
||||
|
|
|
|||
|
|
@ -1,47 +1,38 @@
|
|||
module Hailstone
|
||||
class ListNode
|
||||
include Enumerable
|
||||
attr_reader :value, :size, :succ
|
||||
|
||||
def initialize(value, size, succ=nil)
|
||||
@value, @size, @succ = value, size, succ
|
||||
end
|
||||
|
||||
ListNode = Struct.new(:value, :size, :succ) do
|
||||
def each
|
||||
node = self
|
||||
while node
|
||||
yield node.value
|
||||
node = node.succ
|
||||
yield node.value
|
||||
node = node.succ
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@sequence = {1 => ListNode.new(1, 1)}
|
||||
@@sequence = {1 => ListNode[1,1]}
|
||||
|
||||
module_function
|
||||
|
||||
def sequence(n)
|
||||
unless @@sequence[n]
|
||||
ary = []
|
||||
m = n
|
||||
m, ary = n, []
|
||||
until succ = @@sequence[m]
|
||||
ary << m
|
||||
m = (m.even?) ? (m / 2) : (3 * m + 1)
|
||||
m = m.even? ? (m / 2) : (3 * m + 1)
|
||||
end
|
||||
ary.reverse_each do |m|
|
||||
@@sequence[m] = succ = ListNode.new(m, succ.size + 1, succ)
|
||||
@@sequence[m] = succ = ListNode[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]]
|
||||
puts "for n = 27, show sequence length and first and last 4 elements"
|
||||
hs27 = Hailstone.sequence(27).entries
|
||||
p [hs27.size, 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}"
|
||||
n = (1 ... 100_000).max_by{|n| Hailstone.sequence(n).size}
|
||||
puts "#{n} has a hailstone sequence length of #{Hailstone.sequence(n).size}"
|
||||
puts "the largest number in that sequence is #{Hailstone.sequence(n).max}"
|
||||
|
|
|
|||
27
Task/Hailstone-sequence/Rust/hailstone-sequence.rust
Normal file
27
Task/Hailstone-sequence/Rust/hailstone-sequence.rust
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use std::vec::Vec;
|
||||
|
||||
fn hailstone(mut n : int) -> Vec<int>{
|
||||
let mut v = vec!(n);
|
||||
while n > 1{
|
||||
n = if n % 2 == 0 { n / 2 }
|
||||
else { 3 * n + 1 };
|
||||
v.push(n);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut max_sequence = 0i;
|
||||
let mut number_max_sequence = 0i;
|
||||
let hs27 = hailstone(27);
|
||||
println!("hailstone(27) has {} elements, starting from {} and ending to {}.", hs27.len(), hs27[0..4], hs27[hs27.len()-4..hs27.len()]);
|
||||
|
||||
for i in range(1i, 100000) {
|
||||
let hs_i = hailstone(i);
|
||||
if hs_i.len() as int > max_sequence {
|
||||
max_sequence = hs_i.len() as int;
|
||||
number_max_sequence = i;
|
||||
}
|
||||
}
|
||||
println!("Maximum : {} elements with number {}.", max_sequence, number_max_sequence);
|
||||
}
|
||||
54
Task/Hailstone-sequence/SAS/hailstone-sequence.sas
Normal file
54
Task/Hailstone-sequence/SAS/hailstone-sequence.sas
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
* Create a routine to generate the hailstone sequence for one number;
|
||||
%macro gen_seq(n);
|
||||
data hailstone;
|
||||
array hs_seq(100000);
|
||||
n=&n;
|
||||
do until (n=1);
|
||||
seq_size + 1;
|
||||
hs_seq(seq_size) = n;
|
||||
if mod(n,2)=0 then n=n/2;
|
||||
else n=(3*n)+1;
|
||||
end;
|
||||
seq_size + 1;
|
||||
hs_seq(seq_size)=n;
|
||||
call symputx('seq_length',seq_size);
|
||||
run;
|
||||
|
||||
proc sql;
|
||||
title "First and last elements of Hailstone Sequence for number &n";
|
||||
select seq_size as sequence_length, hs_seq1, hs_seq2, hs_seq3, hs_seq4
|
||||
%do i=&seq_length-3 %to &seq_length;
|
||||
, hs_seq&i
|
||||
%end;
|
||||
from hailstone;
|
||||
quit;
|
||||
%mend;
|
||||
|
||||
* Use the routine to output the first and last four numbers in the sequence for 27;
|
||||
%gen_seq(27);
|
||||
|
||||
* Show the number less than 100,000 which has the longest hailstone sequence, and what that length is ;
|
||||
%macro longest_hailstone(start_num, end_num);
|
||||
data hailstone_analysis;
|
||||
do start=&start_num to &end_num;
|
||||
n=start;
|
||||
length_of_sequence=1;
|
||||
do while (n>1);
|
||||
length_of_sequence+1;
|
||||
if mod(n,2)=0 then n=n/2;
|
||||
else n=(3*n) + 1;
|
||||
end;
|
||||
output;
|
||||
end;
|
||||
run;
|
||||
|
||||
proc sort data=hailstone_analysis;
|
||||
by descending length_of_sequence;
|
||||
run;
|
||||
|
||||
proc print data=hailstone_analysis (obs=1) noobs;
|
||||
title "Number from &start_num to &end_num with longest Hailstone sequence";
|
||||
var start length_of_sequence;
|
||||
run;
|
||||
%mend;
|
||||
%longest_hailstone(1,99999);
|
||||
33
Task/Hailstone-sequence/Scilab/hailstone-sequence.scilab
Normal file
33
Task/Hailstone-sequence/Scilab/hailstone-sequence.scilab
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
function x=hailstone(n)
|
||||
// iterative definition
|
||||
// usage: global verbose; verbose=%T; hailstone(27)
|
||||
global verbose
|
||||
x=0; loop=%T
|
||||
while(loop)
|
||||
x=x+1
|
||||
if verbose then
|
||||
printf('%i ',n)
|
||||
end
|
||||
if n==1 then
|
||||
loop=%F
|
||||
elseif modulo(n,2)==1 then
|
||||
n=3*n+1
|
||||
else
|
||||
n=n/2
|
||||
end
|
||||
end
|
||||
endfunction
|
||||
|
||||
global verbose;
|
||||
verbose=1;
|
||||
N=hailstone(27);
|
||||
printf('\n\n%i\n',N);
|
||||
|
||||
global verbose;
|
||||
verbose=0;
|
||||
N=100000;
|
||||
M=zeros(N,1);
|
||||
for k=1:N
|
||||
M(k)=hailstone(k);
|
||||
end;
|
||||
[maxLength,n]=max(M)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
prompt N
|
||||
N→M: 0→X: 1→L
|
||||
While L=1
|
||||
X+1→X
|
||||
Disp M
|
||||
If M=1
|
||||
Then: 0→L
|
||||
Else
|
||||
If remainder(M,2)=1
|
||||
Then: 3*M+1→M
|
||||
Else: M/2→M
|
||||
End
|
||||
End
|
||||
End
|
||||
{N,X}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
prompt N
|
||||
0→A:0→B
|
||||
for(I,1,N)
|
||||
I→M: 0→X: 1→L
|
||||
While L=1
|
||||
X+1→X
|
||||
If M=1
|
||||
Then: 0→L
|
||||
Else
|
||||
If remainder(M,2)=1
|
||||
Then: 3*M+1→M
|
||||
Else: M/2→M
|
||||
End
|
||||
End
|
||||
End
|
||||
If X>B: Then
|
||||
I→A:X→B
|
||||
End
|
||||
Disp {I,X}
|
||||
End
|
||||
{A,B}
|
||||
57
Task/Hailstone-sequence/Visual-Basic/hailstone-sequence.vb
Normal file
57
Task/Hailstone-sequence/Visual-Basic/hailstone-sequence.vb
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
Option Explicit
|
||||
Dim flag As Boolean ' true to print values
|
||||
Sub main()
|
||||
Dim longest As Long, n As Long
|
||||
Dim i As Long, value As Long
|
||||
' Task 1:
|
||||
flag = True
|
||||
i = 27
|
||||
Debug.Print "The hailstone sequence has length of "; i; " is "; hailstones(i)
|
||||
' Task 2:
|
||||
flag = False
|
||||
longest = 0
|
||||
For i = 1 To 99999
|
||||
If longest < hailstones(i) Then
|
||||
longest = hailstones(i)
|
||||
value = i
|
||||
End If
|
||||
Next i
|
||||
Debug.Print value; " has the longest sequence of "; longest
|
||||
End Sub 'main
|
||||
Function hailstones(n As Long) As Long
|
||||
Dim m As Long, p As Long
|
||||
Dim m1 As Long, m2 As Long, m3 As Long, m4 As Long
|
||||
If flag Then Debug.Print "The sequence for"; n; "is: ";
|
||||
p = 1
|
||||
m = n
|
||||
If flag Then Debug.Print m;
|
||||
While m > 1
|
||||
p = p + 1
|
||||
If (m Mod 2) = 0 Then
|
||||
m = m / 2
|
||||
Else
|
||||
m = 3 * m + 1
|
||||
End If
|
||||
If p <= 4 Then If flag Then Debug.Print m;
|
||||
m4 = m3
|
||||
m3 = m2
|
||||
m2 = m1
|
||||
m1 = m
|
||||
Wend
|
||||
If flag Then
|
||||
If p <= 4 Then
|
||||
Debug.Print
|
||||
ElseIf p = 5 Then
|
||||
Debug.Print m1
|
||||
ElseIf p = 6 Then
|
||||
Debug.Print m2; m1
|
||||
ElseIf p = 7 Then
|
||||
Debug.Print m3; m2; m1
|
||||
ElseIf p = 8 Then
|
||||
Debug.Print m4; m3; m2; m1
|
||||
Else
|
||||
Debug.Print "..."; m4; m3; m2; m1
|
||||
End If
|
||||
End If
|
||||
hailstones = p
|
||||
End Function 'hailstones
|
||||
Loading…
Add table
Add a link
Reference in a new issue