Please help in this simple coding

adittyapadittyap Member Posts: 13
FOR i:=2 TO 20 DO BEGIN
prime:= TRUE;
FOR j:=2 TO i-1 DO BEGIN
IF i MOD j=0 THEN
prime:=FALSE;
END;
IF prime=TRUE THEN
MESSAGE(Text1000,i) ;
END;


i want to run the coding of prime numbers on the form itself.
please tell me whether my code is right or wrong, it showing 2 as a prime number, but whn i dry
run it for i:=2 it should not include 2 as a prime number. Please correct me.

Comments

  • matttraxmatttrax Member Posts: 2,309
    So add in a separate condition to take the number 2 into account, and do the existing "J" loop for all other numbers.
  • adittyapadittyap Member Posts: 13
    i am not getting it. Could you please write its code.....
  • MBergerMBerger Member Posts: 413
    This forum is not to write the code for you, but to help you understand what is wrong.
    the problem here is that in the case of 2, the second loop ( the one with the J variable ) will never execute. Just look at it, it'll read "FOR J := 2 to 1 DO", and that won't do anything.
  • SogSog Member Posts: 1,023
    adittyap wrote:
    [snip]code...[/snip]
    i want to run the coding of prime numbers on the form itself.
    please tell me whether my code is right or wrong, it showing 2 as a prime number, but whn i dry
    run it for i:=2 it should not include 2 as a prime number. Please correct me.

    Definition of prime: A natural number is called a prime number (or a prime) if it is bigger than one and has no divisors other than 1 and itself. [source=Wikipedia]

    Which means that by definition 2 is a prime.

    So there is nothing wrong with your code, only with your interpretation :)
    If you do not want to see 2, then just add a check that when i is equal to 2, it shouldn't show the message.
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • matttraxmatttrax Member Posts: 2,309
    Yeah, 2 is a prime number. In fact it is the only even prime number. So I'm not exactly sure what the problem is with the code. I had an "oops" moment and was thinking the FOR loop wouldn't check the condition until the end (it was early :oops: ). I don't see anything wrong with that code.
  • MBergerMBerger Member Posts: 413
    I have to admit that i forgot that 2 is a prime too :oops:
  • adittyapadittyap Member Posts: 13
    thankz everybody.... :-)
  • adittyapadittyap Member Posts: 13
    you have said "2nd loop will never execute", if it is so? , so why it showing the correct output (2,3,5,7,11,13,17,19)

    When i know according to my logic , it shouldnt print 2 as a pime number. Any one can clear my doubt?
  • DenSterDenSter Member Posts: 8,305
    Well step through the code and debug. Check variable values at each step and see what happens.
  • Marije_BrummelMarije_Brummel Member, Moderators Design Patterns Posts: 4,262
    Use Google. Plenty of examples for code that generate prime numbers, then convert the syntax to NAV.
  • matttraxmatttrax Member Posts: 2,309
    adittyap wrote:
    you have said "2nd loop will never execute", if it is so? , so why it showing the correct output

    Just look at it line by line. If the 2nd loop doesn't execute, how will you ever set "prime" equal to false? The debugger is an excellent tool for figuring out these types of issues.
  • SogSog Member Posts: 1,023
    first loop is 2 until 20
    second loop is from 2 until the first loops current number -1.
    If the first loop is at number 2, the second loop won't trigger. (because its for 2 to 2-1)
    MBerger pointed out that the second loop will never trigger with the number 2, and did not mean never at all.
    That's why this piece of code is very good for prime numbers, even tough it means that the code won't run trough every loop.
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • adittyapadittyap Member Posts: 13
    thankz all,

    this is my final code and its working:


    FOR i:=2 TO 20 DO BEGIN

    IF i=2 THEN BEGIN
    MESSAGE(Text1000,i)
    END
    ELSE
    BEGIN
    prime:= TRUE;
    FOR j:=2 TO i-1 DO BEGIN
    IF i MOD j=0 THEN
    prime:=FALSE;
    END;
    IF prime=TRUE THEN
    MESSAGE(Text1000,i) ;
    END;
    END;
Sign In or Register to comment.