Special odd numbers: Difference between revisions
Content added Content deleted
(→{{header|REXX}}: added the computer programming language REXX.) |
|||
Line 29: | Line 29: | ||
933 939 943 949 951 955 959 965 973 979 985 989 993 995 |
933 939 943 949 951 955 959 965 973 979 985 989 993 995 |
||
</pre> |
</pre> |
||
=={{header|REXX}}== |
|||
<lang rexx>/*REXX program finds squarefree semiprimes (product of 2 primes) that are less then N. */ |
|||
parse arg hi cols . /*obtain optional argument from the CL.*/ |
|||
if hi=='' | hi=="," then hi= 1000 /* " " " " " " */ |
|||
if cols=='' | cols=="," then cols= 10 /* " " " " " " */ |
|||
call genP /*build array of semaphores for primes.*/ |
|||
w= 10 /*width of a number in any column. */ |
|||
@sss= ' squarefree semiprimes < ' commas(1000) |
|||
if cols>0 then say ' index │'center(@sss, 1 + cols*(w+1) ) |
|||
if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') |
|||
idx= 1 /*initialize the index of output lines.*/ |
|||
$=; ss.= 0 /*a list of squarefree semiprimes. */ |
|||
do j=2 while @.j < hi /*generate squarefree semiprimes < HI.*/ |
|||
do k=j+1 while @.k < hi /*ensure primes are squarefree & < HI.*/ |
|||
_= @.j * @.k /*calculate the product of 2 odd primes*/ |
|||
if _>=hi then leave /*Is the product ≥ HI? Then skip it. */ |
|||
ss._= 1 /*mark # as being squarefree semiprime.*/ |
|||
end /*k*/ |
|||
end /*j*/ |
|||
sss= 0 /*the number of squarefree semiprimes. */ |
|||
do m=3 by 2 to hi-1 /*search a list of possible candicates.*/ |
|||
if \ss.m then iterate /*Does this number exist? No, skip it.*/ |
|||
sss= sss + 1 /*bump count of squarefree semiprimes. */ |
|||
if cols==0 then iterate /*Build the list (to be shown later)? */ |
|||
c= commas(m) /*maybe add commas to the number. */ |
|||
$= $ right(c, max(w, length(c) ) ) /*add a Frobenius #──►list, allow big #*/ |
|||
if sss//cols\==0 then iterate /*have we populated a line of output? */ |
|||
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */ |
|||
idx= idx + cols /*bump the index count for the output*/ |
|||
end /*m*/ |
|||
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ |
|||
say |
|||
say 'Found ' commas(sss) @sss |
|||
exit 0 /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
genP: @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6= 13 /*define some low primes. */ |
|||
#=6; s.#= @.# **2 /*number of primes so far; prime²*/ |
|||
/* [↓] generate more primes ≤ high.*/ |
|||
do j=@.#+4 by 2 to hi+1 /*find odd primes from here on. */ |
|||
parse var j '' -1 _; if _==5 then iterate /*J divisible by 5? (right dig)*/ |
|||
if j// 3==0 then iterate /*" " " 3? */ |
|||
if j// 7==0 then iterate /*" " " 7? */ |
|||
if j//11==0 then iterate /*" " " 11? */ |
|||
/* [↑] the above four lines saves time*/ |
|||
do k=6 while s.k<=j /* [↓] divide by the known odd primes.*/ |
|||
if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */ |
|||
end /*k*/ /* [↑] only process numbers ≤ √ J */ |
|||
#= #+1; @.#= j; s.#= j*j /*bump # Ps; assign next P; P squared*/ |
|||
end /*j*/; return</lang> |
|||
{{out|output|text= when using the default inputs:}} |
|||
<pre> |
|||
index │ squarefree semiprimes < 1,000 |
|||
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
1 │ 15 21 33 35 39 51 55 57 65 69 |
|||
11 │ 77 85 87 91 93 95 111 115 119 123 |
|||
21 │ 129 133 141 143 145 155 159 161 177 183 |
|||
31 │ 185 187 201 203 205 209 213 215 217 219 |
|||
41 │ 221 235 237 247 249 253 259 265 267 287 |
|||
51 │ 291 295 299 301 303 305 309 319 321 323 |
|||
61 │ 327 329 335 339 341 355 365 371 377 381 |
|||
71 │ 391 393 395 403 407 411 413 415 417 427 |
|||
81 │ 437 445 447 451 453 469 471 473 481 485 |
|||
91 │ 489 493 497 501 505 511 515 517 519 527 |
|||
101 │ 533 535 537 543 545 551 553 559 565 573 |
|||
111 │ 579 581 583 589 591 597 611 623 629 633 |
|||
121 │ 635 649 655 667 669 671 679 681 685 687 |
|||
131 │ 689 695 697 699 703 707 713 717 721 723 |
|||
141 │ 731 737 745 749 753 755 763 767 771 779 |
|||
151 │ 781 785 789 791 793 799 803 807 813 815 |
|||
161 │ 817 831 835 843 849 851 865 869 871 879 |
|||
171 │ 889 893 895 899 901 905 913 917 921 923 |
|||
181 │ 933 939 943 949 951 955 959 965 973 979 |
|||
191 │ 985 989 993 995 |
|||
Found 194 squarefree semiprimes < 1,000 |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |