mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Move files for special numbers to own directory (#10714)
This commit is contained in:
parent
197604898b
commit
6f2d6f72d5
|
@ -1,98 +1,98 @@
|
||||||
"""
|
"""
|
||||||
An Armstrong number is equal to the sum of its own digits each raised to the
|
An Armstrong number is equal to the sum of its own digits each raised to the
|
||||||
power of the number of digits.
|
power of the number of digits.
|
||||||
|
|
||||||
For example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370.
|
For example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370.
|
||||||
|
|
||||||
Armstrong numbers are also called Narcissistic numbers and Pluperfect numbers.
|
Armstrong numbers are also called Narcissistic numbers and Pluperfect numbers.
|
||||||
|
|
||||||
On-Line Encyclopedia of Integer Sequences entry: https://oeis.org/A005188
|
On-Line Encyclopedia of Integer Sequences entry: https://oeis.org/A005188
|
||||||
"""
|
"""
|
||||||
PASSING = (1, 153, 370, 371, 1634, 24678051, 115132219018763992565095597973971522401)
|
PASSING = (1, 153, 370, 371, 1634, 24678051, 115132219018763992565095597973971522401)
|
||||||
FAILING: tuple = (-153, -1, 0, 1.2, 200, "A", [], {}, None)
|
FAILING: tuple = (-153, -1, 0, 1.2, 200, "A", [], {}, None)
|
||||||
|
|
||||||
|
|
||||||
def armstrong_number(n: int) -> bool:
|
def armstrong_number(n: int) -> bool:
|
||||||
"""
|
"""
|
||||||
Return True if n is an Armstrong number or False if it is not.
|
Return True if n is an Armstrong number or False if it is not.
|
||||||
|
|
||||||
>>> all(armstrong_number(n) for n in PASSING)
|
>>> all(armstrong_number(n) for n in PASSING)
|
||||||
True
|
True
|
||||||
>>> any(armstrong_number(n) for n in FAILING)
|
>>> any(armstrong_number(n) for n in FAILING)
|
||||||
False
|
False
|
||||||
"""
|
"""
|
||||||
if not isinstance(n, int) or n < 1:
|
if not isinstance(n, int) or n < 1:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Initialization of sum and number of digits.
|
# Initialization of sum and number of digits.
|
||||||
total = 0
|
total = 0
|
||||||
number_of_digits = 0
|
number_of_digits = 0
|
||||||
temp = n
|
temp = n
|
||||||
# Calculation of digits of the number
|
# Calculation of digits of the number
|
||||||
number_of_digits = len(str(n))
|
number_of_digits = len(str(n))
|
||||||
# Dividing number into separate digits and find Armstrong number
|
# Dividing number into separate digits and find Armstrong number
|
||||||
temp = n
|
temp = n
|
||||||
while temp > 0:
|
while temp > 0:
|
||||||
rem = temp % 10
|
rem = temp % 10
|
||||||
total += rem**number_of_digits
|
total += rem**number_of_digits
|
||||||
temp //= 10
|
temp //= 10
|
||||||
return n == total
|
return n == total
|
||||||
|
|
||||||
|
|
||||||
def pluperfect_number(n: int) -> bool:
|
def pluperfect_number(n: int) -> bool:
|
||||||
"""Return True if n is a pluperfect number or False if it is not
|
"""Return True if n is a pluperfect number or False if it is not
|
||||||
|
|
||||||
>>> all(armstrong_number(n) for n in PASSING)
|
>>> all(armstrong_number(n) for n in PASSING)
|
||||||
True
|
True
|
||||||
>>> any(armstrong_number(n) for n in FAILING)
|
>>> any(armstrong_number(n) for n in FAILING)
|
||||||
False
|
False
|
||||||
"""
|
"""
|
||||||
if not isinstance(n, int) or n < 1:
|
if not isinstance(n, int) or n < 1:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Init a "histogram" of the digits
|
# Init a "histogram" of the digits
|
||||||
digit_histogram = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
digit_histogram = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||||
digit_total = 0
|
digit_total = 0
|
||||||
total = 0
|
total = 0
|
||||||
temp = n
|
temp = n
|
||||||
while temp > 0:
|
while temp > 0:
|
||||||
temp, rem = divmod(temp, 10)
|
temp, rem = divmod(temp, 10)
|
||||||
digit_histogram[rem] += 1
|
digit_histogram[rem] += 1
|
||||||
digit_total += 1
|
digit_total += 1
|
||||||
|
|
||||||
for cnt, i in zip(digit_histogram, range(len(digit_histogram))):
|
for cnt, i in zip(digit_histogram, range(len(digit_histogram))):
|
||||||
total += cnt * i**digit_total
|
total += cnt * i**digit_total
|
||||||
|
|
||||||
return n == total
|
return n == total
|
||||||
|
|
||||||
|
|
||||||
def narcissistic_number(n: int) -> bool:
|
def narcissistic_number(n: int) -> bool:
|
||||||
"""Return True if n is a narcissistic number or False if it is not.
|
"""Return True if n is a narcissistic number or False if it is not.
|
||||||
|
|
||||||
>>> all(armstrong_number(n) for n in PASSING)
|
>>> all(armstrong_number(n) for n in PASSING)
|
||||||
True
|
True
|
||||||
>>> any(armstrong_number(n) for n in FAILING)
|
>>> any(armstrong_number(n) for n in FAILING)
|
||||||
False
|
False
|
||||||
"""
|
"""
|
||||||
if not isinstance(n, int) or n < 1:
|
if not isinstance(n, int) or n < 1:
|
||||||
return False
|
return False
|
||||||
expo = len(str(n)) # the power that all digits will be raised to
|
expo = len(str(n)) # the power that all digits will be raised to
|
||||||
# check if sum of each digit multiplied expo times is equal to number
|
# check if sum of each digit multiplied expo times is equal to number
|
||||||
return n == sum(int(i) ** expo for i in str(n))
|
return n == sum(int(i) ** expo for i in str(n))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""
|
"""
|
||||||
Request that user input an integer and tell them if it is Armstrong number.
|
Request that user input an integer and tell them if it is Armstrong number.
|
||||||
"""
|
"""
|
||||||
num = int(input("Enter an integer to see if it is an Armstrong number: ").strip())
|
num = int(input("Enter an integer to see if it is an Armstrong number: ").strip())
|
||||||
print(f"{num} is {'' if armstrong_number(num) else 'not '}an Armstrong number.")
|
print(f"{num} is {'' if armstrong_number(num) else 'not '}an Armstrong number.")
|
||||||
print(f"{num} is {'' if narcissistic_number(num) else 'not '}an Armstrong number.")
|
print(f"{num} is {'' if narcissistic_number(num) else 'not '}an Armstrong number.")
|
||||||
print(f"{num} is {'' if pluperfect_number(num) else 'not '}an Armstrong number.")
|
print(f"{num} is {'' if pluperfect_number(num) else 'not '}an Armstrong number.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
main()
|
main()
|
|
@ -1,158 +1,158 @@
|
||||||
"""
|
"""
|
||||||
A harshad number (or more specifically an n-harshad number) is a number that's
|
A harshad number (or more specifically an n-harshad number) is a number that's
|
||||||
divisible by the sum of its digits in some given base n.
|
divisible by the sum of its digits in some given base n.
|
||||||
Reference: https://en.wikipedia.org/wiki/Harshad_number
|
Reference: https://en.wikipedia.org/wiki/Harshad_number
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def int_to_base(number: int, base: int) -> str:
|
def int_to_base(number: int, base: int) -> str:
|
||||||
"""
|
"""
|
||||||
Convert a given positive decimal integer to base 'base'.
|
Convert a given positive decimal integer to base 'base'.
|
||||||
Where 'base' ranges from 2 to 36.
|
Where 'base' ranges from 2 to 36.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
>>> int_to_base(23, 2)
|
>>> int_to_base(23, 2)
|
||||||
'10111'
|
'10111'
|
||||||
>>> int_to_base(58, 5)
|
>>> int_to_base(58, 5)
|
||||||
'213'
|
'213'
|
||||||
>>> int_to_base(167, 16)
|
>>> int_to_base(167, 16)
|
||||||
'A7'
|
'A7'
|
||||||
>>> # bases below 2 and beyond 36 will error
|
>>> # bases below 2 and beyond 36 will error
|
||||||
>>> int_to_base(98, 1)
|
>>> int_to_base(98, 1)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: 'base' must be between 2 and 36 inclusive
|
ValueError: 'base' must be between 2 and 36 inclusive
|
||||||
>>> int_to_base(98, 37)
|
>>> int_to_base(98, 37)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: 'base' must be between 2 and 36 inclusive
|
ValueError: 'base' must be between 2 and 36 inclusive
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if base < 2 or base > 36:
|
if base < 2 or base > 36:
|
||||||
raise ValueError("'base' must be between 2 and 36 inclusive")
|
raise ValueError("'base' must be between 2 and 36 inclusive")
|
||||||
|
|
||||||
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
result = ""
|
result = ""
|
||||||
|
|
||||||
if number < 0:
|
if number < 0:
|
||||||
raise ValueError("number must be a positive integer")
|
raise ValueError("number must be a positive integer")
|
||||||
|
|
||||||
while number > 0:
|
while number > 0:
|
||||||
number, remainder = divmod(number, base)
|
number, remainder = divmod(number, base)
|
||||||
result = digits[remainder] + result
|
result = digits[remainder] + result
|
||||||
|
|
||||||
if result == "":
|
if result == "":
|
||||||
result = "0"
|
result = "0"
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def sum_of_digits(num: int, base: int) -> str:
|
def sum_of_digits(num: int, base: int) -> str:
|
||||||
"""
|
"""
|
||||||
Calculate the sum of digit values in a positive integer
|
Calculate the sum of digit values in a positive integer
|
||||||
converted to the given 'base'.
|
converted to the given 'base'.
|
||||||
Where 'base' ranges from 2 to 36.
|
Where 'base' ranges from 2 to 36.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
>>> sum_of_digits(103, 12)
|
>>> sum_of_digits(103, 12)
|
||||||
'13'
|
'13'
|
||||||
>>> sum_of_digits(1275, 4)
|
>>> sum_of_digits(1275, 4)
|
||||||
'30'
|
'30'
|
||||||
>>> sum_of_digits(6645, 2)
|
>>> sum_of_digits(6645, 2)
|
||||||
'1001'
|
'1001'
|
||||||
>>> # bases below 2 and beyond 36 will error
|
>>> # bases below 2 and beyond 36 will error
|
||||||
>>> sum_of_digits(543, 1)
|
>>> sum_of_digits(543, 1)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: 'base' must be between 2 and 36 inclusive
|
ValueError: 'base' must be between 2 and 36 inclusive
|
||||||
>>> sum_of_digits(543, 37)
|
>>> sum_of_digits(543, 37)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: 'base' must be between 2 and 36 inclusive
|
ValueError: 'base' must be between 2 and 36 inclusive
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if base < 2 or base > 36:
|
if base < 2 or base > 36:
|
||||||
raise ValueError("'base' must be between 2 and 36 inclusive")
|
raise ValueError("'base' must be between 2 and 36 inclusive")
|
||||||
|
|
||||||
num_str = int_to_base(num, base)
|
num_str = int_to_base(num, base)
|
||||||
res = sum(int(char, base) for char in num_str)
|
res = sum(int(char, base) for char in num_str)
|
||||||
res_str = int_to_base(res, base)
|
res_str = int_to_base(res, base)
|
||||||
return res_str
|
return res_str
|
||||||
|
|
||||||
|
|
||||||
def harshad_numbers_in_base(limit: int, base: int) -> list[str]:
|
def harshad_numbers_in_base(limit: int, base: int) -> list[str]:
|
||||||
"""
|
"""
|
||||||
Finds all Harshad numbers smaller than num in base 'base'.
|
Finds all Harshad numbers smaller than num in base 'base'.
|
||||||
Where 'base' ranges from 2 to 36.
|
Where 'base' ranges from 2 to 36.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
>>> harshad_numbers_in_base(15, 2)
|
>>> harshad_numbers_in_base(15, 2)
|
||||||
['1', '10', '100', '110', '1000', '1010', '1100']
|
['1', '10', '100', '110', '1000', '1010', '1100']
|
||||||
>>> harshad_numbers_in_base(12, 34)
|
>>> harshad_numbers_in_base(12, 34)
|
||||||
['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B']
|
['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B']
|
||||||
>>> harshad_numbers_in_base(12, 4)
|
>>> harshad_numbers_in_base(12, 4)
|
||||||
['1', '2', '3', '10', '12', '20', '21']
|
['1', '2', '3', '10', '12', '20', '21']
|
||||||
>>> # bases below 2 and beyond 36 will error
|
>>> # bases below 2 and beyond 36 will error
|
||||||
>>> harshad_numbers_in_base(234, 37)
|
>>> harshad_numbers_in_base(234, 37)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: 'base' must be between 2 and 36 inclusive
|
ValueError: 'base' must be between 2 and 36 inclusive
|
||||||
>>> harshad_numbers_in_base(234, 1)
|
>>> harshad_numbers_in_base(234, 1)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: 'base' must be between 2 and 36 inclusive
|
ValueError: 'base' must be between 2 and 36 inclusive
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if base < 2 or base > 36:
|
if base < 2 or base > 36:
|
||||||
raise ValueError("'base' must be between 2 and 36 inclusive")
|
raise ValueError("'base' must be between 2 and 36 inclusive")
|
||||||
|
|
||||||
if limit < 0:
|
if limit < 0:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
numbers = [
|
numbers = [
|
||||||
int_to_base(i, base)
|
int_to_base(i, base)
|
||||||
for i in range(1, limit)
|
for i in range(1, limit)
|
||||||
if i % int(sum_of_digits(i, base), base) == 0
|
if i % int(sum_of_digits(i, base), base) == 0
|
||||||
]
|
]
|
||||||
|
|
||||||
return numbers
|
return numbers
|
||||||
|
|
||||||
|
|
||||||
def is_harshad_number_in_base(num: int, base: int) -> bool:
|
def is_harshad_number_in_base(num: int, base: int) -> bool:
|
||||||
"""
|
"""
|
||||||
Determines whether n in base 'base' is a harshad number.
|
Determines whether n in base 'base' is a harshad number.
|
||||||
Where 'base' ranges from 2 to 36.
|
Where 'base' ranges from 2 to 36.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
>>> is_harshad_number_in_base(18, 10)
|
>>> is_harshad_number_in_base(18, 10)
|
||||||
True
|
True
|
||||||
>>> is_harshad_number_in_base(21, 10)
|
>>> is_harshad_number_in_base(21, 10)
|
||||||
True
|
True
|
||||||
>>> is_harshad_number_in_base(-21, 5)
|
>>> is_harshad_number_in_base(-21, 5)
|
||||||
False
|
False
|
||||||
>>> # bases below 2 and beyond 36 will error
|
>>> # bases below 2 and beyond 36 will error
|
||||||
>>> is_harshad_number_in_base(45, 37)
|
>>> is_harshad_number_in_base(45, 37)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: 'base' must be between 2 and 36 inclusive
|
ValueError: 'base' must be between 2 and 36 inclusive
|
||||||
>>> is_harshad_number_in_base(45, 1)
|
>>> is_harshad_number_in_base(45, 1)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: 'base' must be between 2 and 36 inclusive
|
ValueError: 'base' must be between 2 and 36 inclusive
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if base < 2 or base > 36:
|
if base < 2 or base > 36:
|
||||||
raise ValueError("'base' must be between 2 and 36 inclusive")
|
raise ValueError("'base' must be between 2 and 36 inclusive")
|
||||||
|
|
||||||
if num < 0:
|
if num < 0:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
n = int_to_base(num, base)
|
n = int_to_base(num, base)
|
||||||
d = sum_of_digits(num, base)
|
d = sum_of_digits(num, base)
|
||||||
return int(n, base) % int(d, base) == 0
|
return int(n, base) % int(d, base) == 0
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
|
@ -1,54 +1,54 @@
|
||||||
"""
|
"""
|
||||||
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
|
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
|
||||||
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … shows the first 11 ugly numbers. By convention,
|
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … shows the first 11 ugly numbers. By convention,
|
||||||
1 is included.
|
1 is included.
|
||||||
Given an integer n, we have to find the nth ugly number.
|
Given an integer n, we have to find the nth ugly number.
|
||||||
|
|
||||||
For more details, refer this article
|
For more details, refer this article
|
||||||
https://www.geeksforgeeks.org/ugly-numbers/
|
https://www.geeksforgeeks.org/ugly-numbers/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def ugly_numbers(n: int) -> int:
|
def ugly_numbers(n: int) -> int:
|
||||||
"""
|
"""
|
||||||
Returns the nth ugly number.
|
Returns the nth ugly number.
|
||||||
>>> ugly_numbers(100)
|
>>> ugly_numbers(100)
|
||||||
1536
|
1536
|
||||||
>>> ugly_numbers(0)
|
>>> ugly_numbers(0)
|
||||||
1
|
1
|
||||||
>>> ugly_numbers(20)
|
>>> ugly_numbers(20)
|
||||||
36
|
36
|
||||||
>>> ugly_numbers(-5)
|
>>> ugly_numbers(-5)
|
||||||
1
|
1
|
||||||
>>> ugly_numbers(-5.5)
|
>>> ugly_numbers(-5.5)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
TypeError: 'float' object cannot be interpreted as an integer
|
TypeError: 'float' object cannot be interpreted as an integer
|
||||||
"""
|
"""
|
||||||
ugly_nums = [1]
|
ugly_nums = [1]
|
||||||
|
|
||||||
i2, i3, i5 = 0, 0, 0
|
i2, i3, i5 = 0, 0, 0
|
||||||
next_2 = ugly_nums[i2] * 2
|
next_2 = ugly_nums[i2] * 2
|
||||||
next_3 = ugly_nums[i3] * 3
|
next_3 = ugly_nums[i3] * 3
|
||||||
next_5 = ugly_nums[i5] * 5
|
next_5 = ugly_nums[i5] * 5
|
||||||
|
|
||||||
for _ in range(1, n):
|
for _ in range(1, n):
|
||||||
next_num = min(next_2, next_3, next_5)
|
next_num = min(next_2, next_3, next_5)
|
||||||
ugly_nums.append(next_num)
|
ugly_nums.append(next_num)
|
||||||
if next_num == next_2:
|
if next_num == next_2:
|
||||||
i2 += 1
|
i2 += 1
|
||||||
next_2 = ugly_nums[i2] * 2
|
next_2 = ugly_nums[i2] * 2
|
||||||
if next_num == next_3:
|
if next_num == next_3:
|
||||||
i3 += 1
|
i3 += 1
|
||||||
next_3 = ugly_nums[i3] * 3
|
next_3 = ugly_nums[i3] * 3
|
||||||
if next_num == next_5:
|
if next_num == next_5:
|
||||||
i5 += 1
|
i5 += 1
|
||||||
next_5 = ugly_nums[i5] * 5
|
next_5 = ugly_nums[i5] * 5
|
||||||
return ugly_nums[-1]
|
return ugly_nums[-1]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from doctest import testmod
|
from doctest import testmod
|
||||||
|
|
||||||
testmod(verbose=True)
|
testmod(verbose=True)
|
||||||
print(f"{ugly_numbers(200) = }")
|
print(f"{ugly_numbers(200) = }")
|
Loading…
Reference in New Issue
Block a user