Skip to content
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

Weird and Semi-perfect numbers #18128

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions sympy/ntheory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from .generate import nextprime, prevprime, prime, primepi, primerange, \
randprime, Sieve, sieve, primorial, cycle_length, composite, compositepi
from .primetest import isprime

from .factor_ import divisors, proper_divisors, factorint, multiplicity, \
perfect_power, pollard_pm1, pollard_rho, primefactors, totient, trailing, \
divisor_count, proper_divisor_count, divisor_sigma, factorrat, \
reduced_totient, primenu, primeomega, mersenne_prime_exponent, \
is_perfect, is_mersenne_prime, is_abundant, is_deficient, is_amicable, \
abundance
abundance, is_semiperfect, is_weird

from .partitions_ import npartitions
from .residue_ntheory import is_primitive_root, is_quad_residue, \
legendre_symbol, jacobi_symbol, n_order, sqrt_mod, quadratic_residues, \
Expand All @@ -29,12 +31,14 @@

'isprime',


'divisors', 'proper_divisors', 'factorint', 'multiplicity', 'perfect_power',
'pollard_pm1', 'pollard_rho', 'primefactors', 'totient', 'trailing',
'divisor_count', 'proper_divisor_count', 'divisor_sigma', 'factorrat',
'reduced_totient', 'primenu', 'primeomega', 'mersenne_prime_exponent',
'is_perfect', 'is_mersenne_prime', 'is_abundant', 'is_deficient', 'is_amicable',
'abundance',
'abundance', 'is_semiperfect', 'is_weird',


'npartitions',

Expand Down
61 changes: 61 additions & 0 deletions sympy/ntheory/factor_.py
Original file line number Diff line number Diff line change
Expand Up @@ -2371,3 +2371,64 @@ def is_amicable(m, n):
return False
a, b = map(lambda i: divisor_sigma(i), (m, n))
return a == b == (m + n)


def is_semiperfect(n):
"""Returns True if the number 'n' is "semi-perfect", else returns False
Semiperfect number is a natural number that is equal to the sum of all
or some of its proper devisors
Examples
========

>>> from sympy.ntheory.factor_ import is_semiperfect
>>> is_semiperfect(40)
True
>>> is_semiperfect(38)
False

Reference
=========
.. [1] https://en.wikipedia.org/wiki/Semiperfect_number
"""

factors = divisors(n)
factors = factors[:-1]
num_factors = len(factors)

subset = [[0 for i in range(n + 1)]for j in range(num_factors + 1)]
for i in range(num_factors + 1):
subset[i][0]= True
for i in range(1, n + 1):
subset[0][i] = False

for i in range(1, num_factors + 1):
for j in range(1, n + 1):
if j < factors[i - 1]:
subset[i][j] = subset[i - 1][j]
else:
subset[i][j] = subset[i - 1][j] or subset[i - 1][j - factors[i - 1]]
if subset[num_factors][n] == 0:
return False
return True


def is_weird(n):
"""Returns True if the number 'n' is "weird", else returns False
Weird number is a natural number taht is abundant but not semiperfect.
Examples
========

>>> from sympy.ntheory.factor_ import is_weird
>>> is_weird(70)
True
>>> is_weird(50)
False

Reference
=========
.. [1] https://en.wikipedia.org/wiki/Weird_number
"""

if is_abundant(n) == True and is_semiperfect(n) == False:
return True
return False
Comment on lines +2432 to +2434
Copy link
Member

@czgdp1807 czgdp1807 Feb 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be transformed to a one liner, return is_abundant(n) == True and is_semiperfect(n) == False.
IMO, this function simply uses two pre-defined functions without much manipulations, so we can remove is_weird.

14 changes: 13 additions & 1 deletion sympy/ntheory/tests/test_factor_.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
antidivisors, antidivisor_count, core, digits, udivisors, udivisor_sigma,
udivisor_count, proper_divisor_count, primenu, primeomega, small_trailing,
mersenne_prime_exponent, is_perfect, is_mersenne_prime, is_abundant,
is_deficient, is_amicable)
is_deficient, is_amicable, is_semiperfect, is_weird)
from sympy.ntheory.generate import cycle_length
from sympy.ntheory.multinomial import (
multinomial_coefficients, multinomial_coefficients_iterator)
Expand Down Expand Up @@ -658,3 +658,15 @@ def test_is_amicable():
assert is_amicable(173, 129) is False
assert is_amicable(220, 284) is True
assert is_amicable(8756, 8756) is False


def test_is_semiperfect():
assert is_semiperfect(260) is True
assert is_semiperfect(38) is False
assert is_semiperfect(70) is False


def test_is_weird():
assert is_weird(13930) is True
assert is_weird(7192) is True
assert is_weird(100) is False