LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Way to identify if a number is a prime in Labview?

I need a way to identify if a number is a prime. Are there any inbuilt functions such as the matlab Boolean “isprime(X)” or a simple way to use matlab with labview?

0 Kudos
Message 1 of 18
(9,079 Views)
if you have LabVIEW 8.0 or later, you can use a simple Mathscript node. No need for matlab. 🙂
 
Here are two possible implementation examples:
 

Message Edited by altenbach on 12-17-2006 09:24 AM

Download All
Message 3 of 18
(9,048 Views)
Hi altenbach,

nice example.
But after playing with it, i found: the mathscript node (or the isprime()-command) doesn't allow numbers bigger than 2^31-1 (=I32).
It gives wrong (and sometimes no) results for bigger numbers, even with DBL as input type...

In the attached example you can test this, run with default value or copy and paste from text...

adding: just read in the LabView-help on isprime(): isprime() is based on function factors(), which is limited to numbers <= 2^31-1.

Message Edited by GerdW on 01-11-2007 05:34 PM

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 4 of 18
(8,957 Views)

Don't forget to wire the error-out for the mathscript node. It will let you know if you exceed the valid range.

For example, If I enter 140737488355333, an error occurs  (Code -90001: Error in function isprime at line 1.  A problem occurred in a subVI call.).

(Clearly, for large primes you'll need something along the lines of the winning entry of the prime challenge. ;))

Do you know if the matlab isprime() function has the same restriction? From the matlab help, it says "A must contain only positive integers" so I guess it has the same problem.

Message Edited by altenbach on 01-11-2007 09:20 AM

Message 5 of 18
(8,936 Views)
Hello GerdW,

You have discovered a bug in the MathScript isprime function.  There is now a bug report, so it will be tracked for possible fixing in a future release.

If 32-bit integers are not large enough for your needs, what size would you need?  Would 64-bit integers be large enough or do you really desire more of arbitrary-precision integers?  Surely you may be doing more than just calculating prime numbers.  What are some other functions you would want to be improved?  Perhaps gcd?

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
Message 6 of 18
(8,927 Views)
Hi GrantM and Altenbach,

emmhh, just calculating primes 🙂

I used Matlab more than 10 years ago in an university course, but did not go into details then. When I read this thread I just wanted to test the mathscript node to be included in my prime generator/tester to maybe speed it up...

For the bug report:
I added a new IsPrime-example to show another bug (in my opinion).
If you have a DBL input array and one of the numbers is bigger than 2^32 you get an empty output array!
In my opinion you should get an filled output array and an error saying 'One or more values out of range'...
And even more errors: when giving numbers between 2^31 and 2^32 you don't get an error message, even when the function is not defined and gives wrong results...

Message Edited by GerdW on 01-12-2007 09:51 AM

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 7 of 18
(8,898 Views)
Altenbach, your example is awesome! The only thing I cannot figure out is why the second dimension in the array consists of only 1s and 0s. Is there a conversion to boolean in there somewhere (not likely; the array is type double)? *I'm asking about the first example*
 
---Templar---

Message Edited by God's Templar on 07-25-2007 11:17 AM

0 Kudos
Message 8 of 18
(8,712 Views)
Hi Templar,

the first example is giving the answers as DBL values - hence the 0s and 1s! Just have a look at the corresponding block diagram...

To get booleans you just take the 2nd column and use a "<>0" comparison.
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 9 of 18
(8,671 Views)
Well, all you need to do is go to the matlab site and look up the help for "isprime". You will probab ly read something along the lines of:
 
"TF = isprime(A) returns an array the same size as A containing logical 1 (true) for the elements of A which are prime, and logical 0 (false) otherwise. A must contain only positive integers."
 
If you want the output as boolean array, define the connector as boolean and you'll get a boolean as in the second example. No extra code needed.
 
🙂

Message Edited by altenbach on 07-26-2007 06:53 AM

Message 10 of 18
(8,636 Views)