Correct function count_divisors

This commit is contained in:
daniele 2019-09-22 21:12:58 +02:00
parent 95611e4401
commit dd628af770
Signed by: fuxino
GPG Key ID: 6FE25B4A3EE16FDA
2 changed files with 28 additions and 24 deletions

View File

@ -143,7 +143,7 @@ int *sieve(int n)
* there is no need check i>sqrt(n).*/ * there is no need check i>sqrt(n).*/
limit = floor(sqrt(n)); limit = floor(sqrt(n));
for(i = 3; i < limit; i += 2) for(i = 3; i <= limit; i += 2)
{ {
/* Find the next number not crossed out, which is prime.*/ /* Find the next number not crossed out, which is prime.*/
if(primes[i]) if(primes[i])
@ -166,20 +166,24 @@ int count_divisors(int n)
{ {
int i, limit, count = 0; int i, limit, count = 0;
/* For every divisor below the square root of n, there is a corresponding one
* above the square root, so it's sufficient to check up to the square root of n
* and count every divisor twice. If n is a perfect square, the last divisor is
* wrongly counted twice and must be corrected.*/
limit = floor(sqrt(n)); limit = floor(sqrt(n));
for(i = 1; i < limit; i++) for(i = 1; i <= limit; i++)
{ {
if(n % i == 0) if(n % i == 0)
{ {
count += 2; count += 2;
} }
}
if(n == limit * limit) if(n == limit * limit)
{ {
count--; count--;
} }
}
return count; return count;
} }

View File

@ -19,6 +19,21 @@ def is_prime(num):
return True return True
def is_palindrome(num, base):
reverse = 0
tmp = num
while tmp > 0:
reverse = reverse * base
reverse = reverse + tmp % base
tmp = tmp // base
if num == reverse:
return True
return False
def lcm(a, b): def lcm(a, b):
return a * b // gcd(a, b) return a * b // gcd(a, b)
@ -65,21 +80,6 @@ def count_divisors(n):
return count return count
def is_palindrome(num, base):
reverse = 0
tmp = num
while tmp > 0:
reverse = reverse * base
reverse = reverse + tmp % base
tmp = tmp // base
if num == reverse:
return True
return False
def is_pandigital(value, n): def is_pandigital(value, n):
i = 0 i = 0
digits = zeros(n + 1, int) digits = zeros(n + 1, int)