I'm newbie, please help me! (calculation problem in macro)

Thanks for read this question.

I have been using Igor to make good looking graph for years,
recently I decided to try to using procedure for automation even if newbie I am at
computer programming.

Then I tried to make easy prime number searching program for self learning.
But the result show some unexpected numbers, like 27, 35, but almost correct.
I can't understand why the program skip the step like (27/3), (27/9), (35/5)...

Please give me an advice.

Best regards.



procedure
-------------------------------------------------------------------------------------------------------------------------

#pragma rtGlobals=1 // Use modern global access method.
macro test()
pauseupdate
silent 1
killwaves /a
make /N = 100 stack1
variable v1
variable v2
variable count1
variable timeron
variable timeroff
timeron = startmstimer
v1 = 2
v2 = 2
count1 = 0
do
if (v1 == 100)
print "end"
break
else
if ( mod(v1, v2) == 0)
if (v1/v2 == 1)
stack1[count1] = v1
count1 = count1 + 1
v1 = v1 + 1
v2 = 2
else
v1 = v1 + 1
endif

else
v2 = v2 + 1
endif
endif
while(1)
timeroff=stopmstimer(timeron)
print "Calculation time is...."
print timeroff / 1000
print "ms"
print stack1
endmacro
I have not looked at it in detail but keep in mind that all calculations are done in double-precision floating point. If you want to do integer arithmetic you need to use trunc to truncate to integer after doing division and mod.

To learn how to use the Igor debugger, execute this:
DisplayHelpTopic "The Debugger"



Also, with rare exceptions you should write functions rather than macros. Execute this:
DisplayHelpTopic "Converting Macros To Functions"


If you have not already done it, read chapters IV-1 through IV-3 of the Igor PDF manual. This will give you an introduction to Igor programming.
Howard is right to point out potential problems with floating-point arithmetic. But in your case, all the numbers are integers, and as long as they are small enough, integer arithmetic will be exact.

The problem is actually with your algorithm. If one number is not a prime, then testing for the next number does not start over at v2=2.

I figured that out using Igor's symbolic debugger, stepping through your code one line at a time, and watching how the values of the variables change during execution. To enable the debugger, right-click in any procedure window and select Enable Debugger. To learn how to use the debugger, execute this command line in Igor:

DisplayHelpTopic "The Debugger"

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
FWIW, here are links to algorithms that generate prime numbers in other code for possible translation to Igor Pro.

http://www.astahost.com/info.php/Prime-Number-Generator_t11203.html

http://www.daniweb.com/software-development/python/code/216558

http://www.codingforums.com/showthread.php?t=167710

You might consider posting your code on the Code Snippets tab once you have it operating, as other folks might likely find it useful.

Perhaps someone resourceful could even use one of these algorithms to create an XOP with a function primes(ww) that fills wave ww with primes.

Also note the statement in one of these URLs that 1 is apparently now no longer considered the first prime number.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
Thanks for many advice and suggestion!!
I find out the mistake by using debugger!

So much thanks!


------------------------------------------------------------
#pragma rtGlobals=1 // Use modern global access method.
macro test()
pauseupdate
silent 1
killwaves /a
make /N = 100 stack1
variable v1
variable v2
variable count1
variable timeron
variable timeroff
timeron = startmstimer
v1 = 2
v2 = 2
count1 = 0
do
if (v1 == 100)
print "end"
break
else
if ( mod(v1, v2) == 0)
if (v1/v2 == 1)
stack1[count1] = v1
count1 = count1 + 1
v1 = v1 + 1
v2 = 2
else
v1 = v1 + 1
v2 = 2 // <-----------HERE!!!
endif

else
v2 = v2 + 1
endif
endif
while(1)
timeroff=stopmstimer(timeron)
print "Calculation time is...."
print timeroff / 1000
print "ms"
print stack1
endmacro