Skip to content

Commit

Permalink
24 & 25
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoiron committed Mar 29, 2012
1 parent c87e9f3 commit 8b59fdf
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
55 changes: 55 additions & 0 deletions 024.go
@@ -0,0 +1,55 @@
package main

import ("fmt"; "sort")

func next(nums []int) []int {

last := len(nums)-1
tail := 0
for i:=1; i<=last; i++ {
if nums[i] > nums[i-1] { break }
if i == last && nums[i] < nums[i-1] { return nil }
}
/* find the longest tail sorted in decreasing order */
for tail = last; nums[tail-1] > nums[tail] && tail > 0; tail-- {}

//fmt.Println("Tail", tail)

if tail == last {
nums[last], nums[last-1] = nums[last-1], nums[last]
return nums
}

if tail >= 1 {
min := 10
mini := 0
token := nums[tail-1]
for i:=tail; i<len(nums); i++ {
if nums[i] > token && nums[i] < min {
min = nums[i]
mini = i
}
}
nums[tail-1], nums[mini] = nums[mini], nums[tail-1]
sort.Ints(nums[tail:])
return nums

}

return nil
}

func main() {
nums := []int{0,1,2,3,4,5,6,7,8,9}
for i:=1; nums != nil; i++ {
//fmt.Println(i, nums)
if i == 1000000 {
for f:=0; f<len(nums); f++ {
fmt.Print(nums[f])
}
fmt.Println()
return
}
nums = next(nums)
}
}
42 changes: 42 additions & 0 deletions 025.go
@@ -0,0 +1,42 @@
package main

import (
"fmt"
"math/big"
)

// generate an endless amt of fibonacci numbers
func fib(ch chan int) {
send := func(i big.Int) {
ch <- len(i.String())
}

fibseq := [3]big.Int{}
fibseq[0].Set(big.NewInt(1))
fibseq[1].Set(big.NewInt(1))
fibseq[2].Set(big.NewInt(2))
send(fibseq[0])
send(fibseq[1])
send(fibseq[2])
for {
fibseq[0].Set(&fibseq[1])
fibseq[1].Set(&fibseq[2])
fibseq[2].Add(&fibseq[0], &fibseq[1])
send(fibseq[2])
}

}

// find the first fib w/ 1000 digits & print which fib it is
func main() {
ord := 1
ch := make(chan int, 100)
go fib(ch)
for next := range ch {
if next >= 1000 {
break
}
ord++
}
fmt.Println(ord)
}

0 comments on commit 8b59fdf

Please sign in to comment.