Sexy primes: Difference between revisions

Content added Content deleted
No edit summary
Line 28: Line 28:
::*Note that 1000033 '''SHOULD NOT''' be counted in the pair count. It is sexy, but not in a pair within the limit. However, it also '''SHOULD NOT''' be listed in the unsexy primes since it is sexy.
::*Note that 1000033 '''SHOULD NOT''' be counted in the pair count. It is sexy, but not in a pair within the limit. However, it also '''SHOULD NOT''' be listed in the unsexy primes since it is sexy.
<br><br>
<br><br>
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f SEXY_PRIMES.AWK
BEGIN {
cutoff = 1000034
for (i=1; i<=cutoff; i++) {
n1 = i
if (is_prime(n1)) {
total_primes++
if ((n2 = n1 + 6) > cutoff) { continue }
if (is_prime(n2)) {
save(2,5,n1 FS n2)
if ((n3 = n2 + 6) > cutoff) { continue }
if (is_prime(n3)) {
save(3,5,n1 FS n2 FS n3)
if ((n4 = n3 + 6) > cutoff) { continue }
if (is_prime(n4)) {
save(4,5,n1 FS n2 FS n3 FS n4)
if ((n5 = n4 + 6) > cutoff) { continue }
if (is_prime(n5)) {
save(5,5,n1 FS n2 FS n3 FS n4 FS n5)
}
}
}
}
if ((s[2] s[3] s[4] s[5]) !~ (n1 "")) { # check for unsexy
save(1,10,n1)
}
}
}
printf("%d primes less than %s\n\n",total_primes,cutoff+1)
printf("%d unsexy primes\n%s\n\n",c[1],s[1])
printf("%d sexy prime pairs\n%s\n\n",c[2],s[2])
printf("%d sexy prime triplets\n%s\n\n",c[3],s[3])
printf("%d sexy prime quadruplets\n%s\n\n",c[4],s[4])
printf("%d sexy prime quintuplets\n%s\n\n",c[5],s[5])
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
function save(key,nbr_to_keep,str) {
c[key]++
str = s[key] str ", "
if (gsub(/,/,"&",str) > nbr_to_keep) {
str = substr(str,index(str,",")+2)
}
s[key] = str
}
</lang>
{{out}}
<pre>
78500 primes less than 1000035


48627 unsexy primes
999853, 999863, 999883, 999907, 999917, 999931, 999961, 999979, 999983, 1000003,

16386 sexy prime pairs
999371 999377, 999431 999437, 999721 999727, 999763 999769, 999953 999959,

2900 sexy prime triplets
997427 997433 997439, 997541 997547 997553, 998071 998077 998083, 998617 998623 998629, 998737 998743 998749,

325 sexy prime quadruplets
977351 977357 977363 977369, 983771 983777 983783 983789, 986131 986137 986143 986149, 990371 990377 990383 990389, 997091 997097 997103 997109,

1 sexy prime quintuplets
5 11 17 23 29,
</pre>
=={{header|C}}==
=={{header|C}}==
Similar approach to the Go entry but only stores the arrays that need to be printed out.
Similar approach to the Go entry but only stores the arrays that need to be printed out.
Line 225: Line 301:
[[999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003]
[[999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003]
</pre>
</pre>
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f SEXY_PRIMES.AWK
BEGIN {
cutoff = 1000034
for (i=1; i<=cutoff; i++) {
n1 = i
if (is_prime(n1)) {
total_primes++
if ((n2 = n1 + 6) > cutoff) { continue }
if (is_prime(n2)) {
save(2,5,n1 FS n2)
if ((n3 = n2 + 6) > cutoff) { continue }
if (is_prime(n3)) {
save(3,5,n1 FS n2 FS n3)
if ((n4 = n3 + 6) > cutoff) { continue }
if (is_prime(n4)) {
save(4,5,n1 FS n2 FS n3 FS n4)
if ((n5 = n4 + 6) > cutoff) { continue }
if (is_prime(n5)) {
save(5,5,n1 FS n2 FS n3 FS n4 FS n5)
}
}
}
}
if ((s[2] s[3] s[4] s[5]) !~ (n1 "")) { # check for unsexy
save(1,10,n1)
}
}
}
printf("%d primes less than %s\n\n",total_primes,cutoff+1)
printf("%d unsexy primes\n%s\n\n",c[1],s[1])
printf("%d sexy prime pairs\n%s\n\n",c[2],s[2])
printf("%d sexy prime triplets\n%s\n\n",c[3],s[3])
printf("%d sexy prime quadruplets\n%s\n\n",c[4],s[4])
printf("%d sexy prime quintuplets\n%s\n\n",c[5],s[5])
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
function save(key,nbr_to_keep,str) {
c[key]++
str = s[key] str ", "
if (gsub(/,/,"&",str) > nbr_to_keep) {
str = substr(str,index(str,",")+2)
}
s[key] = str
}
</lang>
{{out}}
<pre>
78500 primes less than 1000035

48627 unsexy primes
999853, 999863, 999883, 999907, 999917, 999931, 999961, 999979, 999983, 1000003,

16386 sexy prime pairs
999371 999377, 999431 999437, 999721 999727, 999763 999769, 999953 999959,

2900 sexy prime triplets
997427 997433 997439, 997541 997547 997553, 998071 998077 998083, 998617 998623 998629, 998737 998743 998749,

325 sexy prime quadruplets
977351 977357 977363 977369, 983771 983777 983783 983789, 986131 986137 986143 986149, 990371 990377 990383 990389, 997091 997097 997103 997109,

1 sexy prime quintuplets
5 11 17 23 29,
</pre>

=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]