answersLogoWhite

0

A perfect number is a positive integer that is equal to the sum of its positive divisors other than itself. Thus 6 is a perfect number because it has divisors 1, 2 and 3, the sum of which is 6.

There are actually very few perfect numbers in any given range, so rather than waste precious CPU cycles determining the divisors of a number and then summing them to see if it is perfect or not (most of the time it won't be) it is much easier and quicker to simply store them in a static array.

A 64-bit unsigned integer can store values in the range 0 to 18,446,744,073,709,551,615, but there are only 8 perfect numbers in this range. Thus the following array will suffice:

unsigned long long perfect = {6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128 };

To determine if a given positive value is perfect or not, simply test to see if it exists in this array. The following function shows how this can be achieved.

bool is_perfect(const unsigned long long value)

{

const unsigned long long perfect[8] = {6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128};

for(unsigned i=0; i<8; ++i)

if(value==perfect[i])

return true;

return false;

}

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

How do you find the inverse log using a TI 84 plus?

The 2nd function of the log button is the inverse log. Press 2nd log, which displays 10^( on the screen. Put a number after the opening parenthesis.


Is y plus x2 plus 1 a function?

That is not a function, although it does involve the function of addition. A function is something that is done to numbers.


A c plus plus statement that invokes a function is known as?

...a function call.


What are the building function in c plus plus?

There is no such term as "building function" in C++.


How do you write a C plus plus program that displays a pyramid of Xes on the screen using a for loop?

printf ("x")


In C plus plus when a function is executing what happens when the end of the function is reached?

Control is returning to the caller of the function.


When will you make a function inline in c plus plus?

yes,we can make function inline


Is x2 plus 32x plus 64 a perfect-square trinomial.?

The given quadratic expression can not be factored as a perfect square.


What means mass number plus atomic number?

There is no known function for the mass number and atomic number. However, the difference between these two values gives the number of neutrons present in that atom.


Is x2 plus 5x a function?

No. x2+5x is a polynomial, an algebraic expression or a formula, but it is not a function. It could be used to help define a function. {(x,y) | y = x2 + 5x , x any real number} is a function


What is a main function in c plus plus?

It is the first function that gets called when the program is executed.


What is a primary function in C plus plus?

There is no such thing. You probably meant the main function. The main function is the only function that is required as it serves as the entry point of the program.