PDA

View Full Version : Simple C++ Help (reps)



Weader
02-01-2012, 09:22 PM
Sooo I need to make a simple program that counts the number (d) of variables that any positive integer (n) has in the range of [2, 2147483647] using only iostream and namespace std.

I know that n % d ==0 means that a divisor has been found, but how do I continue to find all positive divisors of the number? Would I continue to divide? I'm sort of lost, will rep for help.

voxel
02-01-2012, 09:53 PM
Sooo I need to make a simple program that counts the number (d) of variables that any positive integer (n) has in the range of [2, 2147483647] using only iostream and namespace std.

I know that n % d ==0 means that a divisor has been found, but how do I continue to find all positive divisors of the number? Would I continue to divide? I'm sort of lost, will rep for help.Just keep a tally of the ones you find.

A naive way would be:


uint32_t tally = 2; // Count 1 and self.

for(uint32_t i = 2; i <= input / 2; ++i)
{
if(!(input % i))
{
++tally;
}
}

supafreak22
02-02-2012, 06:29 AM
OP are you taking CMPSC 121?

Weader
02-02-2012, 09:21 AM
OP are you taking CMPSC 121?
haha why do you ask brah?