Data update
This commit is contained in:
parent
35bcdeebf8
commit
74c69a0df6
2427 changed files with 31826 additions and 3468 deletions
|
|
@ -0,0 +1,43 @@
|
|||
#include <basico.h>
|
||||
|
||||
#proto Hailstone(_X_,_SW_)
|
||||
|
||||
algoritmo
|
||||
|
||||
valor=27, máxima secuencia=0, vtemp=0
|
||||
imprimir ( _Hailstone(27,1) ---copiar en 'máxima secuencia'--- , NL )
|
||||
|
||||
i=28
|
||||
iterar
|
||||
_Hailstone(i,0), copiar en 'vtemp'
|
||||
cuando( sea mayor que 'máxima secuencia' ) {
|
||||
máxima secuencia = vtemp
|
||||
valor=i
|
||||
}
|
||||
++i
|
||||
hasta que ' #(i==100000) '
|
||||
imprimir ( #(utf8("Máxima longitud ")),máxima secuencia,\
|
||||
" fue encontrada para Hailstone(",valor,\
|
||||
#(utf8(") para números <100,000")), NL )
|
||||
|
||||
terminar
|
||||
|
||||
subrutinas
|
||||
|
||||
Hailstone(n, sw)
|
||||
largo_de_secuencia = 0
|
||||
v={}, n, mete(v)
|
||||
iterar
|
||||
tomar si ( es par(n), #(n/2), \
|
||||
tomar si ( #(n<>1), #(3*n+1), 1) )
|
||||
---copiar en 'n'--- mete(v)
|
||||
hasta que ' #(n==1) '
|
||||
#(length(v)), mover a 'largo_de_secuencia'
|
||||
cuando (sw){
|
||||
decimales '0'
|
||||
#( v[1:4] ), ",...,",
|
||||
#( v[largo_de_secuencia-4 : largo_de_secuencia] )
|
||||
NL, #(utf8("Tamaño de la secuencia: "))
|
||||
imprime esto; decimales normales
|
||||
}
|
||||
retornar ' largo_de_secuencia '
|
||||
30
Task/Hailstone-sequence/EasyLang/hailstone-sequence.easy
Normal file
30
Task/Hailstone-sequence/EasyLang/hailstone-sequence.easy
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
proc hailstone n . list[] .
|
||||
list[] = [ ]
|
||||
while n <> 1
|
||||
list[] &= n
|
||||
if n mod 2 = 0
|
||||
n = n / 2
|
||||
else
|
||||
n = 3 * n + 1
|
||||
.
|
||||
.
|
||||
list[] &= 1
|
||||
.
|
||||
hailstone 27 l[]
|
||||
write "27 has length " & len l[] & " with "
|
||||
for i to 4
|
||||
write l[i] & " "
|
||||
.
|
||||
write "... "
|
||||
for i = len l[] - 3 to len l[]
|
||||
write l[i] & " "
|
||||
.
|
||||
print ""
|
||||
for i = 1 to 100000
|
||||
hailstone i l[]
|
||||
if len l[] >= max_iter
|
||||
max_i = i
|
||||
max_iter = len l[]
|
||||
end
|
||||
end
|
||||
print max_i & " has length " & max_iter
|
||||
31
Task/Hailstone-sequence/MiniScript/hailstone-sequence-1.mini
Normal file
31
Task/Hailstone-sequence/MiniScript/hailstone-sequence-1.mini
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
getSequence = function(n)
|
||||
results = [n]
|
||||
while n > 1
|
||||
if n % 2 then
|
||||
n = 3 * n + 1
|
||||
else
|
||||
n = n / 2
|
||||
end if
|
||||
results.push n
|
||||
end while
|
||||
return results
|
||||
end function
|
||||
|
||||
h = getSequence(27)
|
||||
print "The hailstone sequence for 27 has 112 elements starting with"
|
||||
print h[:4]
|
||||
print "and ending with"
|
||||
print h[-4:]
|
||||
|
||||
maxSeqLen = 0
|
||||
maxSeqVal = 0
|
||||
for i in range(1,100000)
|
||||
h = getSequence(i)
|
||||
if h.len > maxSeqLen then
|
||||
maxSeqLen = h.len
|
||||
maxSeqVal = i
|
||||
end if
|
||||
end for
|
||||
print
|
||||
print "The number < 100,000 which has the longest hailstone sequence is " + maxSeqVal + "."
|
||||
print "This sequence has " + maxSeqLen + " elements."
|
||||
45
Task/Hailstone-sequence/MiniScript/hailstone-sequence-2.mini
Normal file
45
Task/Hailstone-sequence/MiniScript/hailstone-sequence-2.mini
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
cache = {}
|
||||
calc = function(n)
|
||||
if cache.hasIndex(n) then return
|
||||
items = [n]
|
||||
origNum = n
|
||||
while n > 1 and not cache.hasIndex(n)
|
||||
if n % 2 then
|
||||
n = 3 * n + 1
|
||||
else
|
||||
n = n /2
|
||||
end if
|
||||
items.push n
|
||||
end while
|
||||
cache[origNum] = {"len": items.len,"items":items}
|
||||
end function
|
||||
|
||||
getLen = function(n)
|
||||
if not cache.hasIndex(n) then calc n
|
||||
if n == 1 then return 1
|
||||
return cache[n].len + getLen(cache[n].items[-1]) - 1
|
||||
end function
|
||||
|
||||
getSequence = function(n)
|
||||
if not cache.hasIndex(n) then calc n
|
||||
if n == 1 then return [1]
|
||||
return cache[n].items[:-1] + getSequence(cache[n].items[-1])
|
||||
end function
|
||||
|
||||
h = getSequence(27)
|
||||
print "The hailstone sequence for 27 has " + h.len + " elements starting with"
|
||||
print h[:4]
|
||||
print "and ending with"
|
||||
print h[-4:]
|
||||
|
||||
longSeq = 0
|
||||
longSeqVal =0
|
||||
for i in range(2, 100000)
|
||||
seq = getLen(i)
|
||||
if longSeq < seq then
|
||||
longSeq = seq
|
||||
longSeqVal = i
|
||||
end if
|
||||
end for
|
||||
print "The number < 100,000 which has the longest hailstone sequence is " + longSeqVal + "."
|
||||
print "This sequence has " + longSeq + " elements."
|
||||
24
Task/Hailstone-sequence/SETL/hailstone-sequence.setl
Normal file
24
Task/Hailstone-sequence/SETL/hailstone-sequence.setl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
program hailstone_sequence;
|
||||
hail27 := hailstone(27);
|
||||
print("The hailstone sequence for the number 27 has", #hail27, "elements,");
|
||||
print("starting with", hail27(..4), "and ending with", hail27(#hail27-3..));
|
||||
|
||||
sizes := [#hailstone(n) : n in [1..99999]];
|
||||
maxsize := max/sizes;
|
||||
maxelem := [n : n in [1..#sizes] | sizes(n) = maxsize](1);
|
||||
|
||||
print("The number < 100,000 with the longest hailstone sequence is",maxelem);
|
||||
print("The length of its sequence is",sizes(maxelem));
|
||||
|
||||
proc hailstone(n);
|
||||
seq := [];
|
||||
loop doing seq with:= n; while n/=1 do
|
||||
if even n then
|
||||
n div:= 2;
|
||||
else
|
||||
n := 3*n + 1;
|
||||
end if;
|
||||
end loop;
|
||||
return seq;
|
||||
end proc;
|
||||
end program;
|
||||
Loading…
Add table
Add a link
Reference in a new issue