-
Notifications
You must be signed in to change notification settings - Fork 262
Compute the sum of powers and the sum of divisors of an integer #2030
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MGessinger
wants to merge
9
commits into
flintlib:main
Choose a base branch
from
MGessinger:aliquot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e0751f1
Implement summation of powers of an integer
a4d747b
Add test for sum of powers
36be38c
Fix sum_powers after testing
48ef3d3
Add fmpz_sum_divisors and tests
0f8c263
Add documentation
7d627bb
Add example program to compute aliquot sequence
639dfd9
Fix formatting
92c40ab
Style fixes
dd77483
Fix formatting and check example
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,47 @@ | ||
/* | ||
Copyright (C) 2024 Matthias Gessinger | ||
|
||
This file is part of FLINT. | ||
|
||
FLINT is free software: you can redistribute it and/or modify it under | ||
the terms of the GNU Lesser General Public License (LGPL) as published | ||
by the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. See <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <flint/fmpz.h> | ||
#include <flint/profiler.h> | ||
|
||
int | ||
main(int argc, char * argv[]) | ||
{ | ||
fmpz_t n; | ||
ulong num_iterations = 0; | ||
|
||
fmpz_init_set_si(n, 138); | ||
|
||
flint_printf("Computing aliquot sequence\n"); | ||
flint_printf("%3wu : ", 0); | ||
fmpz_print(n); | ||
flint_printf("\n"); | ||
|
||
TIMEIT_ONCE_START | ||
|
||
while (!fmpz_is_one(n)) | ||
{ | ||
fmpz_sum_divisors_proper(n, n); | ||
num_iterations++; | ||
|
||
flint_printf("%3wu : ", num_iterations); | ||
fmpz_print(n); | ||
flint_printf("\n"); | ||
} | ||
|
||
flint_printf("Sequence terminated after %wu iterations.\n", num_iterations); | ||
|
||
TIMEIT_ONCE_STOP | ||
SHOW_MEMORY_USAGE | ||
|
||
fmpz_clear(n); | ||
return 0; | ||
} |
This file contains hidden or 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 hidden or 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,75 @@ | ||
/* | ||
Copyright (C) 2024 Matthias Gessinger | ||
|
||
This file is part of FLINT. | ||
|
||
FLINT is free software: you can redistribute it and/or modify it under | ||
the terms of the GNU Lesser General Public License (LGPL) as published | ||
by the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. See <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "fmpz.h" | ||
#include "fmpz_factor.h" | ||
|
||
void | ||
fmpz_sum_divisors(fmpz_t f, const fmpz_t g) | ||
{ | ||
fmpz_factor_t factors; | ||
fmpz * temp; | ||
ulong exp; | ||
slong i; | ||
|
||
fmpz * p; | ||
|
||
if (fmpz_is_zero(g) || fmpz_is_pm1(g)) | ||
{ | ||
fmpz_set(f, g); | ||
} | ||
else | ||
{ | ||
fmpz_factor_init(factors); | ||
|
||
fmpz_factor(factors, g); | ||
i = 0; | ||
|
||
temp = factors->p + i; | ||
exp = factors->exp[i]; | ||
|
||
fmpz_sum_powers(temp, temp, exp); | ||
|
||
for (i = 1; i < factors->num; i++) | ||
{ | ||
p = factors->p + i; | ||
exp = factors->exp[i]; | ||
|
||
fmpz_sum_powers(p, p, exp); | ||
|
||
fmpz_mul(temp, temp, p); | ||
} | ||
|
||
if (factors->sign == -1) | ||
{ | ||
fmpz_neg(f, temp); | ||
} | ||
else | ||
{ | ||
fmpz_swap(f, temp); | ||
} | ||
|
||
fmpz_factor_clear(factors); | ||
} | ||
} | ||
|
||
void | ||
fmpz_sum_divisors_proper(fmpz_t f, const fmpz_t g) | ||
{ | ||
fmpz_t temp; | ||
fmpz_init(temp); | ||
|
||
fmpz_sum_divisors(temp, g); | ||
|
||
fmpz_sub(f, temp, g); | ||
|
||
fmpz_clear(temp); | ||
} |
This file contains hidden or 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,92 @@ | ||
/* | ||
Copyright (C) 2024 Matthias Gessinger | ||
|
||
This file is part of FLINT. | ||
|
||
FLINT is free software: you can redistribute it and/or modify it under | ||
the terms of the GNU Lesser General Public License (LGPL) as published | ||
by the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. See <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "fmpz.h" | ||
|
||
void | ||
fmpz_sum_powers_horner(fmpz_t f, const fmpz_t g, ulong exp) | ||
{ | ||
fmpz_t t; | ||
|
||
if (exp == 0 || fmpz_is_zero(g)) | ||
{ | ||
fmpz_one(f); | ||
} | ||
else if (fmpz_is_one(g)) | ||
{ | ||
fmpz_set_ui(f, exp + 1); | ||
} | ||
else | ||
{ | ||
fmpz_init_set_ui(t, 1); | ||
|
||
for (ulong i = 1; i <= exp; i++) | ||
{ | ||
fmpz_mul(t, t, g); | ||
fmpz_add_ui(t, t, 1); | ||
} | ||
|
||
fmpz_swap(f, t); | ||
fmpz_clear(t); | ||
} | ||
} | ||
|
||
void | ||
fmpz_sum_powers_div(fmpz_t f, const fmpz_t g, ulong exp) | ||
{ | ||
fmpz_t t; | ||
|
||
if (exp == 0 || fmpz_is_zero(g)) | ||
{ | ||
fmpz_one(f); | ||
} | ||
else if (fmpz_is_one(g)) | ||
{ | ||
fmpz_set_ui(f, exp + 1); | ||
} | ||
else | ||
{ | ||
fmpz_init(t); | ||
|
||
/* t = g - 1 */ | ||
fmpz_sub_ui(t, g, 1); | ||
|
||
/* f = g^(e + 1) - 1 */ | ||
fmpz_pow_ui(f, g, exp + 1); | ||
fmpz_sub_ui(f, f, 1); | ||
|
||
/* f = (g^(e + 1) - 1) / (g - 1) */ | ||
fmpz_tdiv_q(f, f, t); | ||
|
||
fmpz_clear(t); | ||
} | ||
} | ||
|
||
void | ||
fmpz_sum_powers(fmpz_t f, const fmpz_t g, ulong exp) | ||
{ | ||
if (exp == 0 || fmpz_is_zero(g)) | ||
{ | ||
fmpz_one(f); | ||
} | ||
else if (exp == 1) | ||
{ | ||
fmpz_add_ui(f, g, 1); | ||
} | ||
else if (exp <= 100) | ||
{ | ||
fmpz_sum_powers_horner(f, g, exp); | ||
} | ||
else | ||
{ | ||
fmpz_sum_powers_div(f, g, exp); | ||
} | ||
} |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.