Correct function count_divisors
This commit is contained in:
parent
95611e4401
commit
dd628af770
@ -143,7 +143,7 @@ int *sieve(int n)
|
||||
* there is no need check i>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.*/
|
||||
if(primes[i])
|
||||
@ -166,19 +166,23 @@ int count_divisors(int n)
|
||||
{
|
||||
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));
|
||||
|
||||
for(i = 1; i < limit; i++)
|
||||
for(i = 1; i <= limit; i++)
|
||||
{
|
||||
if(n % i == 0)
|
||||
{
|
||||
count += 2;
|
||||
}
|
||||
|
||||
if(n == limit * limit)
|
||||
{
|
||||
count--;
|
||||
}
|
||||
}
|
||||
|
||||
if(n == limit * limit)
|
||||
{
|
||||
count--;
|
||||
}
|
||||
|
||||
return count;
|
||||
|
@ -19,6 +19,21 @@ def is_prime(num):
|
||||
|
||||
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):
|
||||
return a * b // gcd(a, b)
|
||||
|
||||
@ -60,26 +75,11 @@ def count_divisors(n):
|
||||
if n % i == 0:
|
||||
count = count + 2
|
||||
|
||||
if n == limit * limit:
|
||||
count = count - 1
|
||||
if n == limit * limit:
|
||||
count = count - 1
|
||||
|
||||
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):
|
||||
i = 0
|
||||
digits = zeros(n + 1, int)
|
||||
|
Loading…
x
Reference in New Issue
Block a user