Number Theory Archives - Time Travel, Quantum Entanglement and Quantum Computing https://stationarystates.com/category/number-theory/ Not only is the Universe stranger than we think, it is stranger than we can think...Hiesenberg Sun, 14 Mar 2021 00:45:37 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.3 Perfect Numbers – and a best case algorithm https://stationarystates.com/number-theory/perfect-numbers-and-a-best-case-algorithm/?utm_source=rss&utm_medium=rss&utm_campaign=perfect-numbers-and-a-best-case-algorithm Sun, 07 Mar 2021 16:04:00 +0000 http://stationarystates.com/?p=139 Brute Force Algorithm – O (N) The brute force approach will loop through all the way from 1 to N – looking for divisors and add them to a running […]

The post Perfect Numbers – and a best case algorithm appeared first on Time Travel, Quantum Entanglement and Quantum Computing.

]]>
Brute Force Algorithm – O (N)

The brute force approach will loop through all the way from 1 to N – looking for divisors and add them to a running sum.

public boolean checkPerfectNumber(int num) {
if (num <= 0) {
return false;
}
int sum = 0;
for (int i = 1; i * i <= num; i++) {
if (num % i == 0) {
sum += i;
if (i * i != num) {
sum += num / i;
}

}
}
return sum - num == num;
}
}

Only Loop till SqRt (N) – O(sqrt N)

There is no need to loop till N. Any divisor will need to be less than or equal to sqrt N. So – shorten the loop to only go till sqrt N.

The post Perfect Numbers – and a best case algorithm appeared first on Time Travel, Quantum Entanglement and Quantum Computing.

]]>