Proper divisors | Rosetta Code | #2
URL to the problem page: https://rosettacode.org/wiki/Proper_divisors
The proper divisors of a positive integer N are those numbers, other than N itself, that divide N without remainder.
For N > 1 they will always include 1, but for N == 1 there are no proper divisors.
Examples
The proper divisors of 6 are 1, 2, and 3.
The proper divisors of 100 are 1, 2, 4, 5, 10, 20, 25, and 50.
Find a number in the range 1 to 20,000 with the most proper divisors. Show the number and just the count of how many proper divisors it has.
CLICK TO SEE THE OUTPUT.
The proper divisors of a positive integer N are those numbers, other than N itself, that divide N without remainder.
For N > 1 they will always include 1, but for N == 1 there are no proper divisors.
Examples
The proper divisors of 6 are 1, 2, and 3.
The proper divisors of 100 are 1, 2, 4, 5, 10, 20, 25, and 50.
Find a number in the range 1 to 20,000 with the most proper divisors. Show the number and just the count of how many proper divisors it has.
#include <iostream>
using namespace std;
int main()
{
int counter, result = 0, tmp;
for (int i = 1; i < 20000; i++) {
counter = 0;
for (int j = 1; j < i; j++) {
if (i % j == 0) {
counter++;
}
}
if (counter > result) {
result = counter;
tmp = i;
}
}
cout << "Number: " << tmp << endl << "Divisors: " << result << endl;
return 0;
}
CLICK TO SEE THE OUTPUT.
Comments
Post a Comment