Primes which contain only one odd digit: Difference between revisions
m (→{{header|Julia}}: bases) |
(Added Wren) |
||
Line 70: | Line 70: | ||
Found 45 prime numbers |
Found 45 prime numbers |
||
done... |
done... |
||
</pre> |
|||
=={{header|Wren}}== |
|||
{{libheader|Wren-math}} |
|||
{{libheader|Wren-fmt}} |
|||
{{libheader|Wren-seq}} |
|||
<lang ecmascript>import "/math" for Int |
|||
import "/fmt" for Fmt |
|||
import "/seq" for Lst |
|||
var limit = 999 |
|||
var maxDigits = 3 |
|||
var primes = Int.primeSieve(limit) |
|||
var results = [] |
|||
for (prime in primes.skip(1)) { |
|||
if (Int.digits(prime)[0...-1].all { |d| d & 1 == 0 }) results.add(prime) |
|||
} |
|||
Fmt.print("Primes under $,d which contain only one odd digit:", limit + 1) |
|||
for (chunk in Lst.chunks(results, 9)) Fmt.print("$,%(maxDigits)d", chunk) |
|||
System.print("\nFound %(results.count) such primes.")</lang> |
|||
{{out}} |
|||
<pre> |
|||
Primes under 1,000 which contain only one odd digit: |
|||
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 45 such primes. |
|||
</pre> |
</pre> |
Revision as of 09:53, 28 July 2021
- Task
- Primes which contain only one odd number, where n < 1,000
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. <lang julia>using Primes
function isoneoddprime(n, base = 10)
d = digits(n ÷ base, base = base) return n != 2 && all(iseven, d)
end
found = filter(isoneoddprime, primes(1000)) println("Found $(length(found)) primes with one odd digit in base 10:") foreach(p -> print(rpad(last(p), 5), first(p) % 9 == 0 ? "\n" : ""), enumerate(found))
</lang>
- Output:
Found 45 primes with one odd digit in base 10: 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
Ring
<lang ring> load "stdlib.ring" see "working..." + nl see "Primes which contain only one odd number:" + nl row = 0
for n = 1 to 1000
odd = 0 str = string(n) for m = 1 to len(str) if number(str[m])%2 = 1 odd++ ok next if odd = 1 and isprime(n) see "" + n + " " row++ if row%5 = 0 see nl ok ok
next
see "Found " + row + " prime numbers" + nl see "done..." + nl </lang>
- Output:
working... Primes which contain only one odd number: 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 45 prime numbers done...
Wren
<lang ecmascript>import "/math" for Int import "/fmt" for Fmt import "/seq" for Lst
var limit = 999 var maxDigits = 3 var primes = Int.primeSieve(limit) var results = [] for (prime in primes.skip(1)) {
if (Int.digits(prime)[0...-1].all { |d| d & 1 == 0 }) results.add(prime)
} Fmt.print("Primes under $,d which contain only one odd digit:", limit + 1) for (chunk in Lst.chunks(results, 9)) Fmt.print("$,%(maxDigits)d", chunk) System.print("\nFound %(results.count) such primes.")</lang>
- Output:
Primes under 1,000 which contain only one odd digit: 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 45 such primes.