Multi-base primes: Difference between revisions

m
syntax highlighting fixup automation
m (Promote. multiple implementations, little controversy)
m (syntax highlighting fixup automation)
Line 48:
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.
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <cmath>
#include <cstdint>
Line 173:
multi_base_primes(max_base, max_length);
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 224:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Multi-base primes. Nigel Galloway: July 4th., 2021
let digits="0123456789abcdefghijklmnopqrstuvwxyz"
Line 232:
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 "")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 252:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: assocs assocs.extras formatting io kernel math
math.functions math.parser math.primes math.ranges present
sequences ;
Line 272:
[ dup (bases) filter "%d => %[%d, %]\n" printf ] each ;
 
4 [1,b] [ multibase. nl ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 295:
{{libheader|Go-rcu}}
This takes about 1.2 seconds and 31.3 seconds to process up to 5 and 6 character strings, respectively.
<langsyntaxhighlight lang="go">package main
 
import (
Line 391:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 439:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function maxprimebases(ndig, maxbase)
Line 467:
maxprimebases(n, 36)
end
</langsyntaxhighlight>{{out}}
<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:
Line 493:
 
=== Up to base 62 ===
<langsyntaxhighlight lang="julia">using Primes
 
function maxprimebases(ndig, maxbase)
Line 527:
maxprimebases(n, 62)
end
</langsyntaxhighlight>{{out}}
<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:
Line 566:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[OtherBasePrimes, OtherBasePrimesPower]
OtherBasePrimes[n_Integer] := Module[{digs, minbase, bases},
digs = IntegerDigits[n];
Line 585:
OtherBasePrimesPower[3] // Column
OtherBasePrimesPower[4] // Column
OtherBasePrimesPower[5] // Column</langsyntaxhighlight>
{{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}}
Line 607:
 
 
<langsyntaxhighlight Nimlang="nim">import math, sequtils, strutils
 
const
Line 674:
for m in ctx.maxStrings:
echo m.indices.mapIt(Digits[it]).join(), " → ", m[1].join(" ")
echo()</langsyntaxhighlight>
 
{{out}}
Line 701:
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>
<langsyntaxhighlight lang="pascal">program MAXBaseStringIsPrimeInBase;
{$IFDEF FPC}
{$MODE DELPHI}
Line 1,031:
writeln(' Converting ',(GetTickCount64-T0)/1000:6:3,' s');
{$IFDEF WINDOWS} readln; {$ENDIF}
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,094:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,122:
}
say ''
}</langsyntaxhighlight>
{{out}}
<pre>1 character strings that are prime in maximum bases: 34
Line 1,144:
=={{header|Phix}}==
Originally translated from Rust, but changed to a much fuller range of digits, as per talk page.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,276:
 
''All your base are belong to us. You have no chance to survive make your prime.''
<syntaxhighlight lang="raku" perl6line>use Math::Primesieve;
my $sieve = Math::Primesieve.new;
 
Line 1,298:
.say for %prime-base.grep( +*.value.elems == $e ).grep(*.key.chars == $m).sort: *.key;
say '';
}</langsyntaxhighlight>
 
{{out}}
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.
 
<syntaxhighlight lang="raku" perl6line>use Math::Primesieve;
use Base::Any;
 
Line 1,345:
.say for %prime-base.grep( +*.value.elems == $e ).grep(*.key.chars == $m).sort: *.key;
say '';
}</langsyntaxhighlight>
{{out|Yields}}
<pre>1 character strings that are prime in maximum bases: 60
Line 1,362:
 
=={{header|REXX}}==
<langsyntaxhighlight 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.*/
if widths=='' | widths=="," then widths= 5 /*Not specified? Then use the default.*/
Line 1,405:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= # + 1; @.#= j; sq.#= j*j /*bump # Ps; assign next P; P squared.*/
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,433:
=={{header|Rust}}==
{{trans|Julia}}
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
 
Line 1,484:
max_prime_bases(n, 36);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,513:
===Up to base 62===
{{trans|C++}}
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
 
Line 1,629:
println!("elapsed time: {} milliseconds", time.as_millis());
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,683:
=={{header|Sidef}}==
{{trans|Julia}}
<langsyntaxhighlight lang="ruby">func max_prime_bases(ndig, maxbase=36) {
 
var maxprimebases = [[]]
Line 1,711:
for n in (1..5) {
max_prime_bases(n)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,737:
{{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.
<langsyntaxhighlight lang="ecmascript">import "/math" for Int, Nums
var maxDepth = 5
Line 1,793:
printResults.call()
System.print()
}</langsyntaxhighlight>
 
{{out}}
10,327

edits