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

m
syntax highlighting fixup automation
(First power of 2 that has leading decimal digits of 12 in Yabasic)
m (syntax highlighting fixup automation)
Line 39:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F p(=l, n, pwr = 2)
l = Int(abs(l))
V digitcount = floor(log(l, 10))
Line 52:
 
L(l, n) [(12, 1), (12, 2), (123, 45), (123, 12345), (123, 678910)]
print(‘p(’l‘, ’n‘) = ’p(l, n))</langsyntaxhighlight>
 
{{out}}
Line 66:
As wih the Go second sample, computes approximate integer values for the powers of 2.
<br>Requires LONG INT to be at least 64 bits (as in Algol 68G).
<langsyntaxhighlight lang="algol68"># find values of p( L, n ) where p( L, n ) is the nth-smallest j such that #
# the decimal representation of 2^j starts with the digits of L #
BEGIN
Line 115:
print p( 123, 12345 );
print p( 123, 678910 )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 126:
 
=={{header|BASIC256}}==
<langsyntaxhighlight BASIC256lang="basic256">global FAC
FAC = 0.30102999566398119521373889472449302677
 
Line 149:
end while
return j
end function</langsyntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
Line 155:
=={{header|C}}==
{{trans|Java}}
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
 
Line 191:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>p(12, 1) = 7
Line 203:
{{trans|Go}}
{{trans|Pascal}}
<langsyntaxhighlight lang="cpp">// a mini chrestomathy solution
 
#include <string>
Line 290:
doOne("go integer", gi);
doOne("pascal alternative", pa);
}</langsyntaxhighlight>
{{out}}Exeution times from Tio.run, language: C++ (clang) or C++ (gcc), compiler flags: -O3 -std=c++17
<pre>java simple version:
Line 334:
=={{header|C sharp|C#}}==
{{trans|C++}}
<langsyntaxhighlight lang="cpp">// a mini chrestomathy solution
 
using System;
Line 417:
doOne("pascal alternative", pa);
}
}</langsyntaxhighlight>
{{out}}
Results on the core i7-7700 @ 3.6Ghz.
Line 449:
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang="d">import std.math;
import std.stdio;
 
Line 483:
runTest(123, 12345);
runTest(123, 678910);
}</langsyntaxhighlight>
{{out}}
<pre>p(12, 1) = 7
Line 494:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// First power of 2 that has specified leading decimal digits. Nigel Galloway: March 14th., 2021
let fG n g=let fN l=let l=10.0**(l-floor l) in l>=n && l<g in let f=log10 2.0 in seq{1..0x0FFFFFFF}|>Seq.filter(float>>(*)f>>fN)
Line 504:
printfn "p(123,12345)->%d" (int(Seq.item 12344 (fG 1.23 1.24)))
printfn "p(123,678910)->%d" (int(Seq.item 678909 (fG 1.23 1.24)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 520:
{{trans|Pascal}}
{{works with|Factor|0.99 2019-10-06}}
<langsyntaxhighlight lang="factor">USING: formatting fry generalizations kernel literals math
math.functions math.parser sequences tools.time ;
 
Line 541:
123 678910
[ 2dup p "%d %d p = %d\n" printf ] 2 5 mnapply
] time</langsyntaxhighlight>
{{out}}
<pre>
Line 553:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define FAC 0.30102999566398119521373889472449302677
 
function p( L as uinteger, n as uinteger ) as uinteger
Line 575:
print p(123, 45)
print p(123, 12345)
print p(123, 678910)</langsyntaxhighlight>
{{out}}
<pre>
Line 586:
=={{header|Go}}==
{{trans|Pascal}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 629:
}
fmt.Printf("\nTook %s\n", time.Since(start))
}</langsyntaxhighlight>
 
{{out}}
Line 643:
 
or, translating the alternative Pascal version as well, for good measure:
<langsyntaxhighlight lang="go">package main
 
import (
Line 709:
}
fmt.Printf("\nTook %s\n", time.Since(start))
}</langsyntaxhighlight>
 
{{out}}
Line 724:
=={{header|Haskell}}==
{{trans|Python}}
<langsyntaxhighlight lang="haskell">import Control.Monad (guard)
import Text.Printf (printf)
 
Line 741:
main :: IO ()
main = mapM_ (\(l, n) -> printf "p(%d, %d) = %d\n" l n (p l n))
[(12, 1), (12, 2), (123, 45), (123, 12345), (123, 678910)]</langsyntaxhighlight>
 
Which, desugaring a little from Control.Monad (guard) and the do syntax, could also be rewritten as:
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
 
p :: Int -> Int -> Int
Line 774:
(123, 12345),
(123, 678910)
]</langsyntaxhighlight>
 
{{out}}
Line 785:
=={{header|J}}==
Modeled on the python version. Depending on the part of speech defined, the variable names x, y, u, v, m, and n can have special meaning within explicit code. They are defined by the arguments. In the fonts I usually use, lower case "l" is troublesome as well.
<syntaxhighlight lang="j">
<lang J>
p=: adverb define
:
Line 802:
raised
)
</syntaxhighlight>
</lang>
 
<pre>
Line 814:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
public class FirstPowerOfTwo {
 
Line 849:
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 870:
 
(Starting with (8+28) and tapering off to 8 is insufficient.)
<langsyntaxhighlight lang="jq">def normalize_base($base):
def n:
if length == 1 and .[0] < $base then .[0]
Line 909:
[[12, 1], [12, 2], [123, 45], [123, 12345], [123, 678910]][]
| "With L = \(.[0]) and n = \(.[1]), p(L, n) = \( p(.[0]; .[1]))"
</syntaxhighlight>
</lang>
{{out}}
As for Julia.
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function p(L, n)
@assert(L > 0 && n > 0)
places, logof2, nfound = trunc(log(10, L)), log(10, 2), 0
Line 926:
println("With L = $L and n = $n, p(L, n) = ", p(L, n))
end
</langsyntaxhighlight>{{out}}
<pre>
With L = 12 and n = 1, p(L, n) = 7
Line 937:
===Faster version===
{{trans|Go}}
<langsyntaxhighlight lang="julia">using Formatting, BenchmarkTools
 
function p(L, n)
Line 977:
testpLn(true)
@btime testpLn(false)
</langsyntaxhighlight>{{out}}
<pre>
With L = 12 and n = 1, p(L, n) = 7
Line 989:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import kotlin.math.ln
import kotlin.math.pow
 
Line 1,023:
}
return test
}</langsyntaxhighlight>
{{out}}
<pre>p(12, 1) = 7
Line 1,033:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Without compilation arbitrary precision will be used, and the algorithm will be slow, compiled is much faster:
<langsyntaxhighlight Mathematicalang="mathematica">f = Compile[{{ll, _Integer}, {n, _Integer}},
Module[{l, digitcount, log10power, raised, found, firstdigits, pwr = 2},
l = Abs[ll];
Line 1,054:
f[123, 45]
f[123, 12345]
f[123, 678910]</langsyntaxhighlight>
{{out}}
<pre>7
Line 1,065:
{{trans|Pascal}}
This is a translation to Nim of the very efficient Pascal alternative algorithm, with minor adjustments.
<langsyntaxhighlight Nimlang="nim">import math, strformat
 
const
Line 1,132:
findExp(123, 45)
findExp(123, 12345)
findExp(123, 678910)</langsyntaxhighlight>
 
{{out}}
Line 1,153:
<b>0<= base ** frac(x) < base </b> thats 1 digit before the comma<Br>
Only the first digits are needed.So I think, the accuracy is sufficient, because the results are the same :-)
<langsyntaxhighlight lang="pascal">program Power2FirstDigits;
 
uses
Line 1,226:
FindExp(12345, 123);
FindExp(678910, 123);
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,242:
Logarithm (Ln(Number/Digits)/ln(10) <= frac(i*lD10) < ln((Number+1)/Digits)/ln(10) => no exp<BR>
 
<langsyntaxhighlight lang="pascal">program Power2Digits;
uses
sysutils,strUtils;
Line 1,329:
 
FindExp(1,99);
end.</langsyntaxhighlight>
{{out}}
<pre>The 1th occurrence of 2 raised to a power whose product starts with "12" is 7
Line 1,345:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,407:
printf "%-15s %9s power of two (2^n) that starts with %5s is at n = %s\n", "p($prefix, $nth):",
comma($nth) . ordinal_digit($nth), "'$prefix'", comma p($prefix, $nth);
}</langsyntaxhighlight>
{{out}}
<pre>p(12, 1): 1st power of two (2^n) that starts with '12' is at n = 7
Line 1,417:
=={{header|Phix}}==
{{libheader|Phix/mpfr}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">L</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
Line 1,449:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,463:
Using logs, as seen first in the Pascal example.
 
<langsyntaxhighlight lang="python">from math import log, modf, floor
 
def p(l, n, pwr=2):
Line 1,480:
if __name__ == '__main__':
for l, n in [(12, 1), (12, 2), (123, 45), (123, 12345), (123, 678910)]:
print(f"p({l}, {n}) =", p(l, n))</langsyntaxhighlight>
 
{{out}}
Line 1,503:
===Untyped Racket===
 
<langsyntaxhighlight lang="racket">#lang racket
(define (fract-part f)
(- f (truncate f)))
Line 1,530:
(report-p 123 45)
(report-p 123 12345)
(report-p 123 678910))</langsyntaxhighlight>
 
{{out}}
Line 1,547:
===Typed Racket===
 
<langsyntaxhighlight lang="racket">#lang typed/racket
 
(: fract-part (-> Float Float))
Line 1,586:
(report-p 123 12345)
(report-p 123 678910))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,605:
Uses logs similar to Go and Pascal entries. Takes advantage of patterns in the powers to cut out a bunch of calculations.
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
constant $ln2ln10 = log(2) / log(10);
Line 1,640:
printf "%-15s %9s power of two (2^n) that starts with %5s is at n = %s\n", "p($prefix, $nth):",
comma($nth) ~ ordinal-digit($nth).substr(*-2), "'$prefix'", comma p($prefix, $nth);
}</langsyntaxhighlight>
{{out}}
<pre>p(12, 1): 1st power of two (2^n) that starts with '12' is at n = 7
Line 1,649:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program computes powers of two whose leading decimal digits are "12" (in base 10)*/
parse arg L n b . /*obtain optional arguments from the CL*/
if L=='' | L=="," then L= 12 /*Not specified? Then use the default.*/
Line 1,677:
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: arg _; do c=length(_)-3 to 1 by -3; _= insert(',', _, c); end; return _
th: arg _; return _ || word('th st nd rd', 1 +_//10 * (_//100 % 10\==1) * (_//10 <4))</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> 12 &nbsp; 1 </tt>}}
<pre>
Line 1,700:
=={{header|Ruby}}==
{{trans|C}}
<langsyntaxhighlight lang="ruby">def p(l, n)
test = 0
logv = Math.log(2.0) / Math.log(10.0)
Line 1,727:
runTest(123, 45)
runTest(123, 12345)
runTest(123, 678910)</langsyntaxhighlight>
{{out}}
<pre>P(12, 1) = 7
Line 1,736:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn power_of_two(l: isize, n: isize) -> isize {
let mut test: isize = 0;
let log: f64 = 2.0_f64.ln() / 10.0_f64.ln();
Line 1,770:
run_test(123, 678910);
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,782:
=={{header|Scala}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">object FirstPowerOfTwo {
def p(l: Int, n: Int): Int = {
var n2 = n
Line 1,814:
runTest(123, 678910)
}
}</langsyntaxhighlight>
{{out}}
<pre>p(12, 1) = 7
Line 1,823:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func farey_approximations(r, callback) {
 
var (a1 = r.int, b1 = 1)
Line 1,885:
for a,b in (tests) {
say "p(#{a}, #{b}) = #{p(a,b)}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,903:
===Slower Version===
 
<langsyntaxhighlight lang="swift">let ld10 = log(2.0) / log(10.0)
 
func p(L: Int, n: Int) -> Int {
Line 1,941:
for (l, n) in cases {
print("p(\(l), \(n)) = \(p(L: l, n: n))")
}</langsyntaxhighlight>
 
{{out}}
Line 1,953:
===Faster Version===
 
<langsyntaxhighlight lang="swift">import Foundation
 
func p2(L: Int, n: Int) -> Int {
Line 2,020:
for (l, n) in cases {
print("p(\(l), \(n)) = \(p2(L: l, n: n))")
}</langsyntaxhighlight>
 
{{out}}
Line 2,027:
 
=={{header|Visual Basic .NET}}==
<langsyntaxhighlight lang="vbnet">Module Module1
 
Function Func(ByVal l As Integer, ByVal n As Integer) As Long
Line 2,057:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>p( 12, 1) = 60
Line 2,071:
{{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.
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
import "/math" for Math
 
Line 2,099:
}
 
System.print("\nTook %(System.clock - start) seconds.")</langsyntaxhighlight>
 
{{out}}
Line 2,113:
 
=={{header|Yabasic}}==
<langsyntaxhighlight lang="yabasic">FAC = 0.30102999566398119521373889472449302677
 
print p(12, 1)
Line 2,135:
end while
return j
end sub</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|Pascal}}
Lots of float are slow so I've restricted the tests.
<langsyntaxhighlight lang="zkl">// float*int --> float and int*float --> int
fcn p(L,nth){ // 2^j = <L><digits>
var [const] ln10=(10.0).log(), ld10=(2.0).log() / ln10;
Line 2,149:
return(i);
}
}</langsyntaxhighlight>
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
GMP is just used to give some context on the size of the numbers we
are dealing with.
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
tests:=T( T(12,1),T(12,2), T(123,45),T(123,12345), );
foreach L,nth in (tests){
Line 2,159:
println("2^%-10,d is occurance %,d of 2^n == '%d<abc>' (%,d digits)"
.fmt(n,nth,L,BI(2).pow(n).len()));
}</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits