Found 34488 Articles for Programming

How to print Narcissistic(Armstrong) Numbers with Python?

Ankith Reddy
Updated on 17-Jun-2020 14:54:08

284 Views

To print Narcissistic Numbers, let's first look at the definition of it. It is a number that is the sum of its own digits each raised to the power of the number of digits. For example, 1, 153, 370 are all Narcissistic numbers. You can print these numbers by running the following codedef print_narcissistic_nums(start, end): for i in range(start, end + 1):    # Get the digits from the number in a list:    digits = list(map(int, str(i)))    total = 0    length = len(digits)    for d in digits:       total += d ** length   ... Read More

How to clamp floating numbers in Python?

George John
Updated on 17-Jun-2020 14:51:13

6K+ Views

Clamp function limits a value to a given range. Python doesn't have such a function in built. You can create this function likedef clamp(num, min_value, max_value):    return max(min(num, max_value), min_value) print(clamp(5, 1, 20)) print(clamp(1, 10, 20)) print(clamp(20, 1, 10))This will give the output5 10 10

How can we generate Strong numbers in Python?

Sravani S
Updated on 17-Jun-2020 14:52:33

151 Views

To print Strong Numbers, let's first look at the definition of it. It is a number that is the sum of factorials of its own digits. For example, 145 is a Strong number. First, create a function to calculate factorial:def fact(num):    def factorial(n):    num = 1    while n >= 1:       num = num * n       n = n - 1    return numYou can print these numbers by running the following code:def factorial(n):    num = 1    while n >= 1:       num = num * n   ... Read More

How to convert numbers to words using Python?

Arjun Thakur
Updated on 17-Jun-2020 14:48:33

1K+ Views

The constructor for the string class in python, ie, str can be used to convert a number to a string in python. For example, i = 10050 str_i = str(i) print(type(str_i))This will give the output:But if you want something that converts integers to words like 99 to ninety-nine, you have to use an external package or build one yourself. The pynum2word module is pretty good at this task. You can install it using$ pip install pynum2wordThen use it in the following way>>> import num2word >>> num2word.to_card(16) 'sixteen' >>> num2word.to_card(23) 'twenty-three' >>> num2word.to_card(1223) 'one thousand, two hundred and twenty-three'Read More

How to bitwise XOR of hex numbers in Python?

Ramu Prasad
Updated on 17-Jun-2020 14:47:12

2K+ Views

You can get the XOR of any type of numbers using the ^ operator. Specifically for hex numbers, you can use:a = 0x12ef b = 0xabcd print(hex(a ^ b))This will give the output:0xb922The 0x at the beginning of the numbers implies that the number is in hex representation. You can use the ^ operator for other integer representations as well.

How to divide large numbers using Python?

George John
Updated on 30-Jul-2019 22:30:22

923 Views

You can divide large numbers in python as you would normally do. But this has a lot of precision issues as such operations cannot be guaranteed to be precise as it might slow down the language. You would be better off using a numeric computation library like bigfloat to perform such operations.You can read more about floating point issues that you might face with precision on https://docs.python.org/3/tutorial/floatingpoint.html

How to multiply large numbers using Python?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:22

2K+ Views

You can multiply large numbers in python directly without worrying about speed. Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate. As long as you have version 2.5 or better, just perform standard math operations and any number which exceeds the boundaries of 32-bit math will be automatically (and transparently) converted to a bignum. For example, a = 15421681351 b = 6184685413848 print(a * b) This will give the output − 95378247708541418748648

How to add/subtract large numbers using Python?

karthikeya Boyini
Updated on 05-Mar-2020 11:15:16

1K+ Views

You can add/subtract large numbers in python directly without worrying about speed. Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate.As long as you have version 2.5 or better, just perform standard math operations and any number which exceeds the boundaries of 32-bit math will be automatically (and transparently) converted to a bignum.examplea = 182841384165841685416854134135 b = 135481653441354138548413384135 print(a - b)OutputThis will give the output −47359730724487546868440750000

How to find Kaprekar numbers within a given range using Python?

Nikitha N
Updated on 05-Mar-2020 11:13:49

646 Views

A modified Kaprekar number is a positive whole number n with d digits, such that when we split its square into two pieces - a right hand piece r with d digits and a left hand piece l that contains the remaining d or d−1 digits, the sum of the pieces is equal to the original number (i.e. l + r = n).You can find Kaprekar numbers within a given range by testing each number for the given condition in the given range. exampledef print_Kaprekar_nums(start, end):    for i in range(start, end + 1):       # Get the digits ... Read More

How to generate pyramid of numbers using Python?

Ankith Reddy
Updated on 05-Mar-2020 11:11:42

679 Views

There are multiple variations of generating pyramids using numbers in Python. Let's look at the 2 simplest formsExamplefor i in range(5):    for j in range(i + 1):       print(j + 1, end="")    print("")OutputThis will give the output1 12 123 1234 12345ExampleYou can also print numbers continuously usingstart = 1 for i in range(5):    for j in range(i + 1):       print(start, end=" ")       start += 1 print("")OutputThis will give the output1 2 3 4 5 6 7 8 9 10 11 12 13 14 15ExampleYou can also print these numbers in reverse usingstart = 15 for i in range(5):    for j in range(i + 1):    print(start, end=" ")    start -= 1 print("")OutputThis will give the output15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Advertisements