LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

for loop query

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 

0 Kudos
Message 1 of 9
(3,612 Views)

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.

Message Edited by altenbach on 01-17-2009 01:06 AM
Message 2 of 9
(3,598 Views)

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.

0 Kudos
Message 3 of 9
(3,587 Views)

hi,

thnks.

let me try first. 

0 Kudos
Message 4 of 9
(3,568 Views)

hi,

ok,let me try.

thnks 

0 Kudos
Message 5 of 9
(3,567 Views)

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? 

0 Kudos
Message 6 of 9
(3,548 Views)

 


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

 

0 Kudos
Message 7 of 9
(3,544 Views)

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 😉

Message Edited by altenbach on 01-17-2009 01:21 PM
Download All
Message 8 of 9
(3,530 Views)

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 

0 Kudos
Message 9 of 9
(3,518 Views)