Primes which contain only one odd digit: Difference between revisions
Content added Content deleted
(→J: add) |
m (cleanup) |
||
Line 2: | Line 2: | ||
;Task |
;Task |
||
Show on this page those primes under '''1,000''' which when expressed in decimal |
Show on this page those primes under '''1,000''' which when expressed in decimal contain only one odd digit. |
||
;Stretch goal: |
;Stretch goal: |
||
Show on this page only the <u>count</u> of those primes under '''1,000,000''' which when expressed in decimal |
Show on this page only the <u>count</u> of those primes under '''1,000,000''' which when expressed in decimal contain only one odd digit. |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
=={{header|11l}}== |
||
{{trans|Nim}} |
{{trans|Nim}} |
||
<syntaxhighlight lang="11l">F is_prime(n) |
<syntaxhighlight lang="11l">F is_prime(n) |
||
I n == 2 |
I n == 2 |
||
Line 389: | Line 388: | ||
Elapsed Time: 171.630 ms. |
Elapsed Time: 171.630 ms. |
||
</pre> |
</pre> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
Line 452: | Line 450: | ||
dim as uinteger m = int(log(n-1)/log(5)) |
dim as uinteger m = int(log(n-1)/log(5)) |
||
return int((n-1-5^m)/5^j) |
return int((n-1-5^m)/5^j) |
||
end function |
end function |
||
function evendig(n as uinteger) as uinteger |
function evendig(n as uinteger) as uinteger |
||
'produces the integers with only even digits |
'produces the integers with only even digits |
||
Line 613: | Line 611: | ||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang="j"> getOneOdds=. #~ |
<syntaxhighlight lang="j"> getOneOdds=. #~ 1 = (2 +/@:| "."0@":)"0 |
||
primesTo=. i.&.(p:inv) |
primesTo=. i.&.(p:inv) |
||
Line 628: | Line 626: | ||
'''Works with gojq, the Go implementation of jq''' |
'''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. |
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`. |
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`. |
||
Line 640: | Line 638: | ||
label $out | stream | if cond then break $out else . end; |
label $out | stream | if cond then break $out else . end; |
||
# Output: an unbounded stream |
# Output: an unbounded stream |
||
def primes_with_exactly_one_odd_digit: |
def primes_with_exactly_one_odd_digit: |
||
# Output: a stream of candidate strings, in ascending numerical order |
# Output: a stream of candidate strings, in ascending numerical order |
||
Line 756: | Line 754: | ||
There are 2560 primes with only one odd digit in base 10 between 1 and 1,000,000. |
There are 2560 primes with only one odd digit in base 10 between 1 and 1,000,000. |
||
</pre> |
</pre> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
<syntaxhighlight lang="mathematica">Labeled[Cases[ |
<syntaxhighlight lang="mathematica">Labeled[Cases[ |
||
NestWhileList[NextPrime, |
NestWhileList[NextPrime, |
||
Line 905: | Line 904: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
<code>isprime</code> is defined at [[Primality by trial division#Quackery]]. |
<code>isprime</code> is defined at [[Primality by trial division#Quackery]]. |
||
Line 952: | Line 950: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<syntaxhighlight lang="raku" line>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; |
||
Line 1,040: | Line 1,038: | ||
odd = 0 |
odd = 0 |
||
str = string(n) |
str = string(n) |
||
for m = 1 to len(str) |
for m = 1 to len(str) |
||
if number(str[m])%2 = 1 |
if number(str[m])%2 = 1 |
||
odd++ |
odd++ |
||
Line 1,120: | Line 1,118: | ||
Found 2560 single-odd-digit primes upto 1000000. |
Found 2560 single-odd-digit primes upto 1000000. |
||
</pre> |
</pre> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<syntaxhighlight lang="ruby">func primes_with_one_odd_digit(upto, base = 10) { |
<syntaxhighlight lang="ruby">func primes_with_one_odd_digit(upto, base = 10) { |
||
Line 1,185: | Line 1,184: | ||
import "./fmt" for Fmt |
import "./fmt" for Fmt |
||
import "./seq" for Lst |
import "./seq" for Lst |
||
var limit = 999 |
var limit = 999 |
||
var maxDigits = 3 |
var maxDigits = 3 |
||
Line 1,196: | Line 1,195: | ||
for (chunk in Lst.chunks(results, 9)) Fmt.print("$,%(maxDigits)d", chunk) |
for (chunk in Lst.chunks(results, 9)) Fmt.print("$,%(maxDigits)d", chunk) |
||
System.print("\nFound %(results.count) such primes.\n") |
System.print("\nFound %(results.count) such primes.\n") |
||
limit = 1e9 - 1 |
limit = 1e9 - 1 |
||
primes = Int.primeSieve(limit) |
primes = Int.primeSieve(limit) |