LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Testing a number to see if it s prime WITHOUT using the Q&R function...Possible?

Solved!
Go to solution

We challenge each other at my place with LabVIEW challenges. I threw together a bunch of examples for this weeks challenge that requires finding prime numbers, but aside from one hack ...

 

Spoiler
The "hack" uses Datsockets to read from a web-site that has a list of prime numbers and I parse the text to get the list of primes. Like I said a hack.

 

 

... all of my examples used the Q&R function.

 

Realizing that I asked myself how I could check a number to see if it is prime WITHOUT the Q&R and I could not think of one.

 

Can you?

 

Don't waste your time trying to come up with one becuase I do not have a problem I am trying to solve onyl curious if there are alternatives the the Q&R node.

 

Thank you for reading!

 

Ben 

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 1 of 20
(4,191 Views)

There is something about the formality of posting a question here that engages sleeping brain cells...

 

This question has been lurking in the back of my head for three days and now that I have posted, it occured to me that I only have to apply the grade school idea of division and repeatedly subtract a number until end up with a number that is smaller than what I trying to divide by and if it is not zero then there is a remainder.

 

Just ignore me now!

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 2 of 20
(4,180 Views)
Solution
Accepted by Ben

Well, have a look at my prime sum code from the 2016 NI-Week presentation. Both sieves (Eratosthenes, Sundaram)  don't directly rely on Q&R and could easily be rewritten to not use it (e.g. by replacing the innermost loop with a while loop  and terminate on a comparison etc.. Try it!)

 

Of course you could also write your own Q&R from other primitives, e.g. using newton-raphson. I wrote one very long ago for one of the coding challenges to support arbitrary resolution numbers.

 

You could also use plain division and see if the result is equal (or close enough, due to DBL limitations) to the result rounded to the nearest integer.

 

Of course primality testing of very large numbers (many, many digits) is a special field of mathematics. 

Message 3 of 20
(4,177 Views)

If you search the prime numbers coding challenge theres a method there which is WAY faster than the q&R method.

Message 4 of 20
(4,174 Views)
Solution
Accepted by Ben

You can always trust Regular Expressions to help.

 

http://forums.ni.com/t5/BreakPoint/Regular-Expressions-Board/m-p/1188883/highlight/true#M13470

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 5 of 20
(4,034 Views)

That is great Jim!

 

Thank you!

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 6 of 20
(4,028 Views)
Maybe I should add this one to my benchmarks :o:D
0 Kudos
Message 7 of 20
(4,005 Views)

I want to thank you again Jim for reminding me of the regular expresion post by you to test a number for primeness. It was very helpful to my efforts.

 

We have some young wire-workers that have a text based background and are often reaching for a text based answer to LV coding tasks. That example baffled them and before long we had an adhoc training meeting going to try and figure out that thing even worked.

 

Putting tahat approach next to a version of code that used the Q&R approach made it very clear just how easy it was to "read" the LV version and determine what was happening vs the regular expresion approach.

 

Quoting myself from some time ago why I love LabVIEW...

 

NO SYNTAX REQUIRED!

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 8 of 20
(3,961 Views)

It's not fair to use that example in an effort to make the case that LabVIEW is better than text.  😄  You might as well pull an example from a C "obfuscation contest".

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 9 of 20
(3,950 Views)

@jcarmody wrote:

It's not fair to use that example in an effort to make the case that LabVIEW is better than text.  😄  You might as well pull an example from a C "obfuscation contest".


I was not making that point (while it is true) but rather that there are situations where a text based solution is not always the best approach. The younguns are all to often "seeing what they think is a nail" and reaching for the hammer they are comfortable using rather than considering trying out a screwdriver.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 10 of 20
(3,945 Views)