Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
30
Task/Self-describing-numbers/Go/self-describing-numbers-1.go
Normal file
30
Task/Self-describing-numbers/Go/self-describing-numbers-1.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// task 1 requirement
|
||||
func sdn(n int64) bool {
|
||||
if n >= 1e10 {
|
||||
return false
|
||||
}
|
||||
s := strconv.FormatInt(n, 10)
|
||||
for d, p := range s {
|
||||
if int(p)-'0' != strings.Count(s, strconv.Itoa(d)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// task 2 code (takes a while to run)
|
||||
func main() {
|
||||
for n := int64(0); n < 1e10; n++ {
|
||||
if sdn(n) {
|
||||
fmt.Println(n)
|
||||
}
|
||||
}
|
||||
}
|
||||
56
Task/Self-describing-numbers/Go/self-describing-numbers-2.go
Normal file
56
Task/Self-describing-numbers/Go/self-describing-numbers-2.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func selfDesc(n uint64) bool {
|
||||
if n >= 1e10 {
|
||||
return false
|
||||
}
|
||||
s := strconv.FormatUint(n, 10)
|
||||
for d, p := range s {
|
||||
if int(p)-'0' != strings.Count(s, strconv.Itoa(d)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func main() {
|
||||
start := time.Now()
|
||||
fmt.Println("The self-describing numbers are:")
|
||||
i := uint64(10) // self-describing number must end in 0
|
||||
pw := uint64(10) // power of 10
|
||||
fd := uint64(1) // first digit
|
||||
sd := uint64(1) // second digit
|
||||
dg := uint64(2) // number of digits
|
||||
mx := uint64(11) // maximum for current batch
|
||||
lim := uint64(9_100_000_001) // sum of digits can't be more than 10
|
||||
for i < lim {
|
||||
if selfDesc(i) {
|
||||
secs := time.Since(start).Seconds()
|
||||
fmt.Printf("%d (in %.1f secs)\n", i, secs)
|
||||
}
|
||||
i += 10
|
||||
if i > mx {
|
||||
fd++
|
||||
sd--
|
||||
if sd >= 0 {
|
||||
i = fd * pw
|
||||
} else {
|
||||
pw *= 10
|
||||
dg++
|
||||
i = pw
|
||||
fd = 1
|
||||
sd = dg - 1
|
||||
}
|
||||
mx = i + sd*pw/10
|
||||
}
|
||||
}
|
||||
osecs := time.Since(start).Seconds()
|
||||
fmt.Printf("\nTook %.1f secs overall\n", osecs)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue