sorry to bother, but I have modified my program so that it displays the sum of the factors of all the numbers upto 1000. here it is acting really weird!

Code:

#include <iostream>using namespace std;

int main()
{

for ( int k = 2; k < 1000; k++)
{
int i=2;
int y=0;
int x;
x=k;

while(i<x)
{
if (x%i==0)
{
x=x/i;
y=y+i;

}
else
{i++;}
}

int z;
z=x+y;
cout<<z<<endl;

}
}

what is the wrong here??

01-13-2013

std10093

What do you mean? Please be more specific. It runned fine for me.

01-13-2013

Tamim Ad Dari

Quote:

Originally Posted by std10093

What do you mean? Please be more specific. It runned fine for me.

Well, I want this program to print the sum of factors of all numbers from 2 to 1000. such as,for k=2 it is2,k=3 it is 3,k=4 the factors are 2 and 2 and sum is 4,k=5 it is 5,k=12 factors are 3,2,2 and sum is 7. like this upto k=1000. the program surely didnt print these values.rather it prints 23,55..... which makes no sense!

01-13-2013

whiteflags

You said that once you find the prime factorization of one number, you would add all of them to another number which is the answer.I told you in a previous thread that this would only work if you divide by prime numbers. Well to do that you need to have a prime number test, the simplest of which is trial division: repeatedly divide the number you think is prime by smaller integers, and if you can find a number that divides without a remainder, then the number is composite. Or, you can make a list of primes. This is absolutely required if you want to do this right.

Once you know enough primes, you can start dividing to see which ones are in your numbers. The sum of the prime factors of 42 is 13, every factor appears only once. The sum of the prime factors of 16 is 8, though, so watch out for primes that appear more than once.

If you don't know enough math to do them I would suggest you steer clear of the Project Euler site.

01-14-2013

Tamim Ad Dari

Quote:

Originally Posted by whiteflags

You said that once you find the prime factorization of one number, you would add all of them to another number which is the answer.I told you in a previous thread that this would only work if you divide by prime numbers. Well to do that you need to have a prime number test, the simplest of which is trial division: repeatedly divide the number you think is prime by smaller integers, and if you can find a number that divides without a remainder, then the number is composite. Or, you can make a list of primes. This is absolutely required if you want to do this right.

Once you know enough primes, you can start dividing to see which ones are in your numbers. The sum of the prime factors of 42 is 13, every factor appears only once. The sum of the prime factors of 16 is 8, though, so watch out for primes that appear more than once.

If you don't know enough math to do them I would suggest you steer clear of the Project Euler site.

I certainly know how to find the prime factors of a number.In fact that is the way I used in the program where user gives an input and sum of the prime factors are outputted. there is no problem in that fraction. problem is, when I want to find the 'prime factor sum' of the all numbers from upto 1000 the output is different. the problem is in the "for loop" I have created later and I dont know what is causing the problem. So far I see, it should work fine...

01-14-2013

whiteflags

Quote:

I certainly know how to find the prime factors of a number.In fact that is the way I used in the program where user gives an input and sum of the prime factors are outputted. there is no problem in that fraction.

I disagree with you completely. As written, the computer will use any divisor, not just the primes.

Once again, if you make sure to divide by primes, the only other thing you need to do is process each number in turn. Add together each number's prime factors, using the same variable.

01-14-2013

Shakti

You have an error when computing the y factor. When you find an i that x is evenly dividable by you add this to y, but this is wrong. You need to multiply y with i and set your new y to this.

Also y should be 1 at the start.

01-14-2013

Tamim Ad Dari

Quote:

Originally Posted by Shakti

You have an error when computing the y factor. When you find an i that x is evenly dividable by you add this to y, but this is wrong. You need to multiply y with i and set your new y to this.

Also y should be 1 at the start.

I have done as you said,but yet it outs 75,62 which does not make any sense.