01-17-2009 12:21 AM
hi, I am writing prime number program using labview.I wrote the same with matlab as below
N=input('enter number ');
prime=[];
for j=3:N;
M=ceil(j/2);
for i =2:M;
k = mod(j,i);
if k==0
break;
end
if i==M
prime=[prime j];
end
end
end
prime=[2 prime];
Now ,in labview i hv 2 questions
1)how i implement for loop which starts at say 3 and ends at say N=20
2)how do i manipulate loop variable?
any help will be highly appreciated
thnks
sedy
01-17-2009 03:05 AM - edited 01-17-2009 03:06 AM
sedy wrote:Now ,in labview i hv 2 questions
1)how i implement for loop which starts at say 3 and ends at say N=20
2)how do i manipulate loop variable?
(1) Well, you just calculate the number of iterations and then increment the iteration terminal by the start value, e.g. as follows:
(2) Sorry, I don't quite understand what kind of "manipulations" you have in mind.
You might also want to look at the prime factor challenge discussion. It shows a few algorithms to generate prime factors a bit more efficiently.
01-17-2009 04:52 AM
If you need a variable for the loop, use a shift register.
Right click on the border of the loop and select add shift register.
01-17-2009 09:44 AM
hi,
thnks.
let me try first.
01-17-2009 09:44 AM
hi,
ok,let me try.
thnks
01-17-2009 12:33 PM
hi,
I have done that and even i m getting prime numbers as variable say j. now,the problem is,this variable is inside 2 for loops,
so when i want to make array out of it, i simply updates value at same location.e.g for prime numbers between 2 to 10, it updates value 7(last prime
number) at same loaction instaed of displaying 2,3,5,7. i tried with bulid array,insert array,no use, they update at same location.
so, how can i make array in which elemnts add automatically at contigous locations?
01-17-2009 01:04 PM
hi,
I have done that and even i m getting prime numbers as variable say j. now,the problem is,this variable is inside 2 for loops,
so when i want to make array out of it, i simply updates value at same location.e.g for prime numbers between 2 to 10, it updates value 7(last prime
number) at same loaction instaed of displaying 2,3,5,7. i tried with bulid array,insert array,no use, they update at same location.
so, how can i make array in which elemnts add automatically at contigous locations?
Inside the "for loop" you can add an "if statement" or "while loop" inside the "for loop" to do this. Code it so that they will kill when a prime number is found and or when the range is meet, and you get your output just the way you want it. I had to do something simlar in a class a long time ago and I found a "for" and "while" loop combination worked the best.
I thought there was a prime number function already built into labview or it may have been a custom library you could download??? I do remember it, and being angery I had to do all that work in class.
Zi
01-17-2009 03:17 PM - edited 01-17-2009 03:21 PM
For illustration, here's a "near literal" translation of your code into LabVIEW with some annotations. See if it makes sense. 😉
(There are a couple of problems with your algorithm, for example if N=1 or 0, primes = [2], because the "2" is prepended without thinking ;))
Of course, as I mentioned before, there are much (much!!!) faster and more efficient algorithms for all this 😄
.... However, for learning about FOR loops, this is fine. See of you find any bugs 😉
01-17-2009 05:20 PM
hi alten,
well,I had made the program almost 90% similar to wht u hv shown. and for N=0,N=1, i was not that much bothered.so,let me c,if the array portion i can work out.
thnks so much,i cant beleive people can spare time to make program out of my code..i really am thankful.
sedy