Page 1 of 1

Try it yourself

Posted: Thu Mar 06, 2014 8:34 pm
by zprogrammer
Technical puzzle

Below code in REXX generates prime numbers present between 1 to 1000.
Try to solve the same puzzle in Cobol.If you are able to solve in Cobol try a different language.

See how many languages you could solve the below problem ;)

Code: Select all

/*REXX*/
DO Y = 1000 TO 1 BY -1
   N = 0
   DO X = 1 TO 1000 BY +1
      Z = INDEX(Y/X,'.')
      IF Z = 0 THEN DO
         N     = N+1
         Y     = RIGHT(Y,4)
      END
   END
   IF N<=2 THEN DO
      SAY Y  'IS A PRIME NUMBER'
   END
END

Re: Try it yourself

Posted: Thu Mar 06, 2014 9:28 pm
by enrico-sorichetti
programming note...

the INDEX builtin is not portable
use pos reversing the operands

why use pos when You have available the modulus operation //

and why ... why ... why ...

I tested Your algorithm for 10000 number
and the elapsed was 1 minute and 23 seconds

here is a better solution :ugeek:

Code: Select all

primes = "3 5 7 11 13 17 19"

do  n = 21 to 10000 by 2
    if  right(n,1) = "5" then ,
        iterate n
    do p = 1 to words(primes)
        if  n//word(primes,p) = 0 then ,
            iterate n
    end
    primes = primes n
end

say "    1"
say "    2"
do  p = 1 to words(primes)
    say right(word(primes,p), 5)
end
the elapsed for above algorithm is 2.61 seconds
skipping the division by 5 provides just a very small gain U up to 10000
2.61 instead of 2.66

Re: Try it yourself

Posted: Thu Mar 06, 2014 10:01 pm
by zprogrammer
Got to break my fingers before posting next time :mrgreen:

Re: Try it yourself

Posted: Fri Mar 07, 2014 1:26 am
by enrico-sorichetti
for interesting problems I suggest to lurk here
https://projecteuler.net/problems

if I just could remember my login I could pull out my solutions
I wrote them in REXX, python, C

IIRC I solved about 100 problems, too bad that also the backup of my coding has gone :(