Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
56
Task/Semiprime/REXX/semiprime-1.rexx
Normal file
56
Task/Semiprime/REXX/semiprime-1.rexx
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/* REXX ---------------------------------------------------------------
|
||||
* 20.02.2014 Walter Pachl relying on 'prime decomposition'
|
||||
* 21.02.2014 WP Clarification: I copied the algorithm created by
|
||||
* Gerard Schildberger under the task referred to above
|
||||
* 21.02.2014 WP Make sure that factr is not called illegally
|
||||
*--------------------------------------------------------------------*/
|
||||
Call test 4
|
||||
Call test 9
|
||||
Call test 10
|
||||
Call test 12
|
||||
Call test 1679
|
||||
Exit
|
||||
|
||||
test:
|
||||
Parse Arg z
|
||||
If is_semiprime(z) Then Say z 'is semiprime' fl
|
||||
Else Say z 'is NOT semiprime' fl
|
||||
Return
|
||||
|
||||
is_semiprime:
|
||||
Parse Arg z
|
||||
If z<1 | datatype(z,'W')=0 Then Do
|
||||
Say 'Argument ('z') must be a natural number (1, 2, 3, ...)'
|
||||
fl=''
|
||||
End
|
||||
Else
|
||||
fl=factr(z)
|
||||
Return words(fl)=2
|
||||
|
||||
/*----------------------------------FACTR subroutine-----------------*/
|
||||
factr: procedure; parse arg x 1 z,list /*sets X&Z to arg1, LIST=''. */
|
||||
if x==1 then return '' /*handle the special case of X=1.*/
|
||||
j=2; call .factr /*factor for the only even prime.*/
|
||||
j=3; call .factr /*factor for the 1st odd prime.*/
|
||||
j=5; call .factr /*factor for the 2nd odd prime.*/
|
||||
j=7; call .factr /*factor for the 3rd odd prime.*/
|
||||
j=11; call .factr /*factor for the 4th odd prime.*/
|
||||
j=13; call .factr /*factor for the 5th odd prime.*/
|
||||
j=17; call .factr /*factor for the 6th odd prime.*/
|
||||
/* [?] could be optimized more.*/
|
||||
/* [?] J in loop starts at 17+2*/
|
||||
do y=0 by 2; j=j+2+y//4 /*insure J isn't divisible by 3. */
|
||||
if right(j,1)==5 then iterate /*fast check for divisible by 5. */
|
||||
if j*j>z then leave /*are we higher than the v of Z ?*/
|
||||
if j>Z then leave /*are we higher than value of Z ?*/
|
||||
call .factr /*invoke .FACTR for some factors.*/
|
||||
end /*y*/ /* [?] only tests up to the v X.*/
|
||||
/* [?] LIST has a leading blank.*/
|
||||
if z==1 then return list /*if residual=unity, don't append*/
|
||||
return list z /*return list, append residual. */
|
||||
/*-------------------------------.FACTR internal subroutine----------*/
|
||||
.factr: do while z//j==0 /*keep dividing until we can't. */
|
||||
list=list j /*add number to the list (J). */
|
||||
z=z%j /*% (percent) is integer divide.*/
|
||||
end /*while z··· */ /* // ?---remainder integer ÷.*/
|
||||
return /*finished, now return to invoker*/
|
||||
30
Task/Semiprime/REXX/semiprime-2.rexx
Normal file
30
Task/Semiprime/REXX/semiprime-2.rexx
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/*REXX program determines if any number (or a range) is/are semiprime.*/
|
||||
parse arg bot top . /*obtain #s from the command line*/
|
||||
if bot=='' then bot=random() /*so, the user wants us to guess.*/
|
||||
if top=='' then top=bot /*maybe define a range of numbers*/
|
||||
w=max(length(bot), length(top)) /*get maximum width of numbers. */
|
||||
if w>digits() then numeric digits w /*is there enough digits ? */
|
||||
do n=bot to top /*show results for a range of #s.*/
|
||||
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 done.*/
|
||||
/*──────────────────────────────────ISPRIME subroutine──────────────────*/
|
||||
isPrime: procedure; parse arg x; if x<2 then return 0
|
||||
if wordpos(x,'2 3 5 7')\==0 then return 1 /*handle some special cases*/
|
||||
do i=2 for 2; if x//i==0 then return 0; end /*i*/ /*÷ by 2 & 3*/
|
||||
do j=5 by 6 until j*j>x; if x//j==0 then return 0 /*¬ a prime#*/
|
||||
if x//(j+2)==0 then return 0 /*¬ a prime#*/
|
||||
end /*j*/
|
||||
return 1 /*X is a prime number, for sure.*/
|
||||
/*──────────────────────────────────ISSEMIPRIME subroutine──────────────*/
|
||||
isSemiPrime: procedure; arg x; if \datatype(x,'W') | x<4 then return 0
|
||||
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*/ /* [↑] divides by two and three.*/
|
||||
do j=5 by 6; if j*j>x then return 0 /*÷ by #s. */
|
||||
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 # divisible by 3*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue