Multi-base primes: Difference between revisions

Content added Content deleted
m (Promote. multiple implementations, little controversy)
m (syntax highlighting fixup automation)
Line 48: Line 48:
Originally translated from [[#Wren|Wren]] with ideas borrowed from other solutions.
Originally translated from [[#Wren|Wren]] with ideas borrowed from other solutions.
The maximum base and number of characters can be specified as command line arguments.
The maximum base and number of characters can be specified as command line arguments.
<lang cpp>#include <algorithm>
<syntaxhighlight lang="cpp">#include <algorithm>
#include <cmath>
#include <cmath>
#include <cstdint>
#include <cstdint>
Line 173: Line 173:
multi_base_primes(max_base, max_length);
multi_base_primes(max_base, max_length);
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 224: Line 224:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Multi-base primes. Nigel Galloway: July 4th., 2021
// Multi-base primes. Nigel Galloway: July 4th., 2021
let digits="0123456789abcdefghijklmnopqrstuvwxyz"
let digits="0123456789abcdefghijklmnopqrstuvwxyz"
Line 232: Line 232:
i|>Seq.iter(fun(n,g)->printf " %s->" (n|>List.map(fun n->digits.[n])|>Array.ofList|>System.String)
i|>Seq.iter(fun(n,g)->printf " %s->" (n|>List.map(fun n->digits.[n])|>Array.ofList|>System.String)
g|>Seq.iter(fun(g,_)->printf "%d " g); printfn ""); printfn "")
g|>Seq.iter(fun(g,_)->printf "%d " g); printfn ""); printfn "")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 252: Line 252:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: assocs assocs.extras formatting io kernel math
<syntaxhighlight lang="factor">USING: assocs assocs.extras formatting io kernel math
math.functions math.parser math.primes math.ranges present
math.functions math.parser math.primes math.ranges present
sequences ;
sequences ;
Line 272: Line 272:
[ dup (bases) filter "%d => %[%d, %]\n" printf ] each ;
[ dup (bases) filter "%d => %[%d, %]\n" printf ] each ;


4 [1,b] [ multibase. nl ] each</lang>
4 [1,b] [ multibase. nl ] each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 295: Line 295:
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
This takes about 1.2 seconds and 31.3 seconds to process up to 5 and 6 character strings, respectively.
This takes about 1.2 seconds and 31.3 seconds to process up to 5 and 6 character strings, respectively.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 391: Line 391:
fmt.Println()
fmt.Println()
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 439: Line 439:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


function maxprimebases(ndig, maxbase)
function maxprimebases(ndig, maxbase)
Line 467: Line 467:
maxprimebases(n, 36)
maxprimebases(n, 36)
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
The maximum number of prime valued bases for base 10 numeric strings of length 1 is 34. The base 10 value list of this is:
The maximum number of prime valued bases for base 10 numeric strings of length 1 is 34. The base 10 value list of this is:
Line 493: Line 493:


=== Up to base 62 ===
=== Up to base 62 ===
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


function maxprimebases(ndig, maxbase)
function maxprimebases(ndig, maxbase)
Line 527: Line 527:
maxprimebases(n, 62)
maxprimebases(n, 62)
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
The maximum number of prime valued bases for base up to 36 numeric strings of length 1 is 34. The value list of this is:
The maximum number of prime valued bases for base up to 36 numeric strings of length 1 is 34. The value list of this is:
Line 566: Line 566:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[OtherBasePrimes, OtherBasePrimesPower]
<syntaxhighlight lang="mathematica">ClearAll[OtherBasePrimes, OtherBasePrimesPower]
OtherBasePrimes[n_Integer] := Module[{digs, minbase, bases},
OtherBasePrimes[n_Integer] := Module[{digs, minbase, bases},
digs = IntegerDigits[n];
digs = IntegerDigits[n];
Line 585: Line 585:
OtherBasePrimesPower[3] // Column
OtherBasePrimesPower[3] // Column
OtherBasePrimesPower[4] // Column
OtherBasePrimesPower[4] // Column
OtherBasePrimesPower[5] // Column</lang>
OtherBasePrimesPower[5] // Column</syntaxhighlight>
{{out}}
{{out}}
<pre>{2,{3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36}}
<pre>{2,{3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36}}
Line 607: Line 607:




<lang Nim>import math, sequtils, strutils
<syntaxhighlight lang="nim">import math, sequtils, strutils


const
const
Line 674: Line 674:
for m in ctx.maxStrings:
for m in ctx.maxStrings:
echo m.indices.mapIt(Digits[it]).join(), " → ", m[1].join(" ")
echo m.indices.mapIt(Digits[it]).join(), " → ", m[1].join(" ")
echo()</lang>
echo()</syntaxhighlight>


{{out}}
{{out}}
Line 701: Line 701:
First counting the bases that convert a MAXBASE string of n into a prime number.<BR>
First counting the bases that convert a MAXBASE string of n into a prime number.<BR>
Afterwards only checking the maxcount for the used bases.<BR>
Afterwards only checking the maxcount for the used bases.<BR>
<lang pascal>program MAXBaseStringIsPrimeInBase;
<syntaxhighlight lang="pascal">program MAXBaseStringIsPrimeInBase;
{$IFDEF FPC}
{$IFDEF FPC}
{$MODE DELPHI}
{$MODE DELPHI}
Line 1,031: Line 1,031:
writeln(' Converting ',(GetTickCount64-T0)/1000:6:3,' s');
writeln(' Converting ',(GetTickCount64-T0)/1000:6:3,' s');
{$IFDEF WINDOWS} readln; {$ENDIF}
{$IFDEF WINDOWS} readln; {$ENDIF}
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,094: Line 1,094:
{{trans|Raku}}
{{trans|Raku}}
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 1,122: Line 1,122:
}
}
say ''
say ''
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 character strings that are prime in maximum bases: 34
<pre>1 character strings that are prime in maximum bases: 34
Line 1,144: Line 1,144:
=={{header|Phix}}==
=={{header|Phix}}==
Originally translated from Rust, but changed to a much fuller range of digits, as per talk page.
Originally translated from Rust, but changed to a much fuller range of digits, as per talk page.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">maxbase</span><span style="color: #0000FF;">=</span><span style="color: #000000;">36</span> <span style="color: #000080;font-style:italic;">-- or 62</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">maxbase</span><span style="color: #0000FF;">=</span><span style="color: #000000;">36</span> <span style="color: #000080;font-style:italic;">-- or 62</span>
Line 1,222: Line 1,222:
<span style="color: #000000;">max_prime_bases</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">maxbase</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">max_prime_bases</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">maxbase</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,276: Line 1,276:


''All your base are belong to us. You have no chance to survive make your prime.''
''All your base are belong to us. You have no chance to survive make your prime.''
<lang perl6>use Math::Primesieve;
<syntaxhighlight lang="raku" line>use Math::Primesieve;
my $sieve = Math::Primesieve.new;
my $sieve = Math::Primesieve.new;


Line 1,298: Line 1,298:
.say for %prime-base.grep( +*.value.elems == $e ).grep(*.key.chars == $m).sort: *.key;
.say for %prime-base.grep( +*.value.elems == $e ).grep(*.key.chars == $m).sort: *.key;
say '';
say '';
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,322: Line 1,322:
You can't really assume that the maximum string will be all numeric digits. It is just an accident that they happen to work out that way with a upper limit of base 36. If we do the same filtering using a maximum of base 62, we end up with several that contain alphabetics.
You can't really assume that the maximum string will be all numeric digits. It is just an accident that they happen to work out that way with a upper limit of base 36. If we do the same filtering using a maximum of base 62, we end up with several that contain alphabetics.


<lang perl6>use Math::Primesieve;
<syntaxhighlight lang="raku" line>use Math::Primesieve;
use Base::Any;
use Base::Any;


Line 1,345: Line 1,345:
.say for %prime-base.grep( +*.value.elems == $e ).grep(*.key.chars == $m).sort: *.key;
.say for %prime-base.grep( +*.value.elems == $e ).grep(*.key.chars == $m).sort: *.key;
say '';
say '';
}</lang>
}</syntaxhighlight>
{{out|Yields}}
{{out|Yields}}
<pre>1 character strings that are prime in maximum bases: 60
<pre>1 character strings that are prime in maximum bases: 60
Line 1,362: Line 1,362:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds primes whose values in other bases (2──►36) have the most diff. bases. */
<syntaxhighlight lang="rexx">/*REXX pgm finds primes whose values in other bases (2──►36) have the most diff. bases. */
parse arg widths . /*obtain optional argument from the CL.*/
parse arg widths . /*obtain optional argument from the CL.*/
if widths=='' | widths=="," then widths= 5 /*Not specified? Then use the default.*/
if widths=='' | widths=="," then widths= 5 /*Not specified? Then use the default.*/
Line 1,405: Line 1,405:
end /*k*/ /* [↑] only process numbers ≤ √ J */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= # + 1; @.#= j; sq.#= j*j /*bump # Ps; assign next P; P squared.*/
#= # + 1; @.#= j; sq.#= j*j /*bump # Ps; assign next P; P squared.*/
end /*j*/; return</lang>
end /*j*/; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 1,433: Line 1,433:
=={{header|Rust}}==
=={{header|Rust}}==
{{trans|Julia}}
{{trans|Julia}}
<lang rust>// [dependencies]
<syntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
// primal = "0.3"


Line 1,484: Line 1,484:
max_prime_bases(n, 36);
max_prime_bases(n, 36);
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,513: Line 1,513:
===Up to base 62===
===Up to base 62===
{{trans|C++}}
{{trans|C++}}
<lang rust>// [dependencies]
<syntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
// primal = "0.3"


Line 1,629: Line 1,629:
println!("elapsed time: {} milliseconds", time.as_millis());
println!("elapsed time: {} milliseconds", time.as_millis());
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,683: Line 1,683:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Julia}}
{{trans|Julia}}
<lang ruby>func max_prime_bases(ndig, maxbase=36) {
<syntaxhighlight lang="ruby">func max_prime_bases(ndig, maxbase=36) {


var maxprimebases = [[]]
var maxprimebases = [[]]
Line 1,711: Line 1,711:
for n in (1..5) {
for n in (1..5) {
max_prime_bases(n)
max_prime_bases(n)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,737: Line 1,737:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
This takes about 1.6 seconds to process up to 4 character strings and 58 seconds for the extra credit which is not too bad for the Wren interpreter.
This takes about 1.6 seconds to process up to 4 character strings and 58 seconds for the extra credit which is not too bad for the Wren interpreter.
<lang ecmascript>import "/math" for Int, Nums
<syntaxhighlight lang="ecmascript">import "/math" for Int, Nums
var maxDepth = 5
var maxDepth = 5
Line 1,793: Line 1,793:
printResults.call()
printResults.call()
System.print()
System.print()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}