First power of 2 that has leading decimal digits of 12: Difference between revisions
First power of 2 that has leading decimal digits of 12 (view source)
Revision as of 21:05, 15 January 2020
, 4 years ago→{{header|Go}}: Replaced previous solution with an infinitely better Pascal translation.
m (→{{header|Pascal}}: removed part after end.) |
(→{{header|Go}}: Replaced previous solution with an infinitely better Pascal translation.) |
||
Line 37:
=={{header|Go}}==
{{trans|Pascal}}
<lang go>package main
import (
"fmt"
"
)
var ld10 = math.Ln2 / math.Ln10
one := big.NewInt(1)▼
for count := 0; count < n; count++ {▼
power += shift▼
switch shift {▼
case 90:▼
shift = 485▼
}▼
}▼
return power▼
}▼
func p(L, n int) uint {▼
count := 0▼
i := big.NewInt(1)▼
for {▼
func commatize(n
s := fmt.Sprintf("%d", n)
le := len(s)
Line 120 ⟶ 55:
}
return s
▲}
▲ for i >= 10 {
▲ count := uint64(0)
e := math.Exp(math.Ln10 * math.Mod(float64(i)*ld10, 1))
if uint64(math.Trunc(e*float64(digits))) == L {
▲ }
}
func main() {
start := time.Now()
params := [][2]
for _, param := range params {
fmt.Printf("p(%d, %d) = %s\n", param[0], param[1], commatize(p(param[0], param[1])))
}
fmt.Printf("\nTook %s\n", time.Since(start))
}</lang>
Line 135 ⟶ 89:
p(123, 45) = 12,710
p(123, 12345) = 3,510,491
p(123, 678910) = 193,060,223
Took 38.015225244s
</pre>
=={{header|Pascal}}==
First convert 2**i -> 10**x => x= ln(2)/ln(10) *i<BR>
|