Smarandache prime-digital sequence: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(Tidied and expanded task description a little plus added a Go solution.)
Line 1: Line 1:
{{draft task|Calmo primes}}
{{draft task|Calmo primes}}


Prime p is Calmo prime if each its digit is also prime.
A prime p is defined to be a Calmo prime if each of its digits is also prime.


For example 257 is a Calmo prime because it is prime itself and its digits: 2, 5 and 7 are also prime.
;Task:


(Note: as there doesn't appear to be a generally accepted term for such primes, the author has named them after himself).
Show first 25 Calmo primes.


;Task
Show hundrest Calmo prime.

* Show the first 25 Calmo primes.

* Show the hundredth Calmo prime.

=={{header|Go}}==
As this task doesn't involve large numbers, a simple prime test routine is adequate.
<lang go>package main

import "fmt"

func isPrime(n int) bool {
if n < 2 {
return false
}
if n%2 == 0 {
return n == 2
}
if n%3 == 0 {
return n == 3
}
d := 5
for d*d <= n {
if n%d == 0 {
return false
}
d += 2
if n%d == 0 {
return false
}
d += 4
}
return true
}

func isCalmoPrime(n int) bool {
if !isPrime(n) {
return false
}
for n > 0 {
r := n % 10
if r != 2 && r != 3 && r != 5 && r != 7 {
return false
}
n /= 10
}
return true
}

func listCalmoPrimes(startFrom, countFrom, countTo int, printOne bool) int {
count := countFrom
for n := startFrom; ; n += 2 {
if isCalmoPrime(n) {
count++
if !printOne {
fmt.Printf("%2d. %d\n", count, n)
}
if count == countTo {
if printOne {
fmt.Printf("%2d. %d\n", count, n)
}
return n
}
}
}
}

func main() {
fmt.Println("The first 25 Calmo primes are:")
fmt.Println(" 1. 2")
n := listCalmoPrimes(3, 1, 25, false)
fmt.Println("\nThe hundredth Calmo prime is:")
listCalmoPrimes(n+2, 25, 100, true)
}</lang>

{{out}}
<pre>
The first 25 Calmo primes are:
1. 2
2. 3
3. 5
4. 7
5. 23
6. 37
7. 53
8. 73
9. 223
10. 227
11. 233
12. 257
13. 277
14. 337
15. 353
16. 373
17. 523
18. 557
19. 577
20. 727
21. 733
22. 757
23. 773
24. 2237
25. 2273

The hundredth Calmo prime is:
100. 33223
</pre>


=={{header|Ring}}==
=={{header|Ring}}==

Revision as of 10:13, 31 May 2019

Smarandache prime-digital sequence 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.

A prime p is defined to be a Calmo prime if each of its digits is also prime.

For example 257 is a Calmo prime because it is prime itself and its digits: 2, 5 and 7 are also prime.

(Note: as there doesn't appear to be a generally accepted term for such primes, the author has named them after himself).

Task
  • Show the first 25 Calmo primes.
  • Show the hundredth Calmo prime.

Go

As this task doesn't involve large numbers, a simple prime test routine is adequate. <lang go>package main

import "fmt"

func isPrime(n int) bool {

   if n < 2 {
       return false
   }
   if n%2 == 0 {
       return n == 2
   }
   if n%3 == 0 {
       return n == 3
   }
   d := 5
   for d*d <= n {
       if n%d == 0 {
           return false
       }
       d += 2
       if n%d == 0 {
           return false
       }
       d += 4
   }
   return true

}

func isCalmoPrime(n int) bool {

   if !isPrime(n) {
       return false
   }
   for n > 0 {
       r := n % 10
       if r != 2 && r != 3 && r != 5 && r != 7 {
           return false
       }
       n /= 10
   }
   return true

}

func listCalmoPrimes(startFrom, countFrom, countTo int, printOne bool) int {

   count := countFrom
   for n := startFrom; ; n += 2 {
       if isCalmoPrime(n) {
           count++
           if !printOne {
               fmt.Printf("%2d. %d\n", count, n)
           }
           if count == countTo {
               if printOne {
                   fmt.Printf("%2d. %d\n", count, n)
               }
               return n
           }
       }
   }

}

func main() {

   fmt.Println("The first 25 Calmo primes are:")
   fmt.Println(" 1. 2")
   n := listCalmoPrimes(3, 1, 25, false)
   fmt.Println("\nThe hundredth Calmo prime is:")
   listCalmoPrimes(n+2, 25, 100, true)

}</lang>

Output:
The first 25 Calmo primes are:
 1. 2
 2. 3
 3. 5
 4. 7
 5. 23
 6. 37
 7. 53
 8. 73
 9. 223
10. 227
11. 233
12. 257
13. 277
14. 337
15. 353
16. 373
17. 523
18. 557
19. 577
20. 727
21. 733
22. 757
23. 773
24. 2237
25. 2273

The hundredth Calmo prime is:
100. 33223

Ring

<lang ring>

  1. Project: Calmo primes

load "stdlib.ring" limit = 25 max = 300000 num = 0 see "working..." + nl see "wait for done..." + nl see "First 25 Calmo primes are:" + nl for n = 1 to max

   if isprime(n)
      res = calmo(n)
      if res = 1
         num = num + 1
         if num < limit + 1
            see "" + num + ". " + n + nl
         ok
         if num = 100
            see "The hundrest Calmo prime is:" + nl
            see "" + num + ". " + n + nl
            exit
         ok
      ok
   ok

next see "done..." + nl

func calmo(p)

    sp = string(p)
    for n = 1 to len(sp)
        if not isprime(sp[n])
           return 0
        ok
    next
    return 1

</lang>

Output:
working...
wait for done...
First 25 Calmo primes are:
1. 2
2. 3
3. 5
4. 7
5. 23
6. 37
7. 53
8. 73
9. 223
10. 227
11. 233
12. 257
13. 277
14. 337
15. 353
16. 373
17. 523
18. 557
19. 577
20. 727
21. 733
22. 757
23. 773
24. 2237
25. 2273
The hundrest Calmo prime is:
100. 33223
done...