Pan base non-primes: Difference between revisions

added RPL
(Created Nim solution.)
(added RPL)
 
(7 intermediate revisions by 5 users not shown)
Line 1:
{{draft task}}
 
Primes are prime no matter which base they are expressed in. Some numeric strings are prime in a large number of bases. (Not the ''same'' prime, but '''a''' prime.)
Line 413:
Percent even up to and including 10000: 81.969343
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
if num mod 2 = 0
if num = 2
return 1
.
return 0
.
if num mod 3 = 0
if num = 3
return 1
.
return 0
.
i = 5
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
if num mod i = 0
return 0
.
i += 4
.
return 1
.
proc digits n . d[] .
while n > 0
d[] &= n mod 10
n = n div 10
.
.
proc fromdigits b . d[] n .
n = 0
for i = len d[] downto 1
n = n * b + d[i]
.
.
func panbasenpr n .
if n < 10
return 1 - isprim n
.
if n > 10 and n mod 10 = 0
return 1
.
digits n d[]
for i to len d[]
if maxdig < d[i]
maxdig = d[i]
.
.
for base = maxdig + 1 to n
fromdigits base d[] n
if isprim n = 1
return 0
.
.
return 1
.
print "First 50 prime pan-base composites:"
n = 2
repeat
if panbasenpr n = 1
cnt += 1
write n & " "
.
until cnt = 50
n += 1
.
cnt = 0
print "\n\nFirst 20 odd prime pan-base composites:"
n = 3
repeat
if panbasenpr n = 1
cnt += 1
write n & " "
.
until cnt = 20
n += 2
.
limit = 10000
cnt = 0
for n = 2 to limit
if panbasenpr n = 1
cnt += 1
if n mod 2 = 1
odd += 1
.
.
.
print "\nCount of pan-base composites up to and including " & limit & ": " & cnt
p = 100 * odd / cnt
print "Percent odd up to and including " & limit & ": " & p
print "Percent even up to and including " & limit & ": " & 100 - p
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">#include "isprime.bas"
 
Const lim As Integer = 2500
Dim As Integer pbnp(lim)
Dim As Integer n, base_, d, c, tc, oc, ec
Dim As String digits
Dim As Boolean composite
 
For n = 3 To lim
digits = Str(n)
composite = True
For base_ = 2 To n
d = 0
For c = 1 To Len(digits)
d = d * base_ + Val(Mid(digits, c, 1))
Next c
If IsPrime(d) Then
composite = False
Exit For
End If
Next base_
If composite Then
tc += 1
pbnp(tc) = n
If n Mod 2 <> 0 Then oc += 1
End If
Next n
 
ec = tc - oc
 
Print "First 50 pan-base composites:"
For n = 1 To 50
Print Using "### "; pbnp(n);
If n Mod 10 = 0 Then Print
Next n
 
Print !"\nFirst 20 odd pan-base composites:"
c = 0
For n = 1 To 115
If pbnp(n) Mod 2 Then
Print Using "### "; pbnp(n);
'Print Using "### "; odds(n);
c += 1
If c Mod 10 = 0 Then Print
End If
Next n
 
Print !"\nCount of pan-base_ composites up to and including "; lim; ": "; tc
Print Using "Number odd = ### or ##.######%"; oc; oc/tc*100
Print Using "Number even = ### or ##.######%"; ec; ec/tc*100
 
Sleep</syntaxhighlight>
{{out}}
<pre>Same as Wren entry.</pre>
 
=={{header|J}}==
Line 582 ⟶ 736:
Odd up to and including 2500: 161//953, or 16.89%.
Even up to and including 2500: 792//953, or 83.1%.
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
{{trans|Julia}}
<syntaxhighlight lang="Mathematica">(* Define the function to check if a number is a pan base composite *)
IsPanBaseComposite[n_] := Module[
{d = IntegerDigits[n], bases, compositeInAllBases},
bases = Range[Max[d] + 1, Max[10, n]];
compositeInAllBases = Not /@ PrimeQ[FromDigits[d, #] & /@ bases];
AllTrue[compositeInAllBases, Identity]
]
 
(* Generate the list of all pan base composites up to 2500 *)
panBase2500 = Select[Range[2, 2500], IsPanBaseComposite]
 
(* Filter out the odd numbers from the pan base composites *)
oddPanBase2500 = Select[panBase2500, OddQ]
 
(* Calculate the ratio of odd pan base composites to all pan base composites *)
ratio = Length[oddPanBase2500] / Length[panBase2500]
 
(* Print the first 50 pan base non-primes *)
Print["First 50 pan base non-primes:"]
Print[StringJoin[Riffle[ToString /@ Take[panBase2500, 50], ", "]]]
 
(* Print the first 20 odd pan base non-primes *)
Print["\nFirst 20 odd pan base non-primes:"]
Print[StringJoin[Riffle[ToString /@ Take[oddPanBase2500, 20], ", "]]]
 
(* Print the count of pan-base composites up to and including 2500 *)
Print["\nCount of pan-base composites up to and including 2500: ", Length[panBase2500]]
 
(* Print the ratios *)
Print["Odd up to and including 2500: ", Rationalize[ratio], ", or ", N[ratio * 100, 4], "%."]
Print["Even up to and including 2500: ", Rationalize[1 - ratio], ", or ", N[(1 - ratio) * 100, 4], "%."]</syntaxhighlight>
{{out}}
<pre>
First 50 pan base non-primes:
4, 6, 8, 9, 20, 22, 24, 26, 28, 30, 33, 36, 39, 40, 42, 44, 46, 48, 50, 55, 60, 62, 63, 64, 66, 68, 69, 70, 77, 80, 82, 84, 86, 88, 90, 93, 96, 99, 100, 110, 112, 114, 116, 118, 120, 121, 130, 132, 134, 136
 
First 20 odd pan base non-primes:
9, 33, 39, 55, 63, 69, 77, 93, 99, 121, 143, 165, 169, 187, 231, 253, 273, 275, 297, 299
 
Count of pan-base composites up to and including 2500: 953
Odd up to and including 2500: 161/953, or 16.894018887722980063`4.%.
Even up to and including 2500: 792/953, or 83.105981112277019937`4.%.
</pre>
 
Line 671 ⟶ 871:
Percent odd up to and including 10000: 18.030657
Percent even up to and including 10000: 81.969343
</pre>
 
 
=={{header|PARI/GP}}==
{{trans|Mathematica/Wolfram_Language}}
<syntaxhighlight lang="parigp">
/* Define the function to check if a number is not prime in all bases greater than its maximum digit */
is_pan_base_composite(n) = {
my(d = digits(n), base, isComp);
for (base = vecmax(d)+1, max(n, 10),
isComp = !isprime(fromdigits(d, base));
if (!isComp, return(0)); /* If number is prime in any base, return false */
);
return(1); /* Number is composite in all bases */
}
 
/* Generate the list of all pan base composites up to 2500 */
pan_base_2500 = select(is_pan_base_composite, [2..2500]);
 
/* Define a function to check if a number is odd */
is_odd(n) = n % 2 == 1;
 
/* Filter out the odd numbers from the pan base composites */
odd_pan_base_2500 = select(is_odd, pan_base_2500);
 
/* Calculate the ratio of odd pan base composites to all pan base composites */
ratio = #odd_pan_base_2500 / #pan_base_2500;
 
/* Print the first 50 pan base non-primes */
print("First 50 pan base non-primes:");
print(concat("", vector(min(#pan_base_2500, 50), i, Str(pan_base_2500[i]))));
 
/* Print the first 20 odd pan base non-primes */
print("\nFirst 20 odd pan base non-primes:");
print(concat("", vector(min(#odd_pan_base_2500, 20), i, Str(odd_pan_base_2500[i]))));
 
/* Print the count of pan-base composites up to and including 2500 */
print("\nCount of pan-base composites up to and including 2500: ", #pan_base_2500);
 
/* Print the ratios */
default(realprecision, 4)
print("Odd up to and including 2500: ", ratio, ", or ", ratio * 100.0, "%.");
print("Even up to and including 2500: ", 1 - ratio, ", or ", (1 - ratio) * 100.0, "%.");
</syntaxhighlight>
{{out}}
<pre>
First 50 pan base non-primes:
["4", "6", "8", "9", "20", "22", "24", "26", "28", "30", "33", "36", "39", "40", "42", "44", "46", "48", "50", "55", "60", "62", "63", "64", "66", "68", "69", "70", "77", "80", "82", "84", "86", "88", "90", "93", "96", "99", "100", "110", "112", "114", "116", "118", "120", "121", "130", "132", "134", "136"]
 
First 20 odd pan base non-primes:
["9", "33", "39", "55", "63", "69", "77", "93", "99", "121", "143", "165", "169", "187", "231", "253", "273", "275", "297", "299"]
 
Count of pan-base composites up to and including 2500: 953
Odd up to and including 2500: 161/953, or 16.89%.
Even up to and including 2500: 792/953, or 83.11%.
</pre>
 
Line 1,027 ⟶ 1,282:
Percent odd up to and including 2500: 16.894019
Percent even up to and including 2500: 83.105981</pre>
 
=={{header|RPL}}==
{{works with|RPL|HP-49C}}
« { } SWAP
'''WHILE''' DUP '''REPEAT''' 10 IDIV2 ROT + SWAP '''END'''
DROP
» '<span style="color:blue">→DIGL</span>' STO
« { } 3 ROT '''FOR''' n
1 CF
'''CASE'''
n 10 > LASTARG MOD NOT AND '''THEN''' 1 SF '''END'''
n
'''IF''' DUP 9 > '''THEN'''
<span style="color:blue">→DIGL</span>
'''IF''' DUP « GCD » STREAM 1 > '''THEN''' DROP 1 SF '''END'''
'''END'''
1 FC? '''THEN'''
1 SF
2 n '''FOR''' b
DUP
'''IF''' DUP TYPE 5 == '''THEN''' « SWAP b * + » STREAM '''END'''
'''IF''' ISPRIME? '''THEN''' 1 CF n 'b' STO '''END'''
'''NEXT''' DROP
'''END'''
'''END'''
'''IF''' 1 FS? '''THEN''' n + '''END'''
'''NEXT'''
» '<span style="color:blue">TASK</span>' STO
« DUP 2 MOD OVER IFT
→ pbnp odds
« pbnp 1 50 SUB
odds 1 20 SUB
pbnp SIZE "pbnp ≤ 1000" →TAG
odds SIZE pbnp SIZE / 100 * "% odd" →TAG
» » '<span style="color:blue">PBNPVU</span>' STO
 
1000 <span style="color:blue">TASK</span> <span style="color:blue">PBNPVU</span>
{{out}}
<pre>
4: { 4 6 8 9 20 22 24 26 28 30 33 36 39 40 42 44 46 48 50 55 60 62 63 64 66 68 69 70 77 80 82 84 86 88 90 93 96 99 100 110 112 114 116 118 120 121 130 132 134 136 }
3: { 9 33 39 55 63 69 77 93 99 121 143 165 169 187 231 253 273 275 297 299 }
2: pbnp ≤ 1000: 377.
1: % odd: 16.7108753316
</pre>
 
=={{header|Ruby}}==
Line 1,078 ⟶ 1,379:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
1,150

edits