From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

BreakPoint

cancel
Showing results for 
Search instead for 
Did you mean: 

Rube Goldberg Code

All this talk about simple clocks made me think about this one:

Kind Regards,
Thierry C - CLA, CTA - Senior R&D Engineer (Former Support Engineer) - National Instruments
If someone helped you, let them know. Mark as solved and/or give a kudo. 😉
Message 1501 of 2,571
(10,804 Views)

An atypical Rube Goldberg submission, insofar as it's not seemingly so bad. Neither do I post this here to ridicule the author's solution to a Project Euler question; there are many ways to skin a cat, after all.

 

Found here:

 

https://decibel.ni.com/content/docs/DOC-38402

 

 

Doesn't look too bad, right? Except it takes some 25 s to execute for a control value of 2,000,000. You can drastically reduce the time as soon as you realise that isprime(x) will accept array inputs.

 

As I explain in the comment in that post, the fun thing about critical reviews of your own work is finding quicker ways of doing things and sometimes learning new things.

---
CLA
Message 1502 of 2,571
(10,713 Views)

@thoult wrote:

Doesn't look too bad, right? Except it takes some 25 s to execute for a control value of 2,000,000. You can drastically reduce the time as soon as you realise that isprime(x) will accept array inputs.


Even with arrays, the Mathscript node is way too slow. 

 

I dug up my old prime number generator from the prime factor challenge way back, and using it, I can find the same sum in under 10ms on my 8 year old laptop (no constant folding involved). 😄 There is definitely a lot of slack left and you should be able to speed it up by another 2 orders of magnitude. Try it 😄

0 Kudos
Message 1503 of 2,571
(10,688 Views)

@altenbach wrote:

 

Even with arrays, the Mathscript node is way too slow. 

I dug up my old prime number generator from the prime factor challenge way back, and using it, I can find the same sum in under 10ms on my 8 year old laptop (no constant folding involved). 😄 There is definitely a lot of slack left and you should be able to speed it up by another 2 orders of magnitude. Try it 😄


Sum of Prime factorization isn't the same as some of all prime numbers sub N.  One will have a few dozen primes, other is on order of 100,000 primes

 

Certified-LabVIEW-Architect_rgb.jpgCertified_TestStand_Architect_rgb.jpg


"I won't be wronged. I won't be insulted. I won't be laid a-hand on. I don't do these things to other people, and I require the same from them." John Bernard Books

0 Kudos
Message 1504 of 2,571
(10,685 Views)

@bsvare wrote:

@altenbach wrote:

 

Even with arrays, the Mathscript node is way too slow. 

I dug up my old prime number generator from the prime factor challenge way back, and using it, I can find the same sum in under 10ms on my 8 year old laptop (no constant folding involved). 😄 There is definitely a lot of slack left and you should be able to speed it up by another 2 orders of magnitude. Try it 😄


Sum of Prime factorization isn't the same as some of all prime numbers sub N.  One will have a few dozen primes, other is on order of 100,000 primes

 


Part oy my old code was generating all prime factors below a certain limit, then I simply sum them. I am not doing any factorization for the problem discussed here. Yes, It generates all prime factors below 2M and sums them. I get the correct result. And yes, I could make it even faster by simplifying certain things that I have not done yet. Try it!

0 Kudos
Message 1505 of 2,571
(10,683 Views)

I'll be honest the first time I was asked to determine if a number was prime, I would have a for loop run, performing a mod on all values from n-1 to 2 looking to see if the remainder was 0.  For large numbers it took 10s of seconds to see if it was a prime number.  It seemed like the most logical way since it was by definition what a prime number was.  I thought of other improvements like end in 0 or 5, or was an even number, and only checking floor(n/2) to 2 but nothing comes close to the other examples posted.

0 Kudos
Message 1506 of 2,571
(10,677 Views)

@Hooovahh:

 

That, to me, is the crux of why I posted that original comment (and linked to it there), rather than to show off whatever methods I can come up with to calculate primes etc - if I'm using LV for that, I'm doing something wrong, much like I wouldn't choose to ride my road bike offroad when I've got a perfectly good mountain bike!

 

Anybody who writes a piece of code should be able to critically review it and think "can I do this better?". I posted a reasonably logical list of thoughts that popped into my head during my lunch break.

 

Finding the best solution the first time is both a) unlikely and b) not as ultimately fulfilling as finding improvements. By proposing an initial example and then refining it step by step, you'll learn a helluva lot. That's where I think Project Euler misses a trick - yes, getting the answer the first time is good, but refining your solution to be more efficient in speed/memory usage is a really useful skill.

 

It's all about improving yourself, after all 🙂

---
CLA
0 Kudos
Message 1507 of 2,571
(10,652 Views)

@altenbach wrote:

@thoult wrote:

Doesn't look too bad, right? Except it takes some 25 s to execute for a control value of 2,000,000. You can drastically reduce the time as soon as you realise that isprime(x) will accept array inputs.


Even with arrays, the Mathscript node is way too slow. 

 

I dug up my old prime number generator from the prime factor challenge way back, and using it, I can find the same sum in under 10ms on my 8 year old laptop (no constant folding involved). 😄 There is definitely a lot of slack left and you should be able to speed it up by another 2 orders of magnitude. Try it 😄


And how many iterations in development did it take you to get to that solution? 🙂

 

Will take a look when I get a chance next week. There's a lot of simplifying of the initial array that can be done for a start.

---
CLA
0 Kudos
Message 1508 of 2,571
(10,641 Views)

@thoult wrote:
And how many iterations in development did it take you to get to that solution? 🙂

Well, as I said, I already had the core prime code from 2005. It uses a  Sieve of Eratosthenes implementation. This is a very simple algorithm and does not require much development. Here's my draft.

 

It only takes a seconds or two to sum all primes under 100M, where the result is 279209790387276 (U64). In a few tens of seconds it solves 1G where the result is 24739512092254535.

 

(results are correct according to this table.  Go a little higher and you will soon run out of memory on 32bit LabVIEW).

 

See if you can improve it. 😄

 

The TRUE case is just wired across (array and boolean) and contains no other code. (The case structure could be omitted for simplicity. It only gives a marginal speedup (~10%), It helps skipping the inner calculation completely when it is no longer actually needed, i.e. if the FOR loop interations would be zero anyway).

 

It would be trivial to add an outer case so it returns zero for inputs < 3.

 

 

(I just added a few tweaks to make it faster. 'hope I did not break anything :D)

Download All
Message 1509 of 2,571
(10,616 Views)

@altenbach wrote:
In a few tens of seconds it solves 1G where the result is 24739512092254535.

Finally tested on a modern I7 and it finds and adds all primes below 1'000'000'000 (1G) in well under 10 seconds. 😄

Message 1510 of 2,571
(10,506 Views)