Family Day update

This commit is contained in:
Ingy döt Net 2020-02-17 23:21:07 -08:00
parent aac6731f2c
commit 9ad63ea473
2442 changed files with 39761 additions and 8255 deletions

View file

@ -1,5 +1,4 @@
{{omit from|GUISS}}
{{omit from|Go}}
{{omit from|MATLAB}}
{{omit from|Maxima}}
{{omit from|Octave}}

View file

@ -0,0 +1,37 @@
// modulino.go
package main
import "fmt"
// Function borrowed from Hailstone sequence task.
// 1st arg is the number to generate the sequence for.
// 2nd arg is a slice to recycle, to reduce garbage.
func hailstone(n int, recycle []int) []int {
s := append(recycle[:0], n)
for n > 1 {
if n&1 == 0 {
n = n / 2
} else {
n = 3*n + 1
}
s = append(s, n)
}
return s
}
func libMain() {
seq := hailstone(27, nil)
fmt.Println("\nHailstone sequence for the number 27:")
fmt.Println(" has", len(seq), "elements")
fmt.Println(" starts with", seq[0:4])
fmt.Println(" ends with", seq[len(seq)-4:])
var longest, length int
for i := 1; i < 100000; i++ {
if le := len(hailstone(i, nil)); le > length {
longest = i
length = le
}
}
fmt.Printf("\n%d has the longest Hailstone sequence, its length being %d.\n", longest, length)
}

View file

@ -0,0 +1,6 @@
// modulino_main.go
package main
func main() {
libMain()
}

View file

@ -0,0 +1,19 @@
// hailstone.go
package main
import "fmt"
func main() {
freq := make(map[int]int)
for i := 1; i < 100000; i++ {
freq[len(hailstone(i, nil))]++
}
var mk, mv int
for k, v := range freq {
if v > mv {
mk = k
mv = v
}
}
fmt.Printf("\nThe Hailstone length returned most is %d, which occurs %d times.\n", mk, mv)
}

View file

@ -1,20 +1,3 @@
function isMainOrInclude()
-- returns 1 if called from the main file, 0 if from an include
integer res
#ilASM{
[32]
mov eax,[ebp+20] -- prev_ebp
mov eax,[eax+8] -- rtn
mov [res],eax
[64]
mov rax,[rbp+40] -- prev_ebp
mov rax,[rax+16] -- rtn
mov [res],rax
[]
}
return res=21 -- (21=T_maintls)
end function
--global (if you want to be able to call this from test.exw)
function hailstone(atom n)
sequence s = {n}
@ -42,7 +25,7 @@ integer count = 1
return count
end function
if isMainOrInclude() then
if include_file()==1 then
sequence s = hailstone(27)
integer ls = length(s)

View file

@ -1,19 +1,20 @@
object HailstoneSequence extends App { // Show it all, default number is 27.
def hailstone(n: Int): Stream[Int] =
n #:: (if (n == 1) Stream.empty else hailstone(if (n % 2 == 0) n / 2 else n * 3 + 1))
def hailstone(n: Int): LazyList[Int] =
n #:: (if (n == 1) LazyList.empty else hailstone(if (n % 2 == 0) n / 2 else n * 3 + 1))
Hailstone.details(args.headOption.map(_.toInt).getOrElse(27))
HailTest.main(Array())
}
object Hailstone extends App { // Compute a given or default number to Hailstone sequence
def details(nr: Int) = {
def details(nr: Int): Unit = {
val collatz = HailstoneSequence.hailstone(nr)
println(s"Use the routine to show that the hailstone sequence for the number: $nr.")
println(collatz.toList)
println(s"It has ${collatz.length} elements.")
}
details(args.headOption.map(_.toInt).getOrElse(27))
}