Primes which contain only one odd digit: Difference between revisions

m
(add RPL)
m (→‎{{header|Wren}}: Minor tidy)
 
(6 intermediate revisions by 3 users not shown)
Line 2:
 
;Task
Show on this page those primes under   '''1,000'''   which when expressed in decimal containscontain only one odd digit.
 
 
;Stretch goal:
Show on this page only the &nbsp; <u>count</u> &nbsp; of those primes under &nbsp; '''1,000,000''' &nbsp; which when expressed in decimal containscontain only one odd digit.
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F is_prime(n)
I n == 2
Line 389 ⟶ 388:
Elapsed Time: 171.630 ms.
</pre>
 
 
=={{header|F_Sharp|F#}}==
Line 452 ⟶ 450:
dim as uinteger m = int(log(n-1)/log(5))
return int((n-1-5^m)/5^j)
end function
 
function evendig(n as uinteger) as uinteger
'produces the integers with only even digits
Line 601 ⟶ 599:
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</syntaxhighlight>
{{Outout}}
<pre>Below 1000:
3 5 7 23 29 41 43 47 61 67
Line 611 ⟶ 609:
Count of matches below 10E6:
2560</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j"> getOneOdds=. #~ 1 = (2 +/@:| "."0@":)"0
primesTo=. i.&.(p:inv)
 
_15 ]\ getOneOdds primesTo 1000
3 5 7 23 29 41 43 47 61 67 83 89 223 227 229
241 263 269 281 283 401 409 421 443 449 461 463 467 487 601
607 641 643 647 661 683 809 821 823 827 829 863 881 883 887
 
# getOneOdds primesTo 1e6
2560</syntaxhighlight>
 
=={{header|jq}}==
Line 616 ⟶ 626:
'''Works with gojq, the Go implementation of jq'''
 
As noted in the [[#Julia|Julia entry]], if only one digit of a prime is odd, then that digit is in the ones place. The first solution presented here uses this observation to generate plausible candidates. The second solution is more brutish and slower but simpler.
 
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
Line 628 ⟶ 638:
label $out | stream | if cond then break $out else . end;
 
# Output: an unbounded stream
def primes_with_exactly_one_odd_digit:
# Output: a stream of candidate strings, in ascending numerical order
Line 744 ⟶ 754:
There are 2560 primes with only one odd digit in base 10 between 1 and 1,000,000.
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Labeled[Cases[
NestWhileList[NextPrime,
Line 890 ⟶ 901:
Found 46,708 one odd digit primes < 100,000,000
Found 202,635 one odd digit primes < 1,000,000,000
</pre>
 
=={{header|Quackery}}==
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<code>evendigits</code> returns the nth number which has only even digits and ends with zero. There are self-evidently 5^5 of them less than 1000000.
 
We know that the only prime ending with a 5 is 5. So we can omit generating candidate numbers that end with a 5, and stuff the 5 into the right place in the nest (i.e. as item #1; item #0 will be 3) afterwards. This explains the lines <code>' [ 1 3 7 9 ] witheach</code> and <code>5 swap 1 stuff</code>.
 
<syntaxhighlight lang="ecmascriptQuackery">import "./math" for[ [] Intswap
[ 5 /mod
rot join swap
dup 0 = until ]
drop
0 swap witheach
[ swap 10 * + ]
20 * ] is evendigits ( n --> n )
 
[]
5 5 ** times
[ i^ evendigits
' [ 1 3 7 9 ] witheach
[ over +
dup isprime iff
[ swap dip join ]
else drop ]
drop ]
5 swap 1 stuff
dup say "Qualifying primes < 1000:"
dup findwith [ 999 > ] [ ]
split drop unbuild
1 split nip -1 split drop
nest$ 44 wrap$ cr cr
say "Number of qualifying primes < 1000000: "
size echo</syntaxhighlight>
 
{{out}}
 
<pre>Qualifying primes < 1000:
3 5 7 23 29 41 43 47 61 67 83 89 223 227 229
241 263 269 281 283 401 409 421 443 449 461
463 467 487 601 607 641 643 647 661 683 809
821 823 827 829 863 881 883 887
 
Number of qualifying primes < 1000000: 2560
</pre>
 
=={{header|Raku}}==
<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" ) {
cache $list;
Line 982 ⟶ 1,038:
odd = 0
str = string(n)
for m = 1 to len(str)
if number(str[m])%2 = 1
odd++
Line 1,036 ⟶ 1,092:
<pre>
1: {3 5 7 23 29 41 43 47 61 67 83 89 223 227 229 241 263 269 281 283 401 409 421 443 449 461 463 467 487 601 607 641 643 647 661 683 809 821 823 827 829 863 881 883 887}
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
def single_odd?(pr)
d = pr.digits
d.first.odd? && d[1..].all?(&:even?)
end
 
res = Prime.each(1000).select {|pr| single_odd?(pr)}
res.each_slice(10){|s| puts "%4d"*s.size % s}
 
n = 1_000_000
count = Prime.each(n).count{|pr| single_odd?(pr)}
puts "\nFound #{count} single-odd-digit primes upto #{n}."
</syntaxhighlight>
{{out}}
<pre> 3 5 7 23 29 41 43 47 61 67
83 89 223 227 229 241 263 269 281 283
401 409 421 443 449 461 463 467 487 601
607 641 643 647 661 683 809 821 823 827
829 863 881 883 887
 
Found 2560 single-odd-digit primes upto 1000000.
</pre>
 
Line 1,099 ⟶ 1,180:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Int
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascript">import "./math" for Int
import "./fmt" for Fmt
 
import "./seq" for Lst
var limit = 999
var maxDigits = 3
Line 1,112 ⟶ 1,191:
}
Fmt.print("Primes under $,d which contain only one odd digit:", limit + 1)
for (chunk in Lst.chunks(results, 9)) Fmt.printtprint("$,%(maxDigits)d", chunkresults, 9)
System.print("\nFound %(results.count) such primes.\n")
 
limit = 1e9 - 1
primes = Int.primeSieve(limit)
9,476

edits