June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -5,18 +5,18 @@ print_hailstone(integer h)
|
|||
|
||||
while (h ^ 1) {
|
||||
lb_p_integer(l, h);
|
||||
h = __hold(h & 1, 3 * h + 1, h / 2);
|
||||
h = 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);
|
||||
l[0], l[1], l[2], l[3], l[-3], l[-2], l[-1], 1, ~l + 1);
|
||||
}
|
||||
|
||||
void
|
||||
max_hailstone(integer x)
|
||||
{
|
||||
integer e, i, m;
|
||||
record r;
|
||||
index r;
|
||||
|
||||
m = 0;
|
||||
i = 1;
|
||||
|
|
@ -26,16 +26,16 @@ max_hailstone(integer x)
|
|||
h = i;
|
||||
l = 1;
|
||||
while (h ^ 1) {
|
||||
if (r_j_integer(k, r, itoa(h))) {
|
||||
if (i_j_integer(k, r, h)) {
|
||||
l += k;
|
||||
break;
|
||||
} else {
|
||||
l += 1;
|
||||
h = __hold(h & 1, 3 * h + 1, h / 2);
|
||||
h = h & 1 ? 3 * h + 1 : h / 2;
|
||||
}
|
||||
}
|
||||
|
||||
r_f_integer(r, itoa(i), l - 1);
|
||||
r[i] = l - 1;
|
||||
|
||||
if (m < l) {
|
||||
m = l;
|
||||
|
|
@ -45,7 +45,7 @@ max_hailstone(integer x)
|
|||
i += 1;
|
||||
}
|
||||
|
||||
o_form("hailstone sequence length for ~ is ~ long\n", e, m);
|
||||
o_form("hailstone sequence length for ~ is ~\n", e, m);
|
||||
}
|
||||
|
||||
integer
|
||||
|
|
|
|||
48
Task/Hailstone-sequence/Elena/hailstone-sequence.elena
Normal file
48
Task/Hailstone-sequence/Elena/hailstone-sequence.elena
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import system'collections.
|
||||
import extensions.
|
||||
|
||||
int const maxNumber = 100000.
|
||||
|
||||
Hailstone = (:n:lengths)
|
||||
[
|
||||
if (n == 1)
|
||||
[
|
||||
^ 1
|
||||
].
|
||||
|
||||
while (true)
|
||||
[
|
||||
if (lengths containsKey(n))
|
||||
[
|
||||
^ lengths[n]
|
||||
];
|
||||
[
|
||||
if (n int; isEven)
|
||||
[
|
||||
lengths[n] := 1 + Hailstone(n/2, lengths)
|
||||
];
|
||||
[
|
||||
lengths[n] := 1 + Hailstone((3*n) + 1, lengths)
|
||||
]
|
||||
]
|
||||
]
|
||||
].
|
||||
|
||||
program =
|
||||
[
|
||||
int longestChain := 0.
|
||||
int longestNumber := 0.
|
||||
var recursiveLengths := Dictionary new.
|
||||
|
||||
1 till(maxNumber) do(:i)<int>
|
||||
[
|
||||
var chainLength := Hailstone(i, recursiveLengths).
|
||||
if (longestChain < chainLength)
|
||||
[
|
||||
longestChain := chainLength.
|
||||
longestNumber := i.
|
||||
]
|
||||
].
|
||||
|
||||
console printFormatted("max below {0}: {1} ({2} steps)", maxNumber, longestNumber, longestChain).
|
||||
].
|
||||
|
|
@ -1,20 +1,21 @@
|
|||
import Data.List (maximumBy)
|
||||
import Data.Ord (comparing)
|
||||
|
||||
collatz :: Int -> Int
|
||||
collatz n
|
||||
| even n = n `div` 2
|
||||
| otherwise = 3 * n + 1
|
||||
|
||||
hailstone :: Int -> [Int]
|
||||
hailstone = takeWhile (/= 1) . iterate collatz
|
||||
where
|
||||
collatz n =
|
||||
if even n
|
||||
then n `div` 2
|
||||
else 3 * n + 1
|
||||
|
||||
longestChain :: Int
|
||||
longestChain =
|
||||
fst
|
||||
(maximumBy (comparing snd) (((,) <*> length . hailstone) <$> [1 .. 100000]))
|
||||
fst $
|
||||
maximumBy (comparing snd) $ (,) <*> (length . hailstone) <$> [1 .. 100000]
|
||||
|
||||
--TEST -------------------------------------------------------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
putStrLn
|
||||
|
|
|
|||
|
|
@ -1,58 +1,104 @@
|
|||
(() => {
|
||||
const dctMemo = {};
|
||||
|
||||
// Length only of hailstone sequence
|
||||
// hailstones :: Int -> [Int]
|
||||
const hailstones = x => {
|
||||
const collatz = memoized(n =>
|
||||
even(n) ? div(n, 2) : (3 * n) + 1);
|
||||
return reverse(until(
|
||||
xs => xs[0] === 1,
|
||||
xs => cons(collatz(xs[0]), xs), [x]
|
||||
));
|
||||
};
|
||||
|
||||
// collatzLength :: Int -> Int
|
||||
const collatzLength = n => {
|
||||
let i = 1;
|
||||
let a = n;
|
||||
let lng;
|
||||
const collatzLength = n =>
|
||||
until(
|
||||
xi => xi[0] === 1,
|
||||
([x, i]) => [(x % 2 ? 3 * x + 1 : x / 2), i + 1], //
|
||||
[n, 1]
|
||||
)[1];
|
||||
|
||||
while (a !== 1) {
|
||||
lng = dctMemo[a];
|
||||
if ('u' === (typeof lng)[0]) {
|
||||
a = (a % 2 ? 3 * a + 1 : a / 2);
|
||||
i++;
|
||||
} else return lng + i - 1;
|
||||
}
|
||||
return i;
|
||||
// GENERIC FUNCTIONS -----------------------------------------------------
|
||||
|
||||
// comparing :: (a -> b) -> (a -> a -> Ordering)
|
||||
const comparing = f =>
|
||||
(x, y) => {
|
||||
const
|
||||
a = f(x),
|
||||
b = f(y);
|
||||
return a < b ? -1 : (a > b ? 1 : 0);
|
||||
};
|
||||
|
||||
// cons :: a -> [a] -> [a]
|
||||
const cons = (x, xs) => [x].concat(xs);
|
||||
|
||||
// div :: Int -> Int -> Int
|
||||
const div = (x, y) => Math.floor(x / y);
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = (m, n) =>
|
||||
Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// even :: Int -> Bool
|
||||
const even = n => n % 2 === 0;
|
||||
|
||||
// fst :: (a, b) -> a
|
||||
const fst = pair => pair.length === 2 ? pair[0] : undefined;
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f);
|
||||
|
||||
// maximumBy :: (a -> a -> Ordering) -> [a] -> a
|
||||
const maximumBy = (f, xs) =>
|
||||
xs.length > 0 ? (
|
||||
xs.slice(1)
|
||||
.reduce((a, x) => f(x, a) > 0 ? x : a, xs[0])
|
||||
) : undefined;
|
||||
|
||||
// memoized :: (a -> b) -> (a -> b)
|
||||
const memoized = f => {
|
||||
const dctMemo = {};
|
||||
return x => {
|
||||
const v = dctMemo[x];
|
||||
return v !== undefined ? v : (dctMemo[x] = f(x));
|
||||
};
|
||||
};
|
||||
|
||||
// range :: Int -> Int -> Maybe Int -> [Int]
|
||||
const range = (m, n, delta) => {
|
||||
const blnUp = n > m,
|
||||
d = blnUp ? (delta || 1) : -(delta || 1),
|
||||
lng = Math.abs(Math.floor((blnUp ? n - m : m - n) / d) + 1),
|
||||
a = Array(lng);
|
||||
let i = lng;
|
||||
// reverse :: [a] -> [a]
|
||||
const reverse = xs =>
|
||||
xs.slice(0)
|
||||
.reverse();
|
||||
|
||||
while (i--) a[i] = (d * i) + m;
|
||||
return a;
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
// until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
const until = (p, f, x) => {
|
||||
let v = x;
|
||||
while (!p(v)) v = f(v);
|
||||
return v;
|
||||
};
|
||||
|
||||
// longestBelow :: Int -> {Number::Int, Length:Int}
|
||||
const longestBelow = n =>
|
||||
range(1, n)
|
||||
.reduce(
|
||||
(a, x) => {
|
||||
const lng = dctMemo[x] || (dctMemo[x] = collatzLength(x));
|
||||
// MAIN ------------------------------------------------------------------
|
||||
const
|
||||
// ceiling :: Int
|
||||
ceiling = 100000,
|
||||
|
||||
return lng > a.l ? {
|
||||
n: x,
|
||||
l: lng
|
||||
} : a
|
||||
|
||||
}, {
|
||||
n: 0,
|
||||
l: 0
|
||||
}
|
||||
// (maxLen, maxNum) :: (Int, Int)
|
||||
[maxLen, maxNum] =
|
||||
maximumBy(
|
||||
comparing(fst),
|
||||
map(i => [collatzLength(i), i], enumFromTo(1, ceiling))
|
||||
);
|
||||
|
||||
// TEST
|
||||
// show :: a -> String
|
||||
const show = x => JSON.stringify(x, null, 2);
|
||||
|
||||
return show(
|
||||
[100000, 1000000, 10000000].map(longestBelow)
|
||||
);
|
||||
return unlines([
|
||||
'Collatz sequence for 27: ',
|
||||
`${hailstones(27)}`,
|
||||
'',
|
||||
`The number ${maxNum} has the longest hailstone sequence`,
|
||||
`for any starting number under ${ceiling}.`,
|
||||
'',
|
||||
`The length of that sequence is ${maxLen}.`
|
||||
]);
|
||||
})();
|
||||
|
|
|
|||
11
Task/Hailstone-sequence/Julia/hailstone-sequence-1.julia
Normal file
11
Task/Hailstone-sequence/Julia/hailstone-sequence-1.julia
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function hailstonelength(n::Integer)
|
||||
len = 1
|
||||
while n > 1
|
||||
n = ifelse(iseven(n), n ÷ 2, 3n + 1)
|
||||
len += 1
|
||||
end
|
||||
return len
|
||||
end
|
||||
|
||||
@show hailstonelength(27)
|
||||
@show findmax(hailstonelength(i) for i in 1:100_000)
|
||||
40
Task/Hailstone-sequence/Julia/hailstone-sequence-2.julia
Normal file
40
Task/Hailstone-sequence/Julia/hailstone-sequence-2.julia
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
struct HailstoneSeq{T<:Integer}
|
||||
start::T
|
||||
end
|
||||
|
||||
Base.eltype(::HailstoneSeq{T}) where T = T
|
||||
|
||||
Base.start(hs::HailstoneSeq) = (-1, hs.start)
|
||||
Base.done(::HailstoneSeq, state) = state == (1, 4)
|
||||
function Base.next(::HailstoneSeq, state)
|
||||
_, s2 = state
|
||||
s1 = s2
|
||||
if iseven(s2)
|
||||
s2 = s2 ÷ 2
|
||||
else
|
||||
s2 = 3s2 + 1
|
||||
end
|
||||
return s1, (s1, s2)
|
||||
end
|
||||
|
||||
function Base.length(hs::HailstoneSeq)
|
||||
r = 0
|
||||
for _ in hs
|
||||
r += 1
|
||||
end
|
||||
return r
|
||||
end
|
||||
|
||||
function Base.show(io::IO, hs::HailstoneSeq)
|
||||
f5 = collect(Iterators.take(hs, 5))
|
||||
print(io, "HailstoneSeq(", join(f5, ", "), "...)")
|
||||
end
|
||||
|
||||
hs = HailstoneSeq(27)
|
||||
println("Collection of the Hailstone sequence from 27: $hs")
|
||||
cl = collect(hs)
|
||||
println("First 5 elements: ", join(cl[1:5], ", "))
|
||||
println("Last 5 elements: ", join(cl[end-4:end], ", "))
|
||||
|
||||
Base.isless(h::HailstoneSeq, s::HailstoneSeq) = length(h) < length(s)
|
||||
println("The number with the longest sequence under 100,000 is: ", maximum(HailstoneSeq.(1:100_000)))
|
||||
40
Task/Hailstone-sequence/Julia/hailstone-sequence-3.julia
Normal file
40
Task/Hailstone-sequence/Julia/hailstone-sequence-3.julia
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
struct HailstoneSeq{T<:Integer}
|
||||
start::T
|
||||
end
|
||||
|
||||
Base.eltype(::HailstoneSeq{T}) where T = T
|
||||
|
||||
Base.start(hs::HailstoneSeq) = (-1, hs.start)
|
||||
Base.done(::HailstoneSeq, state) = state == (1, 4)
|
||||
function Base.next(::HailstoneSeq, state)
|
||||
_, s2 = state
|
||||
s1 = s2
|
||||
if iseven(s2)
|
||||
s2 = s2 ÷ 2
|
||||
else
|
||||
s2 = 3s2 + 1
|
||||
end
|
||||
return s1, (s1, s2)
|
||||
end
|
||||
|
||||
function Base.length(hs::HailstoneSeq)
|
||||
r = 0
|
||||
for _ in hs
|
||||
r += 1
|
||||
end
|
||||
return r
|
||||
end
|
||||
|
||||
function Base.show(io::IO, hs::HailstoneSeq)
|
||||
f5 = collect(Iterators.take(hs, 5))
|
||||
print(io, "HailstoneSeq(", join(f5, ", "), "...)")
|
||||
end
|
||||
|
||||
hs = HailstoneSeq(27)
|
||||
println("Collection of the Hailstone sequence from 27: $hs")
|
||||
cl = collect(hs)
|
||||
println("First 5 elements: ", join(cl[1:5], ", "))
|
||||
println("Last 5 elements: ", join(cl[end-4:end], ", "))
|
||||
|
||||
Base.isless(h::HailstoneSeq, s::HailstoneSeq) = length(h) < length(s)
|
||||
println("The number with the longest sequence under 100,000 is: ", maximum(HailstoneSeq.(1:100_000)))
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
function hailstone(n)
|
||||
seq = [n]
|
||||
while n>1
|
||||
n = n % 2 == 0 ? n >> 1 : 3n + 1
|
||||
push!(seq,n)
|
||||
end
|
||||
return seq
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue