First power of 2 that has leading decimal digits of 12: Difference between revisions

julia example
(Undo revision 295493 by Wherrera (talk))
(julia example)
Line 209:
Took 1.422321658s
</pre>
 
=={{header|Julia}}==
<lang julia>function p(L, n)
@assert(L > 0 && n > 0)
places, logof2, logofL = trunc(log(10, L)), log(10, 2), log(10, L)
ldiv, nfound = log(2, 10) / log(2, 2), 0
for i in 1:typemax(Int)
if L == trunc(10^( ((i * logof2)[1] % 1) + places)) && (nfound += 1) == n
return i
end
end
end
 
for (L, n) in [(12, 1), (12, 2), (123, 45), (123, 12345), (123, 678910)]
println("With L = $L and n = $n, p(L, n) = ", p(L, n))
end
</lang>{{out}}
<pre>
With L = 12 and n = 1, p(L, n) = 7
With L = 12 and n = 2, p(L, n) = 80
With L = 123 and n = 45, p(L, n) = 12710
With L = 123 and n = 12345, p(L, n) = 3510491
With L = 123 and n = 678910, p(L, n) = 193060223
</pre>
 
 
=={{header|Pascal}}==
4,105

edits