Primes which contain only one odd digit: Difference between revisions

Content added Content deleted
(→‎{{header|Go}}: Stretched to primes under 10 billion.)
m (syntax highlighting fixup automation)
Line 12: Line 12:
{{trans|Nim}}
{{trans|Nim}}


<lang 11l>F is_prime(n)
<syntaxhighlight lang="11l">F is_prime(n)
I n == 2
I n == 2
R 1B
R 1B
Line 39: Line 39:
I hasLastDigitOdd(n) & is_prime(n)
I hasLastDigitOdd(n) & is_prime(n)
count++
count++
print("\nFound "count‘ primes with only one odd digit below 1000000.’)</lang>
print("\nFound "count‘ primes with only one odd digit below 1000000.’)</syntaxhighlight>


{{out}}
{{out}}
Line 55: Line 55:
=={{header|Action!}}==
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
{{libheader|Action! Sieve of Eratosthenes}}
<lang Action!>INCLUDE "H6:SIEVE.ACT"
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"


BYTE FUNC OddDigitsCount(INT x)
BYTE FUNC OddDigitsCount(INT x)
Line 86: Line 86:
OD
OD
PrintF("%E%EThere are %I primes",count)
PrintF("%E%EThere are %I primes",count)
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Primes_which_contain_only_one_odd_digit.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Primes_which_contain_only_one_odd_digit.png Screenshot from Atari 8-bit computer]
Line 98: Line 98:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find primes whose decimal representation contains only one odd digit #
<syntaxhighlight lang="algol68">BEGIN # find primes whose decimal representation contains only one odd digit #
# sieve the primes to 1 000 000 #
# sieve the primes to 1 000 000 #
PR read "primes.incl.a68" PR
PR read "primes.incl.a68" PR
Line 132: Line 132:
OD;
OD;
show total( p1odd count, UPB prime, "single-odd-digit" )
show total( p1odd count, UPB prime, "single-odd-digit" )
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 146: Line 146:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PRIMES_WHICH_CONTAIN_ONLY_ONE_ODD_NUMBER.AWK
# syntax: GAWK -f PRIMES_WHICH_CONTAIN_ONLY_ONE_ODD_NUMBER.AWK
BEGIN {
BEGIN {
Line 176: Line 176:
return(1)
return(1)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 199: Line 199:
=={{header|C#|CSharp}}==
=={{header|C#|CSharp}}==
Modifies a conventional prime sieve to cull the items with more than one odd digit.
Modifies a conventional prime sieve to cull the items with more than one odd digit.
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;


Line 246: Line 246:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>List of one-odd-digit primes < 1,000:
<pre>List of one-odd-digit primes < 1,000:
Line 267: Line 267:
=={{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">
// Primes which contain only one odd number. Nigel Galloway: July 28th., 2021
// Primes which contain only one odd number. Nigel Galloway: July 28th., 2021
let rec fN g=function 2->false |n when g=0->n=1 |n->fN (g/10) (n+g%2)
let rec fN g=function 2->false |n when g=0->n=1 |n->fN (g/10) (n+g%2)
primes32()|>Seq.takeWhile((>)1000)|>Seq.filter(fun g->fN g 0)|>Seq.iter(printf "%d "); printfn ""
primes32()|>Seq.takeWhile((>)1000)|>Seq.filter(fun g->fN g 0)|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 280: Line 280:
{{libheader|Factor-numspec}}
{{libheader|Factor-numspec}}
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: grouping io lists lists.lazy literals math math.primes
<syntaxhighlight lang="factor">USING: grouping io lists lists.lazy literals math math.primes
numspec prettyprint ;
numspec prettyprint ;


Line 301: Line 301:


"\nCount of such primes under 1,000,000,000:" print
"\nCount of such primes under 1,000,000,000:" print
p [ 1,000,000,000 < ] lwhile llength .</lang>
p [ 1,000,000,000 < ] lwhile llength .</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 319: Line 319:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>
<syntaxhighlight lang="freebasic">
#include "isprime.bas"
#include "isprime.bas"


Line 349: Line 349:
wend
wend


print "There are ";count;" such primes below one million."</lang>
print "There are ";count;" such primes below one million."</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
{{trans|Wren}}
{{trans|Wren}}
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 409: Line 409:
}
}
fmt.Printf("There are %7s such primes under %s\n", rcu.Commatize(count), rcu.Commatize(pow))
fmt.Printf("There are %7s such primes under %s\n", rcu.Commatize(count), rcu.Commatize(pow))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 435: Line 435:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List (intercalate, maximum, transpose)
<syntaxhighlight lang="haskell">import Data.List (intercalate, maximum, transpose)
import Data.List.Split (chunksOf)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (primes)
import Data.Numbers.Primes (primes)
Line 474: Line 474:
let ws = maximum . fmap length <$> transpose rows
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</lang>
in unlines $ intercalate gap . zipWith pw ws <$> rows</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Below 1000:
<pre>Below 1000:
Line 495: Line 495:


===Fast solution===
===Fast solution===
<lang jq>### Preliminaries
<syntaxhighlight lang="jq">### Preliminaries


def count(s): reduce s as $x (null; .+1);
def count(s): reduce s as $x (null; .+1);
Line 527: Line 527:
emit_until(. > 1000; primes_with_exactly_one_odd_digit),
emit_until(. > 1000; primes_with_exactly_one_odd_digit),


"\nThe number of primes less than 1000000 with exactly one odd digits is \(count(emit_until(. > 1000000; primes_with_exactly_one_odd_digit)))."</lang>
"\nThe number of primes less than 1000000 with exactly one odd digits is \(count(emit_until(. > 1000000; primes_with_exactly_one_odd_digit)))."</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 579: Line 579:
</pre>
</pre>
===A simpler but slower solution===
===A simpler but slower solution===
<lang jq># Input is assumed to be prime.
<syntaxhighlight lang="jq"># Input is assumed to be prime.
# So we only need check the other digits are all even.
# So we only need check the other digits are all even.
def prime_has_exactly_one_odd_digit:
def prime_has_exactly_one_odd_digit:
Line 590: Line 590:
# It is much faster to check for primality afterwards.
# It is much faster to check for primality afterwards.
range(3; infinite; 2)
range(3; infinite; 2)
| select(prime_has_exactly_one_odd_digit and is_prime);</lang>
| select(prime_has_exactly_one_odd_digit and is_prime);</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
If only one digit of a prime is odd, then that odd digit is the ones place digit. We don't actually need to check for an odd first digit once we exclude 2.
If only one digit of a prime is odd, then that odd digit is the ones place digit. We don't actually need to check for an odd first digit once we exclude 2.
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


function isoneoddprime(n, base = 10)
function isoneoddprime(n, base = 10)
Line 607: Line 607:
println("\nThere are ", count(isoneoddprime, primes(1_000_000)),
println("\nThere are ", count(isoneoddprime, primes(1_000_000)),
" primes with only one odd digit in base 10 between 1 and 1,000,000.")
" primes with only one odd digit in base 10 between 1 and 1,000,000.")
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Found 45 primes with one odd digit in base 10:
Found 45 primes with one odd digit in base 10:
Line 619: Line 619:
</pre>
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>Labeled[Cases[
<syntaxhighlight lang="mathematica">Labeled[Cases[
NestWhileList[NextPrime,
NestWhileList[NextPrime,
2, # <
2, # <
Line 628: Line 628:
2, # <
2, # <
1000000 &], _?(Total[Mod[IntegerDigits@#, 2]] ==
1000000 &], _?(Total[Mod[IntegerDigits@#, 2]] ==
1 &)], "Number of primes < 1,000,000 with one odd digit", Top]</lang>
1 &)], "Number of primes < 1,000,000 with one odd digit", Top]</syntaxhighlight>


{{out}}<pre>
{{out}}<pre>
Line 639: Line 639:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import sequtils, strutils
<syntaxhighlight lang="nim">import sequtils, strutils


func isPrime(n: Positive): bool =
func isPrime(n: Positive): bool =
Line 678: Line 678:
for _ in primesOneOdd(1_000_000):
for _ in primesOneOdd(1_000_000):
inc count
inc count
echo "\nFound $# primes with only one odd digit below 1_000_000.".format(count)</lang>
echo "\nFound $# primes with only one odd digit below 1_000_000.".format(count)</syntaxhighlight>


{{out}}
{{out}}
Line 691: Line 691:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict;
use strict;
Line 700: Line 700:
my $million = grep tr/13579// == 1, @{ primes(1e6) };
my $million = grep tr/13579// == 1, @{ primes(1e6) };
print "found " . @singleodd .
print "found " . @singleodd .
"\n\n@singleodd\n\nfound $million in 1000000\n" =~ s/.{60}\K /\n/gr;</lang>
"\n\n@singleodd\n\nfound $million in 1000000\n" =~ s/.{60}\K /\n/gr;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 714: Line 714:
=={{header|Phix}}==
=={{header|Phix}}==
Relies on the fact that '0', '1', '2', etc are just as odd/even as 0, 1, 2, etc.
Relies on the fact that '0', '1', '2', etc are just as odd/even as 0, 1, 2, etc.
<!--<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;">function</span> <span style="color: #000000;">oneodddigit</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">odd</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">oneodddigit</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">odd</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1_000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">oneodddigit</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1_000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">oneodddigit</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d one odd digit primes &lt; 1,000: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d one odd digit primes &lt; 1,000: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 727: Line 727:
Fast skip from 11 direct to 20+, from 101 direct to 200+, etc. Around forty times faster than the above would be, but less than twice as fast as it would be without such skipping.<br>
Fast skip from 11 direct to 20+, from 101 direct to 200+, etc. Around forty times faster than the above would be, but less than twice as fast as it would be without such skipping.<br>
Of course the last digit must/will be odd for all primes (other than 2 which has no odd digit anyway), and ''all'' digits prior to that must be even, eg 223 or 241, and not 257 or 743.
Of course the last digit must/will be odd for all primes (other than 2 which has no odd digit anyway), and ''all'' digits prior to that must be even, eg 223 or 241, and not 257 or 743.
<!--<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;">for</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">8</span><span style="color: #0000FF;">:</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">8</span><span style="color: #0000FF;">:</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
Line 752: Line 752:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %,d one odd digit primes &lt; %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m10</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %,d one odd digit primes &lt; %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m10</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 767: Line 767:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>put display ^1000 .grep: { ($_ % 2) && .is-prime && (.comb[^(*-1)].all %% 2) }
<syntaxhighlight lang="raku" line>put display ^1000 .grep: { ($_ % 2) && .is-prime && (.comb[^(*-1)].all %% 2) }
sub display ($list, :$cols = 10, :$fmt = '%6d', :$title = "{+$list} matching:\n" ) {
sub display ($list, :$cols = 10, :$fmt = '%6d', :$title = "{+$list} matching:\n" ) {
cache $list;
cache $list;
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>45 matching:
<pre>45 matching:
Line 782: Line 782:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX pgm finds & displays primes (base ten) that contain only one odd digit (< 1,000).*/
<syntaxhighlight lang="rexx">/*REXX pgm finds & displays primes (base ten) that contain only one odd digit (< 1,000).*/
parse arg hi cols . /*obtain optional argument from the CL.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
Line 826: Line 826:
end /*k*/ /* [↑] only process numbers ≤ √ J */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j /*bump # of Ps; assign next P; P square*/
#= #+1; @.#= j; sq.#= j*j /*bump # of Ps; assign next P; P square*/
end /*j*/; return</lang>
end /*j*/; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 847: Line 847:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"
see "working..." + nl
see "working..." + nl
Line 872: Line 872:
see "Found " + row + " prime numbers" + nl
see "Found " + row + " prime numbers" + nl
see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 891: Line 891:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func primes_with_one_odd_digit(upto, base = 10) {
<syntaxhighlight lang="ruby">func primes_with_one_odd_digit(upto, base = 10) {


upto = prev_prime(upto+1)
upto = prev_prime(upto+1)
Line 928: Line 928:
var count = primes_with_one_odd_digit(10**k).len
var count = primes_with_one_odd_digit(10**k).len
say "There are #{'%6s' % count.commify} such primes <= 10^#{k}"
say "There are #{'%6s' % count.commify} such primes <= 10^#{k}"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 952: Line 952:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
<lang ecmascript>import "./math" for Int
<syntaxhighlight lang="ecmascript">import "./math" for Int
import "./fmt" for Fmt
import "./fmt" for Fmt
import "./seq" for Lst
import "./seq" for Lst
Line 978: Line 978:
}
}
}
}
Fmt.print("There are $,7d such primes under $,d", count, pow)</lang>
Fmt.print("There are $,7d such primes under $,d", count, pow)</syntaxhighlight>


{{out}}
{{out}}
Line 1,003: Line 1,003:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if N is a prime number
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
int N, I;
[if N <= 1 then return false;
[if N <= 1 then return false;
Line 1,027: Line 1,027:
Text(0, " such numbers found.
Text(0, " such numbers found.
");
");
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}