Try it yourself

Share a quote, a general thought, jokes or one liners here.
Post Reply
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Try it yourself

Post 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
zprogrammer
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 826
Joined: Wed Sep 11, 2013 3:57 pm

Re: Try it yourself

Post 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
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-)
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: Try it yourself

Post by zprogrammer »

Got to break my fingers before posting next time :mrgreen:
zprogrammer
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 826
Joined: Wed Sep 11, 2013 3:57 pm

Re: Try it yourself

Post 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 :(
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-)
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “Thought of the Day, General Talk & Jokes.”