Primes which contain only one odd digit

Revision as of 10:14, 28 July 2021 by PureFox (talk | contribs) (Attempted to clarify task description.)

Show on this page those primes under 1,000 which when expressed in decimal contain only one odd digit.

Primes which contain only one odd digit is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task


Go

Translation of: Wren
Library: Go-rcu

<lang go>package main

import (

   "fmt"
   "rcu"

)

func main() {

   const (
       LIMIT      = 999
       MAX_DIGITS = 3
   )
   primes := rcu.Primes(LIMIT)
   var results []int
   for _, prime := range primes[1:] {
       digits := rcu.Digits(prime, 10)
       digits = digits[:len(digits)-1]
       allEven := true
       for _, d := range digits {
           if d&1 == 1 {
               allEven = false
               break
           }
       }
       if allEven {
           results = append(results, prime)
       }
   }
   fmt.Println("Primes under", rcu.Commatize(LIMIT+1), "which contain only one odd digit:")
   for i, p := range results {
       fmt.Printf("%*s ", MAX_DIGITS, rcu.Commatize(p))
       if (i+1)%9 == 0 {
           fmt.Println()
       }
   }
   fmt.Println("\nFound", len(results), "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.

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

Library: Wren-math
Library: Wren-fmt
Library: 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>

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.