Prime decomposition: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Minor tidy) |
Walterpachl (talk | contribs) (→optimized slightly: ooRexx conformance end corrected output) |
||
Line 5,128: | Line 5,128: | ||
A method exists in this REXX program to also test Mersenne-type numbers <big>(2<sup>n</sup> - 1)</big>. |
A method exists in this REXX program to also test Mersenne-type numbers <big>(2<sup>n</sup> - 1)</big>. |
||
Since the majority of computing time is spent looking for primes, that part of the program was |
Since the majority of computing time is spent looking for primes, that part of the program was |
||
<br>optimized somewhat (but could be extended if more optimization is wanted). |
<br>optimized somewhat (but could be extended if more optimization is wanted). |
||
<syntaxhighlight lang="rexx">/ |
<syntaxhighlight lang="rexx"></syntaxhighlight> |
||
/*REXX pgm does prime decomposition of a range of positive integers (with a prime count)*/ |
|||
numeric digits 1000 /*handle thousand digits for the powers*/ |
|||
Numeric Digits 1000 /*handle thousand digits For the powers*/ |
|||
Parse Arg bot top step base add /*get optional arguments from the C.L. */ |
|||
If bot='?' Then Do |
|||
⚫ | |||
Say 'rexx pfoo bot top step base add' |
|||
⚫ | |||
Exit |
|||
⚫ | |||
End |
|||
tell= top>0; top=abs(top) /*if TOP is negative, suppress displays*/ |
|||
If bot=='' Then /* no arguments given */ |
|||
Parse Value 1 100 With bot top /* set default range .*/ |
|||
if base\=='' then w=length(base**top) /*will be testing powers of two later? */ |
|||
⚫ | |||
⚫ | |||
If step=='' Then step=1 /* step=2 to process only odd numbers */ |
|||
If add =='' Then add=-1 /* for Mersenne tests */ |
|||
tell=top>0 /*If TOP is negative, suppress displays*/ |
|||
top=abs(top) |
|||
?=n; if base\=='' then ?=base**n + add /*should we perform a "Mercenne" test? */ |
|||
w=length(top) /*get maximum width For aligned display*/ |
|||
If base\=='' Then |
|||
⚫ | |||
w=length(base**top) /*will be testing powers of two later? */ |
|||
⚫ | |||
⚫ | |||
tag.0='{unity}' |
|||
say |
|||
tag.1='[prime]' |
|||
⚫ | |||
Numeric Digits max(9,w+1) /*maybe increase the digits precision. */ |
|||
np=0 /*np: is the number of primes found.*/ |
|||
Do n=bot To top by step /*process a single number or a range. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
?=n |
|||
⚫ | |||
If base\=='' Then /*should we perform a 'Mersenne' test? */ |
|||
?=base**n+add |
|||
do while x//2==0; $=$ 2; x=x%2; end /*append all the 2 factors of new X.*/ |
|||
pf=factr(?) /* get prime factors */ |
|||
f=words(pf) /* number of prime factors */ |
|||
If f=1 Then /* If the number is prime */ |
|||
np=np+1 /* Then bump prime counter */ |
|||
If tell Then |
|||
q=1; do while q<=x; q=q*4; end /*these two lines compute integer √ X */ |
|||
Say right(?,w) right('('f')',9) 'prime factors: ' tag.f pf |
|||
r=0; do while q>1; q=q%4; _=d-r-q; r=r%2; if _>=0 then do; d=_; r=r+q; end; end |
|||
End /*n*/ |
|||
Say '' |
|||
ps='prime' |
|||
⚫ | |||
Say right(np, w+9+1) ps 'found.' /*display the number of primes found. */ |
|||
⚫ | |||
/*---------------------------------------------------------------------------*/ |
|||
factr: Procedure |
|||
⚫ | |||
If x==1 Then Return '' /*handle the special case of X=1. */ |
|||
primes=2 3 5 7 |
|||
Do While primes>'' /* first check the small primes */ |
|||
Parse Var primes prime primes |
|||
Do While x//prime==0 |
|||
pl=pl prime |
|||
x=x%prime |
|||
End |
|||
End |
|||
r=isqrt(x) |
|||
⚫ | |||
Parse var j '' -1 _ /*obtain the last decimal digit of J. */ |
|||
If _\==5 Then Do |
|||
⚫ | |||
pl=pl j |
|||
x=x%j |
|||
⚫ | |||
End |
|||
If _ ==3 Then Iterate /*If next Y is divisible by 5? Skip. */ |
|||
y=j+2 |
|||
Do While x//y==0 |
|||
pl=pl y |
|||
x=x%y |
|||
⚫ | |||
⚫ | |||
If x==1 Then Return pl /*Is residual=unity? Then don't append.*/ |
|||
Return pl x /*return pl with appended residual.*/ |
|||
if _\==5 then do while x//j==0; $=$ j; x=x%j; end /*maybe reduce by J. */ |
|||
isqrt: Procedure |
|||
⚫ | |||
Parse Arg x |
|||
y=j+2; do while x//y==0; $=$ y; x=x%y; end /*maybe reduce by J. */ |
|||
x=abs(x) |
|||
⚫ | |||
Parse Value 0 x with lo hi |
|||
/* [↓] The $ list has a leading blank.*/ |
|||
Do While lo<=hi |
|||
if x==1 then return $ /*Is residual=unity? Then don't append.*/ |
|||
t=(lo+hi)%2 |
|||
return $ x /*return $ with appended residual. */</syntaxhighlight> |
|||
If t**2>x Then |
|||
hi=t-1 |
|||
Else |
|||
lo=t+1 |
|||
End |
|||
Return t |
|||
'''output''' when using the default input of: <tt> 1 100 </tt> |
'''output''' when using the default input of: <tt> 1 100 </tt> |
||
Line 5,178: | Line 5,218: | ||
<pre style="font-size:75%;height:160ex"> |
<pre style="font-size:75%;height:160ex"> |
||
1 (0) prime factors: {unity} |
1 (0) prime factors: {unity} |
||
2 (1) prime factors: [prime] 2 |
2 (1) prime factors: [prime] 2 |
||
Line 5,304: | Line 5,345: | ||
<pre style="font-size:75%> |
<pre style="font-size:75%> |
||
3 (1) prime factors: [prime] 3 |
3 (1) prime factors: [prime] 3 |
||
7 (1) prime factors: [prime] 7 |
7 (1) prime factors: [prime] 7 |
||
Line 5,316: | Line 5,358: | ||
4095 (5) prime factors: 3 3 5 7 13 |
4095 (5) prime factors: 3 3 5 7 13 |
||
8191 (1) prime factors: [prime] 8191 |
8191 (1) prime factors: [prime] 8191 |
||
16383 ( |
16383 (3) prime factors: 3 43 127 |
||
32767 ( |
32767 (3) prime factors: 7 31 151 |
||
65535 (4) prime factors: 3 5 17 257 |
65535 (4) prime factors: 3 5 17 257 |
||
131071 (1) prime factors: [prime] 131071 |
131071 (1) prime factors: [prime] 131071 |
||
262143 ( |
262143 (6) prime factors: 3 3 3 7 19 73 |
||
524287 (1) prime factors: [prime] 524287 |
524287 (1) prime factors: [prime] 524287 |
||
1048575 (6) prime factors: 3 5 5 11 |
1048575 (6) prime factors: 3 5 5 11 31 41 |
||
2097151 ( |
2097151 (4) prime factors: 7 7 127 337 |
||
4194303 (4) prime factors: 3 23 89 683 |
4194303 (4) prime factors: 3 23 89 683 |
||
8388607 (2) prime factors: 47 178481 |
8388607 (2) prime factors: 47 178481 |
||
16777215 (7) prime factors: 3 3 5 7 13 17 241 |
16777215 (7) prime factors: 3 3 5 7 13 17 241 |
||
33554431 ( |
33554431 (3) prime factors: 31 601 1801 |
||
67108863 ( |
67108863 (3) prime factors: 3 2731 8191 |
||
134217727 ( |
134217727 (3) prime factors: 7 73 262657 |
||
268435455 ( |
268435455 (6) prime factors: 3 5 29 43 113 127 |
||
536870911 (3) prime factors: 233 1103 2089 |
536870911 (3) prime factors: 233 1103 2089 |
||
1073741823 ( |
1073741823 (7) prime factors: 3 3 7 11 31 151 331 |
||
2147483647 (1) prime factors: [prime] 2147483647 |
2147483647 (1) prime factors: [prime] 2147483647 |
||
4294967295 (5) prime factors: 3 5 17 257 65537 |
4294967295 (5) prime factors: 3 5 17 257 65537 |
||
8589934591 (4) prime factors: 7 23 89 599479 |
8589934591 (4) prime factors: 7 23 89 599479 |
||
17179869183 (3) prime factors: 3 43691 131071 |
17179869183 (3) prime factors: 3 43691 131071 |
||
34359738367 ( |
34359738367 (4) prime factors: 31 71 127 122921 |
||
68719476735 |
68719476735 (10) prime factors: 3 3 3 5 7 13 19 37 73 109 |
||
137438953471 ( |
137438953471 (2) prime factors: 223 616318177 |
||
274877906943 ( |
274877906943 (3) prime factors: 3 174763 524287 |
||
549755813887 ( |
549755813887 (4) prime factors: 7 79 8191 121369 |
||
1099511627775 ( |
1099511627775 (8) prime factors: 3 5 5 11 17 31 41 61681 |
||
2199023255551 (2) prime factors: 13367 164511353 |
2199023255551 (2) prime factors: 13367 164511353 |
||
4398046511103 ( |
4398046511103 (8) prime factors: 3 3 7 7 43 127 337 5419 |
||
8796093022207 (3) prime factors: 431 9719 2099863 |
8796093022207 (3) prime factors: 431 9719 2099863 |
||
17592186044415 ( |
17592186044415 (7) prime factors: 3 5 23 89 397 683 2113 |
||
35184372088831 ( |
35184372088831 (6) prime factors: 7 31 73 151 631 23311 |
||
70368744177663 (4) prime factors: 3 47 178481 2796203 |
70368744177663 (4) prime factors: 3 47 178481 2796203 |
||
140737488355327 ( |
140737488355327 (3) prime factors: 2351 4513 13264529 |
||
281474976710655 |
281474976710655 (10) prime factors: 3 3 5 7 13 17 97 241 257 673 |
||
562949953421311 ( |
562949953421311 (2) prime factors: 127 4432676798593 |
||
1125899906842623 ( |
1125899906842623 (7) prime factors: 3 11 31 251 601 1801 4051 |
||
8 primes found. |
|||
</pre> |
</pre> |
||
'''output''' when using the input of: <tt> 1 50 1 2 +1 </tt> |
'''output''' when using the input of: <tt> 1 50 1 2 +1 </tt> |
||
Line 5,363: | Line 5,405: | ||
<pre style="font-size:75%> |
<pre style="font-size:75%> |
||
3 (1) prime factors: [prime] 3 |
3 (1) prime factors: [prime] 3 |
||
7 (1) prime factors: [prime] 7 |
|||
15 (2) prime factors: 3 5 |
|||
31 (1) prime factors: [prime] 31 |
|||
63 (3) prime factors: 3 3 7 |
|||
127 (1) prime factors: [prime] 127 |
|||
255 (3) prime factors: 3 5 17 |
|||
511 (2) prime factors: 7 73 |
|||
1023 (3) prime factors: 3 11 31 |
|||
2047 (2) prime factors: 23 89 |
|||
4095 (5) prime factors: 3 3 5 7 13 |
|||
8191 (1) prime factors: [prime] 8191 |
|||
16383 (3) prime factors: 3 43 127 |
|||
32767 (3) prime factors: 7 31 151 |
|||
65535 (4) prime factors: 3 5 17 257 |
|||
131071 (1) prime factors: [prime] 131071 |
|||
262143 (6) prime factors: 3 3 3 7 19 73 |
|||
524287 (1) prime factors: [prime] 524287 |
|||
1048575 (6) prime factors: 3 5 5 11 31 41 |
|||
2097151 (4) prime factors: 7 7 127 337 |
|||
4194303 (4) prime factors: 3 23 89 683 |
|||
8388607 (2) prime factors: 47 178481 |
|||
16777215 (7) prime factors: 3 3 5 7 13 17 241 |
|||
33554431 (3) prime factors: 31 601 1801 |
|||
67108863 (3) prime factors: 3 2731 8191 |
|||
134217727 (3) prime factors: 7 73 262657 |
|||
268435455 (6) prime factors: 3 5 29 43 113 127 |
|||
536870911 (3) prime factors: 233 1103 2089 |
|||
1073741823 (7) prime factors: 3 3 7 11 31 151 331 |
|||
2147483647 (1) prime factors: [prime] 2147483647 |
|||
4294967295 (5) prime factors: 3 5 17 257 65537 |
|||
8589934591 (4) prime factors: 7 23 89 599479 |
|||
17179869183 (3) prime factors: 3 43691 131071 |
|||
34359738367 (4) prime factors: 31 71 127 122921 |
|||
68719476735 (10) prime factors: 3 3 3 5 7 13 19 37 73 109 |
|||
137438953471 (2) prime factors: 223 616318177 |
|||
274877906943 (3) prime factors: 3 174763 524287 |
|||
549755813887 (4) prime factors: 7 79 8191 121369 |
|||
1099511627775 (8) prime factors: 3 5 5 11 17 31 41 61681 |
|||
2199023255551 (2) prime factors: 13367 164511353 |
|||
4398046511103 (8) prime factors: 3 3 7 7 43 127 337 5419 |
|||
8796093022207 (3) prime factors: 431 9719 2099863 |
|||
17592186044415 (7) prime factors: 3 5 23 89 397 683 2113 |
|||
35184372088831 (6) prime factors: 7 31 73 151 631 23311 |
|||
70368744177663 (4) prime factors: 3 47 178481 2796203 |
|||
140737488355327 (3) prime factors: 2351 4513 13264529 |
|||
281474976710655 (10) prime factors: 3 3 5 7 13 17 97 241 257 673 |
|||
562949953421311 (2) prime factors: 127 4432676798593 |
|||
1125899906842623 (7) prime factors: 3 11 31 251 601 1801 4051 |
|||
1125899906842625 (6) prime factors: 5 5 5 41 101 2175126601 |
|||
8 primes found. |
|||
</pre> |
</pre> |
||