Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,39 @@
/* hailstone.wren */
var Hailstone = Fn.new { |n|
if (n < 1) Fiber.abort("Parameter must be a positive integer.")
var h = [n]
while (n != 1) {
n = (n%2 == 0) ? (n/2).floor : 3*n + 1
h.add(n)
}
return h
}
var libMain_ = Fn.new {
var h = Hailstone.call(27)
System.print("For the Hailstone sequence starting with n = 27:")
System.print(" Number of elements = %(h.count)")
System.print(" First four elements = %(h[0..3])")
System.print(" Final four elements = %(h[-4..-1])")
System.print("\nThe Hailstone sequence for n < 100,000 with the longest length is:")
var longest = 0
var longlen = 0
for (n in 1..99999) {
var h = Hailstone.call(n)
var c = h.count
if (c > longlen) {
longest = n
longlen = c
}
}
System.print(" Longest = %(longest)")
System.print(" Length = %(longlen)")
}
// Check if it's being used as a library or not.
import "os" for Process
if (Process.allArguments[1] == "hailstone.wren") { // if true, not a library
libMain_.call()
}

View file

@ -0,0 +1,20 @@
/* hailstone2.wren */
import "/hailstone" for Hailstone
var freq = {}
for (i in 1...100000) {
var len = Hailstone.call(i).count
var f = freq[len]
freq[len] = f ? f + 1 : 1
}
var mk = 0
var mv = 0
for (k in freq.keys) {
var v = freq[k]
if (v > mv) {
mk = k
mv = v
}
}
System.print("The Hailstone length returned most is %(mk), which occurs %(mv) times.")