Euler Problem5

Hi,

I am trying to solve Problem 5 in Project Euler website. There is a very bad looking way to code it with too many nested loops. I found a solution online but can anyone please explain the way and approach to do it. I am getting confused by the code that I found.

Below is the code I found:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int findProduct( int );

int findProduct(int maxFactor )
{
	for ( int n = maxFactor; ; n += maxFactor )
	{
		for ( int i = 2; i <= maxFactor; i++ )
		{
			if ( n % i > 0 ) break;
			else if ( i == maxFactor ) return n;
		}
	}
}

int main()
{
	std::cout<<findProduct(20);
	return 0;
}
closed account (zwA4jE8b)
set a breakpoint on line 9 and step through it if you can.

the outer for loop increments n each iteration by maxFactor, which is what you send it, in this case 20.
the inner for loop

start
i = 2
n = 20

n%i not greater than 0.

next,

i = 3
n = 20

n%i is greater than 0

break, return to outer loop.

n += 20

return to inner loop

i = 2
n = 40

n%i not greater than 0

next

i = 3
n = 40

n%i is greater than 0

break

repeat until i == '20'
closed account (D80DSL3A)
Those problems are not to be "solved" by looking up another persons solution.
They are meant to test your own programming/problem solving skills.
Solve it yourself or choose an easier problem.
Last edited on
Okay.like I said I did that but my solution had a lot of nested loops. and i could not figure it out so tried looking online. I wanted to get the explanantion. Can you provide one?
closed account (D80DSL3A)
Think about the factors which such a number would have.
answer = 2*3*2*5*7*2*3*11*13*2*17*19
Quite a nice problem. I'd like to make it a bit more interesting if anyone wants a go.

Let F(k) be the lowest common multiple of all the natural numbers <= k. Original problem is calculate F(20)

Problem inputs are double precision l, u.

Find U - L where U =largest x such that ln (F(x)) < u and L = smallest x such that ln(F(x)) > l

l and u are between 10 and 50000
Topic archived. No new replies allowed.