I'm working on an algorithm to calculate weird numbers, and to do so there are several properties that needs to be calculated, one of them being, if it is NOT a semi-perfect/pseudoperfect number.
My code can surely be done a lot more effectively because semi perfect numbers have very interesting properties. The first one being, every multiple of a semiperfect number is semiperfect. So for every semi perfect number calculated, the multiple of that number can be stored so it will not have to be recalculated.
What I don't know is if there is a way to calculate if a number is NOT a semi perfect number without having to calculate if it is. It really is ineffective to calculate if a number is semi perfect, just to know if it is NOT.
Imagine infinity, which has infinite valid divisors, to prove if it is a semi perfect number would take infinite amount of calculations, but would proving the opposite also take infinite amount of calculations?
Anyways here is my code (written in Java) which will return a boolean indicating whether or not a number is semi perfect:
/**
*
* @param x
* a number to test if it is semiperfect or not.
* @param list
* this is a array of the factors of x (e.g factors of 6 are 3, 2, 1 excluding itself).
* @return true if x is semiperfect, false otherwise.
*/
public static boolean isSemiPerfect(int x, List<Integer> list) {
if (x == 0)
return true;
for (int i = 0; i < list.size(); i++) {
int temp = list.remove(i);
if (isSemiPerfect(x - temp, list)) // using recursion
return true;
list.add(i, temp);
}
return false;
}
It seems like it is not possible to create a more effective algorithm to check if a number is NOT semi perfect, on the other hand, my current algorithm to check if a number is semi perfect or not can be improved.
I believe semi perfect numbers have some properties that would prevent the need to calculate every number, for instance. So far NO weird number has been discovered which is a odd number.
So unless the number in question (x) is above 1.8 * 1019, every odd number doesn't have to be checked if it is semi perfect or not, because every weird number below that is even.
Taking a look at this, a list of properties can be found, which can reduce the amount of numbers that needs to be checked.
I've realized that it will be hard to eliminate the calculation aspect completely, but can be reduced a lot. So I would like to have help with my algorithm to improve it, and perhaps implement a few of these properties to reduce the need to check every single number.
x
is anint
and so has a maximum value of ~2e9. If you want larger values, useBigInteger
. \$\endgroup\$