Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |