Semiprime: Difference between revisions

m
→‎version 2: added/changed whitespace and comments, made other changes, added a much higher range for 2nd output.
m (→‎{{header|Erlang}}: cleanup formatting)
m (→‎version 2: added/changed whitespace and comments, made other changes, added a much higher range for 2nd output.)
Line 1,110:
 
===version 2===
The method used is to examine numbers, skipping primes. &nbsp; If 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.
 
<lang rexx>/*REXX program determines if any number (or a range) is/are 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 semiprime.
parse arg bot top . /*obtain #s from the command line*/
 
if bot=='' then bot=random() /*so, the user wants us to guess.*/
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.
if top=='' then top=bot /*maybe define a range of numbers*/
<lang rexx>/*REXX program determines if any number (or a range) is/are semiprime. */
w=max(length(bot), length(top)) /*get maximum width of numbers. */
parse arg bot top . /*obtain #soptional numbers from the command lineC.L.*/
if w>digits() then numeric digits w /*is there enough digits ? */
if bot==''|bot=="," then bot=random() /*None given? /*so, the userUser wants us to guess.*/
do n=bot to top /*show results for a range of #s.*/
if top==''|top=="," then top=bot /*maybe define a range of numbers. */
if isSemiPrime(n) then say right(n,w) ' is semiprime.'
w=max(length(bot), length(top)) /*getobtain the maximum width of numbers. */
else say right(n,w) " isn't semiprime."
numeric digits max(9,w) /*ensure there're enough decimal digits*/
do n=bot to top /*show results for a range of #snumbers. */
if isSemiPrime(n) then say right(n,w) ' " is semiprime.'"
else say right(n,w) " isn't semiprime."
end /*n*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────ISPRIME subroutine──────────────────*/
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 /*handle someit's speciallow casesprime*/
if do i=x//2==0 forthen 2return 0; if x//i3==0 then return 0; endthen return /*i*/ 0 /*÷ by 2 &by 3?*/
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 /*¬ a" " " prime#*/
end /*j*/
return 1 /*indicate that X is a prime number, for sure. */
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────ISSEMIPRIME subroutine──────────────*/
isSemiPrime: procedure; parse arg x; if \datatype(x,'W') | x<4 then return 0
x=x/1
x=x/1 /*normalize the X number. */
do i=2 for 2; if x//i==0 then if isPrime(x%i) then return 1
else return 0
end /*i*/ end /* [↑] divides by two and three.i*/
do j=5 by 6; if j*j>x then return 0 /*÷ by #s. ___ */
x=x/1 do j=5 by 6; if j*j>x then return 0 /*normalize the X /* number.> x ? */
do k=j by 2 for 2; if x//k==0 then if isPrime(x%k) then return 1
else return 0
end /*k*/ /* [↑] see if 2nd factor is prime or ¬*/
end /*j*/ /* [↑] never ÷ by #J divisibleif byJ is mult. of 3*/</lang>
'''output''' &nbsp; when the input is: &nbsp; <tt> -1 &nbsp; 106 </tt>
<pre style="height:40ex">
-1 isn't semiprime.
Line 1,251 ⟶ 1,256:
105 isn't semiprime.
106 is semiprime.
</pre>
'''output''' &nbsp; when the input is: &nbsp; <tt> 99888111555 &nbsp; 99888111600 </tt>
<pre style="height:40ex">
99888111555 isn't semiprime.
99888111556 isn't semiprime.
99888111557 isn't semiprime.
99888111558 isn't semiprime.
99888111559 isn't semiprime.
99888111560 isn't semiprime.
99888111561 isn't semiprime.
99888111562 isn't semiprime.
99888111563 is semiprime.
99888111564 isn't semiprime.
99888111565 isn't semiprime.
99888111566 is semiprime.
99888111567 isn't semiprime.
99888111568 isn't semiprime.
99888111569 is semiprime.
99888111570 isn't semiprime.
99888111571 isn't semiprime.
99888111572 isn't semiprime.
99888111573 isn't semiprime.
99888111574 is semiprime.
99888111575 isn't semiprime.
99888111576 isn't semiprime.
99888111577 isn't semiprime.
99888111578 is semiprime.
99888111579 isn't semiprime.
99888111580 isn't semiprime.
99888111581 isn't semiprime.
99888111582 isn't semiprime.
99888111583 isn't semiprime.
99888111584 isn't semiprime.
99888111585 isn't semiprime.
99888111586 isn't semiprime.
99888111587 isn't semiprime.
99888111588 isn't semiprime.
99888111589 isn't semiprime.
99888111590 isn't semiprime.
99888111591 is semiprime.
99888111592 isn't semiprime.
99888111593 is semiprime.
99888111594 isn't semiprime.
99888111595 isn't semiprime.
99888111596 isn't semiprime.
99888111597 isn't semiprime.
99888111598 isn't semiprime.
99888111599 isn't semiprime.
99888111600 isn't semiprime.
</pre>