Successive prime differences: Difference between revisions

m
(added RPL)
m (→‎{{header|Wren}}: Minor tidy)
 
(3 intermediate revisions by 2 users not shown)
Line 202:
Found 1 prime sequence(s) that differ by: [ 32 16 8 4 2 ]
first: [ 148091 148123 148139 148147 148151 148153 ] last: [ 148091 148123 148139 148147 148151 148153 ]
 
</pre>
 
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
 
#proto buscarprimos(_X_,_Y_)
 
#include <basico.h>
 
algoritmo
 
pila de trabajo 50
decimales '0'
números( dif 1, dif 2, dif 22, dif 24, dif 42, dif 642)
cadenas( inicio1, inicio2, inicio22, inicio24, inicio42, inicio642,\
final1, final2, final22, final24, final42, final642 )
sw1=1, sw2=1, sw22=1, sw24=1, sw42=1, sw642=1
i=2
iterar
i, es primo?, entonces {
i2 = i, i4=i, i6=i
++i2; i2, es primo?, entonces{
++dif 1
sw1, entonces{
#( string(i)),#(string(i2)), unir en 'inicio1'
sw1=0
}
#( string(i)),#(string(i2)), unir en 'final1'
}
++i2; i2, es primo?, entonces{
++dif 2
sw2, entonces{
#( string(i)),#(string(i2)), unir en 'inicio2'
sw2=0
}
#( string(i)),#(string(i2)), unir en 'final2'
 
i2+=2; i2, es primo?, entonces{
++dif 22
sw22, entonces{
#( string(i)),#(string(i2-2)),#(string(i2))
unir en 'inicio22'
sw22=0
}
#( string(i)),#(string(i2-2)),#(string(i2))
unir en 'final22'
}
i2+=2; i2, es primo?, entonces{
++dif 24
sw24, entonces{
#( string(i)),#(string(i2-4)),#(string(i2))
unir en 'inicio24'
sw24=0
}
#( string(i)),#(string(i2-4)),#(string(i2))
unir en 'final24'
}
}
i4+=4, i4, es primo?, entonces{
i4+=2, i4, es primo?, entonces{
++dif 42
sw42, entonces{
#( string(i)),#(string(i4-2)),#(string(i4))
unir en 'inicio42'
sw42=0
}
#( string(i)),#(string(i4-2)),#(string(i4))
unir en 'final42'
}
}
/* aquí, debido al espaciamiento, pueden haber primos entre 'i'
e 'i+12', y debo chequear eso */
i6+=6, i6, es primo?, entonces{
i6+=4, i6, es primo?, entonces{
i6+=2, i6, es primo?, entonces{
si '#(buscar primos( (i+1),(i6-6) ) && buscar primos( (i6-5),i6-2))'
sw642, entonces{
#( string(i)),#(string(i6-6)),#(string(i6-2)),#(string(i6))
unir en 'inicio642'
sw642=0
}
++dif 642
fin si
 
#( string(i)),#(string(i6-6)),#(string(i6-2)),#(string(i6))
unir en 'final642'
}
}
}
}
++i
hasta que '#(i == 1000000)'
imprimir( "Diff Sequence\tCount\t\tFirst\tLast\n")
imprimir( "[ 1 ] \t", dif 1, "\t", #(lpad(" ",13,inicio1))," ",final1,\
"\n[ 2 ] \t", dif 2, "\t", #(lpad(" ",13,inicio2))," ",final2,\
"\n[ 2-2 ] \t", dif22, "\t", #(lpad(" ",13,inicio22))," ",final22,\
"\n[ 2-4 ] \t", dif 24,"\t", #(lpad(" ",13,inicio24))," ",final24,\
"\n[ 4-2 ] \t", dif 42,"\t", #(lpad(" ",13,inicio42))," ",final42,\
"\n[ 6-4-2 ]\t", dif 642,"\t", #(lpad(" ",13,inicio642))," ",final642,"\n")
 
terminar
 
subrutinas
 
buscar primos(x,y)
sw=1
iterar para( i=x, #(sw && i<y), ++i )
i, es primo?, entonces{
sw=0
}
siguiente
retornar 'sw'
</syntaxhighlight>
{{out}}
<pre>
Diff Sequence Count First Last
[ 1 ] 1 2,3 2,3
[ 2 ] 8169 3,5 999959,999961
[ 2-2 ] 1 3,5,7 3,5,7
[ 2-4 ] 1393 5,7,11 999431,999433,999437
[ 4-2 ] 1444 7,11,13 997807,997811,997813
[ 6-4-2 ] 306 31,37,41,43 997141,997147,997151,997153
 
</pre>
Line 1,018 ⟶ 1,146:
(6, 4, 2): first group = (31,37,41,43), last group = (997141,997147,997151,997153), count = 306
</pre>
 
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func nextprime n .
n += 1
while isprim n = 0
n += 1
.
return n
.
func spd n d[] .
if isprim n = 0
return 0
.
for i = 1 to len d[]
if nextprime n <> n + d[i]
return 0
.
n += d[i]
.
return 1
.
proc print_set n d[] . .
write "( " & n & " "
for i = 1 to len d[]
write n + d[i] & " "
n += d[i]
.
print ")"
.
proc show max d[] . .
write "Differences of "
for d in d[]
write d & " "
.
print ""
for n = 2 to max - d[len d[]]
if spd n d[] = 1
c += 1
if c = 1
print_set n d[]
.
last = n
.
.
print_set last d[]
print "Number of occurrences: " & c
print ""
.
show 1000000 [ 2 ]
show 1000000 [ 1 ]
show 1000000 [ 2 2 ]
show 1000000 [ 2 4 ]
show 1000000 [ 4 2 ]
show 1000000 [ 6 4 2 ]
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 2,807 ⟶ 3,002:
{{trans|Go}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var successivePrimes = Fn.new { |primes, diffs|
9,477

edits