Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
48
Task/Stern-Brocot-sequence/Go/stern-brocot-sequence-1.go
Normal file
48
Task/Stern-Brocot-sequence/Go/stern-brocot-sequence-1.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sternbrocot"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Task 1, using the conventional sort of generator that generates
|
||||
// terms endlessly.
|
||||
g := sb.Generator()
|
||||
|
||||
// Task 2, demonstrating the generator.
|
||||
fmt.Println("First 15:")
|
||||
for i := 1; i <= 15; i++ {
|
||||
fmt.Printf("%2d: %d\n", i, g())
|
||||
}
|
||||
|
||||
// Task 2 again, showing a simpler technique that might or might not be
|
||||
// considered to "generate" terms.
|
||||
s := sb.New()
|
||||
fmt.Println("First 15:", s.FirstN(15))
|
||||
|
||||
// Tasks 3 and 4.
|
||||
for _, x := range []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100} {
|
||||
fmt.Printf("%3d at 1-based index %d\n", x, 1+s.Find(x))
|
||||
}
|
||||
|
||||
// Task 5.
|
||||
fmt.Println("1-based indexes: gcd")
|
||||
for n, f := range s.FirstN(1000)[:999] {
|
||||
g := gcd(f, (*s)[n+1])
|
||||
fmt.Printf("%d,%d: gcd(%d, %d) = %d\n", n+1, n+2, f, (*s)[n+1], g)
|
||||
if g != 1 {
|
||||
panic("oh no!")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// gcd copied from greatest common divisor task
|
||||
func gcd(x, y int) int {
|
||||
for y != 0 {
|
||||
x, y = y, x%y
|
||||
}
|
||||
return x
|
||||
}
|
||||
74
Task/Stern-Brocot-sequence/Go/stern-brocot-sequence-2.go
Normal file
74
Task/Stern-Brocot-sequence/Go/stern-brocot-sequence-2.go
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
// SB implements the Stern-Brocot sequence.
|
||||
//
|
||||
// Generator() satisfies RC Task 1. For remaining tasks, Generator could be
|
||||
// used but FirstN(), and Find() are simpler methods for specific stopping
|
||||
// criteria. FirstN and Find might also be considered to satisfy Task 1,
|
||||
// in which case Generator would not really be needed. Anyway, there it is.
|
||||
package sb
|
||||
|
||||
// Seq represents an even number of terms of a Stern-Brocot sequence.
|
||||
//
|
||||
// Terms are stored in a slice. Terms start with 1.
|
||||
// (Specifically, the zeroth term, 0, given in OEIS A002487 is not represented.)
|
||||
// Term 1 (== 1) is stored at slice index 0.
|
||||
//
|
||||
// Methods on Seq rely on Seq always containing an even number of terms.
|
||||
type Seq []int
|
||||
|
||||
// New returns a Seq with the two base terms.
|
||||
func New() *Seq {
|
||||
return &Seq{1, 1} // Step 1 of the RC task.
|
||||
}
|
||||
|
||||
// TwoMore appends two more terms to p.
|
||||
// It's the body of the loop in the RC algorithm.
|
||||
// Generate(), FirstN(), and Find() wrap this body in different ways.
|
||||
func (p *Seq) TwoMore() {
|
||||
s := *p
|
||||
n := len(s) / 2 // Steps 2 and 5 of the RC task.
|
||||
c := s[n]
|
||||
*p = append(s, c+s[n-1], c) // Steps 3 and 4 of the RC task.
|
||||
}
|
||||
|
||||
// Generator returns a generator function that returns successive terms
|
||||
// (until overflow.)
|
||||
func Generator() func() int {
|
||||
n := 0
|
||||
p := New()
|
||||
return func() int {
|
||||
if len(*p) == n {
|
||||
p.TwoMore()
|
||||
}
|
||||
t := (*p)[n]
|
||||
n++
|
||||
return t
|
||||
}
|
||||
}
|
||||
|
||||
// FirstN lazily extends p as needed so that it has at least n terms.
|
||||
// FirstN then returns a list of the first n terms.
|
||||
func (p *Seq) FirstN(n int) []int {
|
||||
for len(*p) < n {
|
||||
p.TwoMore()
|
||||
}
|
||||
return []int((*p)[:n])
|
||||
}
|
||||
|
||||
// Find lazily extends p as needed until it contains the value x
|
||||
// Find then returns the slice index of x in p.
|
||||
func (p *Seq) Find(x int) int {
|
||||
for n, f := range *p {
|
||||
if f == x {
|
||||
return n
|
||||
}
|
||||
}
|
||||
for {
|
||||
p.TwoMore()
|
||||
switch x {
|
||||
case (*p)[len(*p)-2]:
|
||||
return len(*p) - 2
|
||||
case (*p)[len(*p)-1]:
|
||||
return len(*p) - 1
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue