1- A positive number N is called as perfect if the sum of its divisors is equal to N. For example, 28 is called as perfect since the sum of its divisors 1, 2, 4, 7, and 14 is equal to 28. Write a C++ program that reads any positive integer and checks whether the number is perfect or not.
2- Display all the numbers between 2 and 10000 that are called perfect.
1
Expert's answer
2012-11-06T10:30:01-0500
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
&
bool GenerateFactors(int n)
{
int sum = 0;
std::vector<int> factors;
factors.push_back(1);
factors.push_back(n);
for(int i = 2; i * i <= n; ++i)
{
if(n % i == 0)
{
& sum+=i;
& factors.push_back(i);
& if(i * i != n)
& factors.push_back(n / i);
& sum+=n/i;
}
}
if (sum+1 == n){
return true;
} else
return false;
}
&
int main()
{
const int SampleNumbers[] = {28, 45, 60, 81};
&
for(size_t i = 2; i < 10000; ++i)
{
if (GenerateFactors(i)) {
& std::cout << i << "\n";
}
}
system("pause");
}
& where [HA]=[A] pH of a buffer solution will be equal to pKa.
Comments
Leave a comment