Primes which contain only one odd digit: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|Go}}: Stretched to primes under 10 billion.)
m (syntax highlighting fixup automation)
Line 12:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F is_prime(n)
I n == 2
R 1B
Line 39:
I hasLastDigitOdd(n) & is_prime(n)
count++
print("\nFound "count‘ primes with only one odd digit below 1000000.’)</langsyntaxhighlight>
 
{{out}}
Line 55:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE FUNC OddDigitsCount(INT x)
Line 86:
OD
PrintF("%E%EThere are %I primes",count)
RETURN</langsyntaxhighlight>
{{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]
Line 98:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find primes whose decimal representation contains only one odd digit #
# sieve the primes to 1 000 000 #
PR read "primes.incl.a68" PR
Line 132:
OD;
show total( p1odd count, UPB prime, "single-odd-digit" )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 146:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PRIMES_WHICH_CONTAIN_ONLY_ONE_ODD_NUMBER.AWK
BEGIN {
Line 176:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 199:
=={{header|C#|CSharp}}==
Modifies a conventional prime sieve to cull the items with more than one odd digit.
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 246:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>List of one-odd-digit primes < 1,000:
Line 267:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// 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)
primes32()|>Seq.takeWhile((>)1000)|>Seq.filter(fun g->fN g 0)|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 280:
{{libheader|Factor-numspec}}
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: grouping io lists lists.lazy literals math math.primes
numspec prettyprint ;
 
Line 301:
 
"\nCount of such primes under 1,000,000,000:" print
p [ 1,000,000,000 < ] lwhile llength .</langsyntaxhighlight>
{{out}}
<pre>
Line 319:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
#include "isprime.bas"
 
Line 349:
wend
 
print "There are ";count;" such primes below one million."</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 409:
}
fmt.Printf("There are %7s such primes under %s\n", rcu.Commatize(count), rcu.Commatize(pow))
}</langsyntaxhighlight>
 
{{out}}
Line 435:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (intercalate, maximum, transpose)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (primes)
Line 474:
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</langsyntaxhighlight>
{{Out}}
<pre>Below 1000:
Line 495:
 
===Fast solution===
<langsyntaxhighlight lang="jq">### Preliminaries
 
def count(s): reduce s as $x (null; .+1);
Line 527:
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)))."</langsyntaxhighlight>
{{out}}
<pre>
Line 579:
</pre>
===A simpler but slower solution===
<langsyntaxhighlight lang="jq"># Input is assumed to be prime.
# So we only need check the other digits are all even.
def prime_has_exactly_one_odd_digit:
Line 590:
# It is much faster to check for primality afterwards.
range(3; infinite; 2)
| select(prime_has_exactly_one_odd_digit and is_prime);</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="julia">using Primes
 
function isoneoddprime(n, base = 10)
Line 607:
println("\nThere are ", count(isoneoddprime, primes(1_000_000)),
" primes with only one odd digit in base 10 between 1 and 1,000,000.")
</langsyntaxhighlight>{{out}}
<pre>
Found 45 primes with one odd digit in base 10:
Line 619:
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Labeled[Cases[
NestWhileList[NextPrime,
2, # <
Line 628:
2, # <
1000000 &], _?(Total[Mod[IntegerDigits@#, 2]] ==
1 &)], "Number of primes < 1,000,000 with one odd digit", Top]</langsyntaxhighlight>
 
{{out}}<pre>
Line 639:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import sequtils, strutils
 
func isPrime(n: Positive): bool =
Line 678:
for _ in primesOneOdd(1_000_000):
inc count
echo "\nFound $# primes with only one odd digit below 1_000_000.".format(count)</langsyntaxhighlight>
 
{{out}}
Line 691:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 700:
my $million = grep tr/13579// == 1, @{ primes(1e6) };
print "found " . @singleodd .
"\n\n@singleodd\n\nfound $million in 1000000\n" =~ s/.{60}\K /\n/gr;</langsyntaxhighlight>
{{out}}
<pre>
Line 714:
=={{header|Phix}}==
Relies on the fact that '0', '1', '2', etc are just as odd/even as 0, 1, 2, etc.
<!--<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;">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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
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>
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.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 767:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>put display ^1000 .grep: { ($_ % 2) && .is-prime && (.comb[^(*-1)].all %% 2) }
sub display ($list, :$cols = 10, :$fmt = '%6d', :$title = "{+$list} matching:\n" ) {
cache $list;
$title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}</langsyntaxhighlight>
{{out}}
<pre>45 matching:
Line 782:
 
=={{header|REXX}}==
<langsyntaxhighlight 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.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
Line 826:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j /*bump # of Ps; assign next P; P square*/
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 847:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 872:
see "Found " + row + " prime numbers" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 891:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func primes_with_one_odd_digit(upto, base = 10) {
 
upto = prev_prime(upto+1)
Line 928:
var count = primes_with_one_odd_digit(10**k).len
say "There are #{'%6s' % count.commify} such primes <= 10^#{k}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 952:
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<langsyntaxhighlight lang="ecmascript">import "./math" for Int
import "./fmt" for Fmt
import "./seq" for Lst
Line 978:
}
}
Fmt.print("There are $,7d such primes under $,d", count, pow)</langsyntaxhighlight>
 
{{out}}
Line 1,003:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 1,027:
Text(0, " such numbers found.
");
]</langsyntaxhighlight>
 
{{out}}
10,327

edits