first N primes

No Language here - just algorithms!
Previous topicNext topic

Topic Author
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 892
Joined: Wed Sep 11, 2013 3:57 pm

first N primes

Post by enrico-sorichetti » Thu Jun 25, 2015 7:40 pm

Code: Select all

#!  /usr/bin/rexx
Trace "O"
signal on novalue name novalue
numeric digits 32

parse arg n
if  n = "" then ,
    n = 100

w = length(n)

k = 1
say right(k, w) 2
do p = 3 by 2 while k < n
    if  isprime(p) then do
        k += 1
        say right(k, w) p
    end
end

Exit

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
isPrime: procedure
    parse arg p
    if  p = 1 then ,
        return 0
    if  p < 4 then ,
        return 1
    if  p // 2 = 0 then ,
        return 0
    if  p < 9 then ,
        return 1
    if  p // 3 = 0 then ,
        return 0

    f = 5
    do  while ( f * f <= p )
        if  p // f = 0 then ,
            return 0
        if  p // (f+2) = 0 then ,
            return 0
        f +=6
    end
    return 1

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
logic_error:
say "**"copies(" -", 35)
say "**"
say "**" "Logic error at line '"sigl"' "
say "**"
say "**"copies(" -", 35)
exit

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
novalue:
say "**"copies(" -", 35)
say "**"
say "**" "Novalue trapped, line '"sigl"' var '"condition("D")"' "
say "**"
say "**"copies(" -", 35)
exit



cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)


nicc
Global Moderator
Global Moderator
Posts: 597
Joined: Wed Apr 23, 2014 8:45 pm

Re: first N primes

Post by nicc » Thu Jun 25, 2015 8:51 pm

k += 1
OMG! Has someone trying to turn rexx into a 'curlty-wurly' language or did you write this in one of those first and fail to convert this line?


Regards
Nic


Topic Author
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 892
Joined: Wed Sep 11, 2013 3:57 pm

Re: first N primes

Post by enrico-sorichetti » Thu Jun 25, 2015 9:02 pm

NOPE ;)

open object rexx is getting curlier

even if I will never get used to

Code: Select all

l = somestring~length()
instead of the simpler

Code: Select all

l = length(some string)
one nice things of ooRexx is that it handles REAL arrays

apart the initial definition

Code: Select all

somearray = .array~new(dimensions list)
it is nice to be able to write

Code: Select all

do i = 1 to imax
    do j = 1 to jmax
        somearray[i,j] = some_expression
    end
end 



cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)

Previous topicNext topic

Return to “Programming Algorithms.”