05-09-2014 06:33 AM
Hi friends, I need help doing the problems below in LabVIEW. I need a code that is quick and efficient because my code is simply consuming too much time. I want the code without using any inbuilt LabVIEW VIs such as the prime factor.vi.
1) Find the greatest product of 6 consecutive digits in the first 1000 decimal digits of Euler's number e.
2) By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number?
3) The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million.
4) 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
Thanks
05-09-2014 08:07 AM
These are always pretty cool homework problems.
What have you tried so far?
Post your code and you'll get comments about how to improve it.
05-09-2014 08:14 AM
Yeah, let us know what you've tried.
I've done these problems myself when I first stated with LabVIEW so I can point you in the right direction
-Jim B.
05-09-2014 08:43 AM
Hi-
No point reinventing the wheel- please post the working code you mentioned and we can point out how to make it more efficient.
05-09-2014 08:58 AM
Ok I am posting the code I did for questions 2,3 & 4. I don't know how to solve question 1. I did'nt implement any formulas in the code since I don't know, which is why the code is taking too much time. Also I haven't put wait ms function since my code was already too slow. I am posting these solutions to convince you guys that I have tried it and it is not the best solution, which is why I am asking for help. Also please don't comment on style guidelines because that is not the intend of posting these solution of mine.
I was able to solve a few other questions in Project Euler, but for these 4 questions I am unable to proceed.
Thanks
05-09-2014 09:31 AM - edited 05-09-2014 09:32 AM
Hi,
So I looked at your problem 2 (Euler Problem 7) and 3 (Euler Problem 10) and number 2 seems to work fine. It runs in 8 seconds on my computer which shouldn't be a problem.
Problem 3 does take some time because you are currently iterating over every number from 0 to 2,000,000, and checking if it's prime by dividing it by every number less than it.
Some improvements:
-One thing to remember with primes is when checking if a number n is prime, you only have to check from 2 up to SQRT(n). This makes sense when you think about it because if a number is not divisible by any number below its square root, it won't be divible by any number greater than it either.
-The way I did this problem is I built an array of primes and checked the next numbers by iterating through the array of primes. This works because every number can be broken down into prime factors so if a number is not divisible by any primes less than it, it is prime itself.
05-09-2014 09:50 AM
And for your problem 4, you just need to take the prime factorization of all numbers less than 20, combine them where possible, and multiply them together.
For instance, if we want the least common multiple of 6, 8, and 10 we would break them down:
6 = 2^1 * 3^1 8 = 2^3 10 = 2^1 * 3^0 * 5^1 Then we combine and get: 2^3 * 3^1 * 5^1 = 120. So 120 is the smallest number divisible by 6, 8, and 10.
I can't find my LabVIEW code that does it, but that's how you do it.
05-09-2014 09:58 AM
Thanks James-B for your valuable time. I will try questions 3 & 4 again keeping your suggestions in mind. Do you know how to solve question 1? I am absolutely clueless.
05-09-2014 10:18 AM - edited 05-09-2014 10:18 AM
@Mithun_770 wrote:
Do you know how to solve question 1? I am absolutely clueless.
First you need to find the first 1000 decimal places of e. I would recommend putting them into a string and then make a program to put the digits from the string into an array. Now you just have to go through each subset of 6 digits and multiply them. If the product is greater than the previous greatest product, store it.
05-09-2014 10:19 AM
Does anyone know how to solve question 1 ? Just need the idea on how to implement it. I thought I could use the euler constant in LabVIEW but it doesn't have 1000 decimal digits. What can I do to solve it?