09-02-2016 01:23 PM
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 ...
... 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
Solved! Go to Solution.
09-02-2016 01:37 PM
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
09-02-2016 01:38 PM - edited 09-02-2016 01:44 PM
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.
09-02-2016 01:41 PM
If you search the prime numbers coding challenge theres a method there which is WAY faster than the q&R method.
09-06-2016 09:21 AM
You can always trust Regular Expressions to help.
http://forums.ni.com/t5/BreakPoint/Regular-Expressions-Board/m-p/1188883/highlight/true#M13470
09-06-2016 09:39 AM
That is great Jim!
Thank you!
Ben
09-06-2016 12:00 PM
09-07-2016 07:54 AM
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
09-07-2016 09:24 AM
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".
09-07-2016 09:34 AM
@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