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,56 @@
package main
import "fmt"
var ffr, ffs func(int) int
// The point of the init function is to encapsulate r and s. If you are
// not concerned about that or do not want that, r and s can be variables at
// package level and ffr and ffs can be ordinary functions at package level.
func init() {
// task 1, 2
r := []int{0, 1}
s := []int{0, 2}
ffr = func(n int) int {
for len(r) <= n {
nrk := len(r) - 1 // last n for which r(n) is known
rNxt := r[nrk] + s[nrk] // next value of r: r(nrk+1)
r = append(r, rNxt) // extend sequence r by one element
for sn := r[nrk] + 2; sn < rNxt; sn++ {
s = append(s, sn) // extend sequence s up to rNext
}
s = append(s, rNxt+1) // extend sequence s one past rNext
}
return r[n]
}
ffs = func(n int) int {
for len(s) <= n {
ffr(len(r))
}
return s[n]
}
}
func main() {
// task 3
for n := 1; n <= 10; n++ {
fmt.Printf("r(%d): %d\n", n, ffr(n))
}
// task 4
var found [1001]int
for n := 1; n <= 40; n++ {
found[ffr(n)]++
}
for n := 1; n <= 960; n++ {
found[ffs(n)]++
}
for i := 1; i <= 1000; i++ {
if found[i] != 1 {
fmt.Println("task 4: FAIL")
return
}
}
fmt.Println("task 4: PASS")
}

View file

@ -0,0 +1,49 @@
package main
import "fmt"
type xint int64
func R() (func() (xint)) {
r, s := xint(0), func() (xint) (nil)
return func() (xint) {
switch {
case r < 1: r = 1
case r < 3: r = 3
default:
if s == nil {
s = S()
s()
}
r += s()
}
if r < 0 { panic("r overflow") }
return r
}
}
func S() (func() (xint)) {
s, r1, r := xint(0), xint(0), func() (xint) (nil)
return func() (xint) {
if s < 2 {
s = 2
} else {
if r == nil {
r = R()
r()
r1 = r()
}
s++
if s > r1 { r1 = r() }
if s == r1 { s++ }
}
if s < 0 { panic("s overflow") }
return s
}
}
func main() {
r, sum := R(), xint(0)
for i := 0; i < 10000000; i++ {
sum += r()
}
fmt.Println(sum)
}