March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -1,14 +1,14 @@
(defn hailstone-seq [n]
(:pre [(pos? 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 [hseq (hailstone-seq 27)]
(-> hseq count (= 112) assert)
(->> hseq (take 4) (= [27 82 41 124]) assert)
(->> hseq (drop 108) (= [8 4 2 1]) assert))
(let [{max-i :num, max-len :len}
(reduce #(max-key :len %1 %2)

View file

@ -0,0 +1,29 @@
import java.util.ArrayDeque
fun hailstone(n : Int) : ArrayDeque<Int> {
val hails = when {
n == 1 -> ArrayDeque<Int>()
n % 2 == 0 -> hailstone(n / 2)
else -> hailstone(3 * n + 1)
}
hails addFirst(n)
return hails
}
fun main(args : Array<String>) {
val hail27 = hailstone(27)
fun showSeq(s : List<Int>) = s map {it.toString()} reduce {a, b -> a + ", " + b}
System.out.println(
"Hailstone sequence for 27 is " +
showSeq(hail27 take(3)) + " ... " + showSeq(hail27 drop(hail27.size - 3)) +
" with length ${hail27.size}."
)
var longestHail = hailstone(1)
for (x in 1 .. 99999)
longestHail = array(hailstone(x), longestHail) maxBy {it.size} ?: longestHail
System.out.println(
"${longestHail.getFirst()} is the number less than 100000 with " +
"the longest sequence, having length ${longestHail.size}."
)
}

View file

@ -1 +1 @@
HailstoneFP[n_] := Drop[FixedPointList[If[# != 1, Which[Mod[#, 2] == 0, #/2, True, ( 3*# + 1) ], 1] &, n], -1]
HailstoneFP[n_Integer] := Most[FixedPointList[Which[# == 1, 1, EvenQ[#] , #/2, OddQ[#], (3*# + 1)] &, n]]

View file

@ -1,12 +1,4 @@
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];
hailstone[n_Integer] := Block[{sequence = {}, c = n},
While[c > 1, c = If[EvenQ[c], c/2, 3 c + 1];
AppendTo[sequence, c]];
sequence]

View file

@ -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];

View file

@ -0,0 +1,19 @@
:- module hailstone.
:- interface.
:- import_module int, list.
:- func hailstone(int) = list(int).
:- pred hailstone(int::in, list(int)::out) is det.
:- implementation.
hailstone(N) = S :- hailstone(N, S).
hailstone(N, [N|S]) :-
( N = 1 -> S = []
; N mod 2 = 0 -> hailstone(N/2, S)
; hailstone(3 * N + 1, S) ).
:- end_module hailstone.

View file

@ -0,0 +1,36 @@
:- module test_hailstone.
:- interface.
:- import_module io.
:- pred main(io.state::di, io.state::uo) is det.
:- implementation.
:- import_module int, list.
:- import_module hailstone.
:- pred longest(int::in, int::out, int::out) is det.
:- pred longest(int::in, int::in, int::in, int::out, int::out) is det.
longest(M, N, L) :- longest(M, 0, 0, N, L).
longest(N, CN, CL, MN, ML) :-
( N > 1 ->
L = list.length(hailstone(N)),
( L > CL -> longest(N - 1, N, L, MN, ML)
; longest(N - 1, CN, CL, MN, ML) )
; MN = CN, ML = CL ).
main(!IO) :-
S = hailstone(27),
( list.length(S) = 112,
list.append([27, 82, 41, 124], _, S),
list.remove_suffix(S, [8, 4, 2, 1], _),
longest(100000, 77031, 351) ->
io.write_string("All tests succeeded.\n", !IO)
; io.write_string("At least one test failed.\n", !IO) ).
:- end_module test_hailstone.

View file

@ -0,0 +1 @@
:- pred longest(int::in, int::out, int::out) is det.

View file

@ -0,0 +1,26 @@
# Author M. McNabb
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)}
}
}
$Counts = @()
}
process {
for ($i = 1; $i -lt $UpperLimit; $i++) {
$Object = [pscustomobject]@{
'Number' = $i
'Count' = (Get-HailStone $i).count
}
$Counts += $Object
}
}
end {$Counts}
}