The sieve of Sundaram: Difference between revisions
Content added Content deleted
GordonBGood (talk | contribs) (Add a comment to make the time complexity deficiency of the SoS clear...) |
No edit summary |
||
Line 143: | Line 143: | ||
The millionth Sundaram prime is 15485867 |
The millionth Sundaram prime is 15485867 |
||
</pre> |
|||
=={{header|Amazing Hopper}}== |
|||
{{trans|C}} |
|||
<syntaxhighlight lang="Amazing Hopper"> |
|||
#include <jambo.h> |
|||
Main |
|||
Set break |
|||
tiempo inicio = 0, tiempo final = 0 |
|||
Tic( tiempo inicio ) |
|||
nprimes=1000000, nmax=0 |
|||
Let ( nmax := Ceil( Mul( nprimes, Sub( Add(Log(nprimes), Log(Log(nprimes))), 0.9385) ) ) ) |
|||
k=0, Let( k := Div( Minus two 'nmax', 2) ) |
|||
a=0 |
|||
Set decimal '0' |
|||
Seqspaced(3, {k} Mul by '2' Plus '1', {k} Mul by '2' Plus '1' Div into '2', a) |
|||
Unset decimal |
|||
i=1 |
|||
pos inicial sumando=2, pos ini factor = 2, factor factor = 2, suma = 6 |
|||
sumando = 0, factor = 0 |
|||
end subloop=0 |
|||
Loop |
|||
/* calculo las secuencias para las posiciones; |
|||
si lo hago con ciclos, el loop termina dentro de 6 minutos :D */ |
|||
Let ( end subloop := {k} Minus 'i', {i} Mul by '2' Plus '1', Div it ) |
|||
Sequence( pos inicial sumando, 1, end subloop, sumando ) |
|||
Sequence( pos ini factor, factor factor, end subloop, factor ) |
|||
Let ( sumando := Add( sumando, factor) ) |
|||
Set range 'sumando', Set '0', Put 'a' // pongo ceros en las posiciones calculadas |
|||
Clr range |
|||
/* recalculo índices para nuevas posiciones */ |
|||
pos inicial sumando += 2 // 2,4,6,8... |
|||
pos ini factor += suma // 2, 8, 18, 32 |
|||
suma += 4 // 10, 14, 18 |
|||
factor factor += 2 // 2,4,6,8 |
|||
++i |
|||
While ( Less equal ( Mul( Mul( Plus one (i),i ),2), k ) ) |
|||
/* Visualización de los primeros 100 primos */ |
|||
Cls |
|||
ta=0, Compact 'a', Move to 'a' // elimino los ceros = compacto array |
|||
[1:100] Get 'a', Move to 'ta', Redim (ta, 10, 10) |
|||
Tok sep ("\t"), Print table 'ta' |
|||
Clr interval, Clear 'ta' |
|||
/* imprimo el primo número "nprimes" */ |
|||
Print( nprimes, " th Sundaram prime is ", [ nprimes ] Get 'a', "\n" ) |
|||
Toc( tiempo inicio, tiempo final ) |
|||
Printnl( "Time = ", tiempo final, " segs" ) |
|||
End |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
3 5 7 11 13 17 19 23 29 31 |
|||
37 41 43 47 53 59 61 67 71 73 |
|||
79 83 89 97 101 103 107 109 113 127 |
|||
131 137 139 149 151 157 163 167 173 179 |
|||
181 191 193 197 199 211 223 227 229 233 |
|||
239 241 251 257 263 269 271 277 281 283 |
|||
293 307 311 313 317 331 337 347 349 353 |
|||
359 367 373 379 383 389 397 401 409 419 |
|||
421 431 433 439 443 449 457 461 463 467 |
|||
479 487 491 499 503 509 521 523 541 547 |
|||
1000000 th Sundaram prime is 15485867 |
|||
Time = 11.0265 segs |
|||
/* Sí, mi lenguaje es lento para algunas cosas... */ |
|||
</pre> |
</pre> |
||