Semiprime: Difference between revisions

Content added Content deleted
mNo edit summary
m (→‎version 2: added/changed comments and whitespace, changed indentations.)
Line 1,245: Line 1,245:


===version 2===
===version 2===
The method used is to examine numbers, skipping primes.
The method used is to examine integers, skipping primes.


If it's composite (the 1<sup>st</sup> factor is prime), then check if the 2<sup>nd</sup> factor is prime. &nbsp; If so, the number is a semiprime.
If it's composite (the 1<sup>st</sup> factor is prime), then check if the 2<sup>nd</sup> factor is prime. &nbsp; If so, the number is a &nbsp; ''semiprime''.


The &nbsp; '''isPrime''' &nbsp; function could be optimized by utilizing an integer square root function instead of testing if &nbsp; '''j*j>x''' &nbsp; for every divisor.
The &nbsp; '''isPrime''' &nbsp; function could be optimized by utilizing an integer square root function instead of testing if &nbsp; '''j*j>x''' &nbsp; for every divisor.
<lang rexx>/*REXX program determines if any number (or a range) is/are semiprime. */
<lang rexx>/*REXX program determines if any integer (or a range of integers) is/are semiprime. */
parse arg bot top . /*obtain optional numbers from the C.L.*/
parse arg bot top . /*obtain optional arguments from the CL*/
if bot==''|bot=="," then bot=random() /*None given? User wants us to guess.*/
if bot=='' | bot=="," then bot=random() /*None given? User wants us to guess.*/
if top==''|top=="," then top=bot /*maybe define a range of numbers. */
if top=='' | top=="," then top=bot /*maybe define a range of numbers. */
w=max(length(bot), length(top)) /*obtain the maximum width of numbers. */
w=max(length(bot), length(top)) /*obtain the maximum width of numbers. */
numeric digits max(9,w) /*ensure there're enough decimal digits*/
numeric digits max(9, w) /*ensure there're enough decimal digits*/
do n=bot to top /*show results for a range of numbers. */
do n=bot to top /*show results for a range of numbers. */
if isSemiPrime(n) then say right(n,w) " is semiprime."
if isSemiPrime(n) then say right(n, w) " is semiprime."
else say right(n,w) " isn't semiprime."
else say right(n, w) " isn't semiprime."
end /*n*/
end /*n*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
isPrime: procedure; parse arg x; if x<2 then return 0 /*number too low*/
isPrime: procedure; parse arg x; if x<2 then return 0 /*number too low?*/
if wordpos(x, '2 3 5 7 11 13 17 19 23')\==0 then return 1 /*it's low prime*/
if wordpos(x, '2 3 5 7 11 13 17 19 23')\==0 then return 1 /*it's low prime.*/
if x//2==0 then return 0; if x//3==0 then return 0 /*÷ by 2;÷ by 3?*/
if x//2==0 then return 0; if x//3==0 then return 0 /*÷ by 2; ÷ by 3?*/
do j=5 by 6 until j*j>x; if x//j==0 then return 0 /*not a prime. */
do j=5 by 6 until j*j>x; if x//j==0 then return 0 /*not a prime. */
if x//(j+2)==0 then return 0 /* " " " */
if x//(j+2)==0 then return 0 /* " " " */
end /*j*/
end /*j*/
return 1 /*indicate that X is a prime number. */
return 1 /*indicate that X is a prime number. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
isSemiPrime: procedure; parse arg x; if \datatype(x,'W') | x<4 then return 0
isSemiPrime: procedure; parse arg x; if x<4 then return 0

x=x/1
do i=2 for 2; if x//i==0 then if isPrime(x%i) then return 1
do i=2 for 2; if x//i==0 then if isPrime(x%i) then return 1
else return 0
else return 0
end /*i*/
end /*i*/
/* ___ */
/* ___ */
do j=5 by 6; if j*j>x then return 0 /* > √ x ? */
do j=5 by 6; if j*j>x then return 0 /* > √ x ?*/
do k=j by 2 for 2; if x//k==0 then if isPrime(x%k) then return 1
do k=j by 2 for 2; if x//k==0 then if isPrime(x%k) then return 1
else return 0
else return 0
end /*k*/ /* [↑] see if 2nd factor is prime or ¬*/
end /*k*/ /* [↑] see if 2nd factor is prime or ¬*/
end /*j*/ /* [↑] never ÷ by J if J is mult. of 3*/</lang>
end /*j*/ /* [↑] J is never a multiple of three.*/</lang>
'''output''' &nbsp; when the input is: &nbsp; <tt> -1 &nbsp; 106 </tt>
'''output''' &nbsp; when using the input of: &nbsp; <tt> -1 &nbsp; 106 </tt>
<pre style="height:44ex">
<pre style="height:44ex">
-1 isn't semiprime.
-1 isn't semiprime.
Line 1,392: Line 1,392:
106 is semiprime.
106 is semiprime.
</pre>
</pre>
'''output''' &nbsp; when the input is: &nbsp; <tt> 99888111555 &nbsp; 99888111600 </tt>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 99888111555 &nbsp; 99888111600 </tt>
<pre style="height:44ex">
<pre style="height:44ex">
99888111555 isn't semiprime.
99888111555 isn't semiprime.