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

m
Added Easylang
m (syntax highlighting fixup automation)
m (Added Easylang)
 
(5 intermediate revisions by 5 users not shown)
Line 334:
=={{header|C sharp|C#}}==
{{trans|C++}}
<syntaxhighlight lang="cppcsharp">// a mini chrestomathy solution
 
using System;
Line 492:
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/First_power_of_2_that_has_leading_decimal_digits_of_12#Pascal Pascal].
 
=={{header|EasyLang}}==
{{trans|Java}}
<syntaxhighlight>
func p l n .
log = log10 2
factor = 1
loop = l
while loop > 10
factor *= 10
loop = loop div 10
.
while n > 0
test += 1
val = floor (factor * pow 10 (test * log mod 1))
if val = l
n -= 1
.
.
return test
.
proc test l n . .
print "p(" & l & ", " & n & ") = " & p l n
.
test 12 1
test 12 2
test 123 45
test 123 12345
test 123 678910
</syntaxhighlight>
{{out}}
<pre>
p(12, 1) = 7
p(12, 2) = 80
p(123, 45) = 12710
p(123, 12345) = 3510491
p(123, 678910) = 193060223
</pre>
 
=={{header|F_Sharp|F#}}==
Line 516 ⟶ 554:
Real: 00:00:10.140
</pre>
 
=={{header|Factor}}==
A translation of the first Pascal example:
Line 583 ⟶ 622:
3510491
193060223</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn p( l as NSInteger, n as NSInteger ) as NSInteger
NSInteger test = 0, factor = 1, loop = l
double logv = log(2.0) / log(10.0)
while ( loop > 10 )
factor *= 10
loop /= 10
wend
while ( n > 0 )
NSInteger v
test++
v = (NSInteger)( factor * fn pow( 10.0, fn fmod( test * logv, 1 ) ) )
if ( v == l ) then n--
wend
end fn = test
 
void local fn RunTest( l as NSInteger, n as NSInteger )
printf @"fn p( %d, %d ) = %d", l, n, fn p( l, n )
end fn
 
fn RunTest( 12, 1 )
fn RunTest( 12, 2 )
fn RunTest( 123, 45 )
fn RunTest( 123, 12345 )
fn RunTest( 123, 678910 )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
fn p( 12, 1 ) = 7
fn p( 12, 2 ) = 80
fn p( 123, 45 ) = 12710
fn p( 123, 12345 ) = 3510491
fn p( 123, 678910 ) = 193060223
</pre>
 
=={{header|Gambas}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public Sub Main()
Print p(12, 1)
Print p(12, 2)
Print p(123, 45)
Print p(123, 12345)
Print p(123, 678910)
End
 
Function p(L As Integer, n As Integer) As Integer
 
Dim FAC As Float = 0.30102999566398119521373889472449302677
Dim count As Integer, j As Integer = 0
Dim x As Float, y As Float
Dim digits As String, LS As String = Str(L)
 
While count < n
j += 1
x = FAC * j
If x < Len(LS) Then Continue
y = 10 ^ (x - CInt(x))
y *= 10 ^ Len(LS)
digits = Str(y)
If Left(digits, Len(LS)) = LS Then count += 1
Wend
Return j
 
End Function</syntaxhighlight>
 
=={{header|Go}}==
Line 1,698 ⟶ 1,809:
The 678910th occurrence of 2 raised to a power whose product starts with "123' is 193,060,223.
</pre>
=={{header|RPL}}==
{{trans|C}}
{{works with|HP|48}}
The <code>6 RND</code> sequence is necessary on calculators, which do the math in simple precision. It can be removed when executing the program on a double precision emulator.
« SWAP DUP XPON ALOG 2 LOG → digits factor log2
« 0
'''WHILE''' OVER 0 > '''REPEAT'''
1 +
DUP log2 * FP ALOG 6 RND factor * IP
'''IF''' digits == '''THEN''' SWAP 1 - SWAP '''END'''
'''END'''
SWAP DROP
» » '<span style="color:blue">P</span>' STO
 
12 1 <span style="color:blue">P</span>
12 2 <span style="color:blue">P</span>
123 45 <span style="color:blue">P</span>
{{out}}
<pre>
3: 7
2: 80
1: 12710
</pre>
The last result needs 29 minutes on a basic HP-48SX.
 
=={{header|Ruby}}==
{{trans|C}}
Line 2,071 ⟶ 2,207:
{{libheader|Wren-math}}
Just the first version which has turned out to be much quicker than the Go entry (around 28 seconds on the same machine). Frankly, I've no idea why.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Math
 
var ld10 = Math.ln2 / Math.ln10
2,017

edits