Please help in this simple coding

adittyap
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.
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.
0
Comments
-
So add in a separate condition to take the number 2 into account, and do the existing "J" loop for all other numbers.0
-
i am not getting it. Could you please write its code.....0
-
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.0 -
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.0 -
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.0
-
I have to admit that i forgot that 2 is a prime too :oops:0
-
thankz everybody.... :-)0
-
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?0 -
Well step through the code and debug. Check variable values at each step and see what happens.0
-
Use Google. Plenty of examples for code that generate prime numbers, then convert the syntax to NAV.0
-
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.0 -
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.0 -
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;1
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions