-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve id0021 and id0023 using new libraries
- Loading branch information
1 parent
f570ce3
commit 3d3d015
Showing
13 changed files
with
208 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Licensed under the MIT License. | ||
|
||
#include <math.h> | ||
#include "factor_iterator.h" | ||
|
||
void factor_begin(FactorIterator iterator, long long n, Sieve primes) | ||
{ | ||
iterator->remainder = n; | ||
|
||
sieve_begin(&iterator->iterator, primes); | ||
factor_next(iterator); | ||
} | ||
|
||
bool factor_end(FactorIterator iterator) | ||
{ | ||
return !iterator->remainder; | ||
} | ||
|
||
void factor_next(FactorIterator iterator) | ||
{ | ||
if (iterator->remainder == 1) | ||
{ | ||
iterator->current = *iterator->iterator.current; | ||
iterator->remainder = 0; | ||
iterator->currentCount = 1; | ||
|
||
return; | ||
} | ||
|
||
while (iterator->remainder % *iterator->iterator.current != 0) | ||
{ | ||
sieve_next(&iterator->iterator); | ||
} | ||
|
||
iterator->current = *iterator->iterator.current; | ||
iterator->remainder /= iterator->current; | ||
iterator->currentCount = 1; | ||
|
||
while (iterator->remainder % iterator->current == 0) | ||
{ | ||
iterator->remainder /= iterator->current; | ||
iterator->currentCount++; | ||
} | ||
} | ||
|
||
int factor_divisor_count(long long n, Sieve primes) | ||
{ | ||
int result = 1; | ||
struct FactorIterator it; | ||
|
||
for (factor_begin(&it, n, primes); !factor_end(&it); factor_next(&it)) | ||
{ | ||
result *= it.currentCount + 1; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
int factor_divisor_sum(long long n, Sieve primes) | ||
{ | ||
int result = 1; | ||
struct FactorIterator it; | ||
|
||
for (factor_begin(&it, n, primes); !factor_end(&it); factor_next(&it)) | ||
{ | ||
result *= (pow(it.current, it.currentCount + 1) - 1) / (it.current - 1); | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Licensed under the MIT License. | ||
|
||
#include "sieve_iterator.h" | ||
|
||
/** Iterates over the prime factors of a positive integer. */ | ||
struct FactorIterator | ||
{ | ||
struct SieveIterator iterator; | ||
long long current; | ||
long long remainder; | ||
int currentCount; | ||
}; | ||
|
||
/** Iterates over the prime factors of a positive integer. */ | ||
typedef struct FactorIterator* FactorIterator; | ||
|
||
/** | ||
* Provides an iterator over the prime factors of `n`. | ||
* | ||
* @param iterator the iterator. | ||
* @param n the number whose factors to enumerate. | ||
* @param primes the prime sequence. | ||
*/ | ||
void factor_begin(FactorIterator iterator, long long n, Sieve primes); | ||
|
||
/** | ||
* Returns a value indicating whether the iterator can advance to the next prime | ||
* factor. | ||
* | ||
* @param iterator the iterator. | ||
* @return `true` if the iterator can successfully advance to the next factor; | ||
* `false` if there are no more factors. | ||
*/ | ||
bool factor_end(FactorIterator iterator); | ||
|
||
/** | ||
* Advances the iterator to the next prime factor. | ||
* | ||
* @param iterator the iterator. | ||
*/ | ||
void factor_next(FactorIterator iterator); | ||
|
||
/** | ||
* Returns the number of divisors of a positive integer. | ||
* | ||
* @param n the number whose divisors to count. | ||
* @param primes the prime sequence. | ||
* @return The number of divisors of `n`. | ||
*/ | ||
int factor_divisor_count(long long n, Sieve primes); | ||
|
||
/** | ||
* Computes the sum of the divisors of a positive integer. | ||
* | ||
* @param n the number whose divisors to sum. | ||
* @param primes the prime sequence. | ||
* @return The sum of the divisors of `n`. | ||
*/ | ||
int factor_divisor_sum(long long n, Sieve primes); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.