Erdős-primes: Difference between revisions

From Rosetta Code
Content added Content deleted
(deleted entries, are all now on the additive numbers page)
Line 12: Line 12:


;Also see:
;Also see:
:*   the OEIS entry:   [https://oeis.org/A046704 A046704 additive primes].
:*   the OEIS entry:   [http://oeis.org/A064152 A064152 Erdos primes].
:*   the OEIS entry:   [http://oeis.org/A064152 A064152 Erdos primes].
:*   the prime-numbers entry:   [https://prime-numbers.info/list/first-100-additive-primes additive primes].
:*   the geeks for geeks entry: [https://www.geeksforgeeks.org/additive-prime-number/ additive prime number].
:*   the prime-numbers fandom: [https://prime-numbers.fandom.com/wiki/Additive_Primes additive primes].
<br><br>
<br><br>

=={{header|APL}}==

<lang APL>((+⌿(4/10)⊤P)∊P)/P←(~P∊P∘.×P)/P←1↓⍳500</lang>

{{out}}

<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283
311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487</pre>

=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<lang factor>USING: formatting grouping io kernel math math.primes
prettyprint sequences ;

: sum-digits ( n -- sum )
0 swap [ 10 /mod rot + swap ] until-zero ;

499 primes-upto [ sum-digits prime? ] filter
[ 9 group simple-table. nl ]
[ length "Found %d Erdős primes < 500.\n" printf ] bi</lang>
{{out}}
<pre>
2 3 5 7 11 23 29 41 43
47 61 67 83 89 101 113 131 137
139 151 157 173 179 191 193 197 199
223 227 229 241 263 269 281 283 311
313 317 331 337 353 359 373 379 397
401 409 421 443 449 461 463 467 487

Found 54 Erdős primes < 500.
</pre>

=={{header|Go}}==
<lang go>package main

import "fmt"

func isPrime(n int) bool {
switch {
case n < 2:
return false
case n%2 == 0:
return n == 2
case n%3 == 0:
return n == 3
default:
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 sumDigits(n int) int {
sum := 0
for n > 0 {
sum += n % 10
n /= 10
}
return sum
}

func main() {
fmt.Println("Additive primes less than 500:")
i := 2
count := 0
for {
if isPrime(i) && isPrime(sumDigits(i)) {
count++
fmt.Printf("%3d ", i)
if count%10 == 0 {
fmt.Println()
}
}
if i > 2 {
i += 2
} else {
i++
}
if i > 499 {
break
}
}
fmt.Printf("\n\n%d additive primes found.\n", count)
}</lang>

{{out}}
<pre>
Additive primes less than 500:
2 3 5 7 11 23 29 41 43 47
61 67 83 89 101 113 131 137 139 151
157 173 179 191 193 197 199 223 227 229
241 263 269 281 283 311 313 317 331 337
353 359 373 379 397 401 409 421 443 449
461 463 467 487
54 additive primes found.
</pre>

=={{header|Julia}}==
<lang julia>using Primes

let
p = primesmask(500)
println("Erdős primes under 500:")
pcount = 0
for i in 2:499
if p[i] && p[sum(digits(i))]
pcount += 1
print(lpad(i, 4), pcount % 20 == 0 ? "\n" : "")
end
end
println("\n\n$pcount Erdős primes found.")
end
</lang>{{out}}
<pre>
Erdős primes under 500:
2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151
157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337
353 359 373 379 397 401 409 421 443 449 461 463 467 487

54 Erdős primes found.
</pre>

=={{header|Phix}}==
<lang Phix>function erdos(integer p) return is_prime(sum(sq_sub(sprint(p),'0'))) end function
sequence res = filter(get_primes_le(500),erdos)
string r = join(shorten(apply(res,sprint),"",6))
printf(1,"%d additive primes found: %s\n",{length(res),r})</lang>
{{out}}
<pre>
54 additive primes found: 2 3 5 7 11 23 ... 443 449 461 463 467 487
</pre>

=={{header|REXX}}==
<lang rexx>/*REXX program counts/displays the number of additive primes under a specified number N.*/
parse arg n cols . /*get optional number of primes to find*/
if n=='' | n=="," then n= 500 /*Not specified? Then assume default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " */
Ocols= cols; cols= abs(cols) /*Use the absolute value of cols. */
call genP n /*generate all primes under N. */
primes= 0 /*initialize number of additive primes.*/
$= /*a list of additive primes (so far). */
do j=2 until j>=n; if \!.j then iterate /*Is J not a prime? No, then skip it.*/
_= sumDigs(j); if \!._ then iterate /*is sum of J's digs a prime? No, skip.*/
primes= primes + 1 /*bump the count of additive primes. */
if Ocols<1 then iterate /*Build the list (to be shown later)? */
$= $ right(j, w) /*add the additive prime to the $ list.*/
if primes//cols\==0 then iterate /*have we populated a line of output? */
say substr($, 2); $= /*display what we have so far (cols). */
end /*j*/

if $\=='' then say substr($, 2) /*possible display some residual output*/
say
say 'found ' primes " additive primes < " n
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sumDigs: parse arg x 1 s 2; do k=2 for length(x)-1; s= s + substr(x,k,1); end; return s
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: parse arg n; @.=.; @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; @.7=17; #= 7
w= length(n); !.=0; !.2=1; !.3=1; !.5=1; !.7=1; !.11=1; !.13=1; !.17=1
do j=@.7+2 by 2 while j<n /*continue on with the next odd prime. */
parse var j '' -1 _ /*obtain the last digit of the J var.*/
if _ ==5 then iterate /*is this integer a multiple of five? */
if j // 3 ==0 then iterate /* " " " " " " three? */
/* [↓] divide by the primes. ___ */
do k=4 to # while k*k<=j /*divide J by other primes ≤ √ J */
if j//@.k == 0 then iterate j /*÷ by prev. prime? ¬prime ___ */
end /*k*/ /* [↑] only divide up to √ J */
#= # + 1; @.#= j; !.j= 1 /*bump prime count; assign prime & flag*/
end /*j*/
return</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
2 3 5 7 11 23 29 41 43 47
61 67 83 89 101 113 131 137 139 151
157 173 179 191 193 197 199 223 227 229
241 263 269 281 283 311 313 317 331 337
353 359 373 379 397 401 409 421 443 449
461 463 467 487


found 54 additive primes < 500
</pre>

=={{header|Ring}}==
<lang ring>
load "stdlib.ring"

see "working..." + nl
see "Erdős-primes are:" + nl

row = 0
limit = 500

for n = 1 to limit
num = 0
if isprime(n)
strn = string(n)
for m = 1 to len(strn)
num = num + number(strn[m])
next
if isprime(num)
row = row + 1
see "" + n + " "
if row%10 = 0
see nl
ok
ok
ok
next

see nl + "found " + row + " Erdös-primes." + nl
see "done..." + nl
</lang>
{{out}}
<pre>
working...
Erdős-primes are:
2 3 5 7 11 23 29 41 43 47
61 67 83 89 101 113 131 137 139 151
157 173 179 191 193 197 199 223 227 229
241 263 269 281 283 311 313 317 331 337
353 359 373 379 397 401 409 421 443 449
461 463 467 487
found 54 Erdös-primes.
done...
</pre>

=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Int
import "/fmt" for Fmt

var sumDigits = Fn.new { |n|
var sum = 0
while (n > 0) {
sum = sum + (n % 10)
n = (n/10).floor
}
return sum
}

System.print("Additive primes less than 500:")
var primes = Int.primeSieve(499)
var count = 0
for (p in primes) {
if (Int.isPrime(sumDigits.call(p))) {
count = count + 1
Fmt.write("$3d ", p)
if (count % 10 == 0) System.print()
}
}
System.print("\n\n%(count) additive primes found.")</lang>

{{out}}
<pre>
Additive primes less than 500:
2 3 5 7 11 23 29 41 43 47
61 67 83 89 101 113 131 137 139 151
157 173 179 191 193 197 199 223 227 229
241 263 269 281 283 311 313 317 331 337
353 359 373 379 397 401 409 421 443 449
461 463 467 487

54 additive primes found.
</pre>

Revision as of 19:48, 19 March 2021

Erdős-primes 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.
Definitions

In mathematics, Erdős-primes are prime numbers which sum of digits are also primes.


Task

Write a program to determine (and show here) all Erdős-primes whose elements are less than 500.

Optionally, show the number of Erdős-primes.


Also see