June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,7 +1,12 @@
{{omit from|Go}}
{{omit from|GUISS}}
{{omit from|Go}}
{{omit from|MATLAB}}
{{omit from|Maxima}}
{{omit from|Octave}}
{{omit from|R}}
{{omit from|SAS}}
{{omit from|Scilab}}
{{omit from|Stata}}
The general idea behind an executable library is to create a library
that when used as a library does one thing;
but has the ability to be run directly via command line.

View file

@ -0,0 +1,28 @@
(ns rosetta-code.hailstone-sequence)
(defn next-in-hailstone
"Returns the next number in the Hailstone sequence that starts with x.
If x is less than 2, returns nil."
[x]
(when (> x 1)
(if (even? x)
(/ x 2)
(inc (* 3 x)))))
(defn hailstone-seq
"Returns a lazy Hailstone sequence starting with the number x."
[x]
(take-while some?
(iterate next-in-hailstone x)))
(defn -main [& args]
(let [h27 (hailstone-seq 27)]
(printf "The Hailstone sequence starting at 27 contains %s elements:\n%s ... %s.\n"
(count h27)
(vec (take 4 h27))
(vec (take-last 4 h27)))
(let [[number length] (apply max-key second
(map (fn [x] [x (count (hailstone-seq x))])
(range 100000)))]
(printf "The number %s has the longest Hailstone sequence under 100000, of length %s.\n"
number length))))

View file

@ -0,0 +1,13 @@
(ns rosetta-code.frequent-hailstone-lengths
(:require [rosetta-code.hailstone-sequence
:refer [hailstone-seq]]))
(defn -main [& args]
(let [frequencies (apply merge-with +
(for [x (range 1 100000)]
{(count (hailstone-seq x)) 1}))
[most-frequent-length frequency]
(apply max-key val (seq frequencies))]
(printf (str "The most frequent Hailstone sequence length for numbers under 100000 is %s,"
" with a frequency of %s.\n")
most-frequent-length frequency)))

View file

@ -0,0 +1,53 @@
############### in file hailstone.jl ###############
module Hailstone
function hailstone(n)
ret = [n]
while n > 1
if n & 1 > 0
n = 3n + 1
else
n = Int(n//2)
end
append!(ret, n)
end
return ret
end
export hailstone
end
if PROGRAM_FILE == "hailstone.jl"
using Hailstone
h = hailstone(27)
n = length(h)
println("The sequence of hailstone(27) is:\n $h.\nThis sequence is of length $n. It starts with $(h[1:4]) and ends with $(h[n-3:end]).")
end
############ in file moduletest.jl ####################
include("hailstone.jl")
using Hailstone
function countstones(mi, mx)
lengths2occurences = Dict()
mostfreq = mi
maxcount = 1
for i in mi:mx
h = hailstone(i)
n = length(h)
if haskey(lengths2occurences, n)
newoccurences = lengths2occurences[n] + 1
if newoccurences > maxcount
maxcount = newoccurences
mostfreq = n
end
lengths2occurences[n] = newoccurences
else
lengths2occurences[n] = 1
end
end
mostfreq, maxcount
end
nlen, cnt = countstones(1,99999)
print("The most common hailstone sequence length for hailstone(n) for 1 <= n < 100000 is $nlen, which occurs $cnt times.")