Reputation: 13
I am a complete (GCSE) beginner in python. I am trying to add the numbers in an integer. My variable FNT comes out as a different number every time (depending on what I have inputted earlier) and I then need the numbers to add themselves. E.g. FNT=19 I now need the number to do this- 1+9=10 1+0=1 The numbers need to keep adding themselves until they are a single digit but the number could be different every time. All help is greatly appreciated but, as I said I am a complete beginner and probably won't be able to understand anything too complicated, so would anybody know how to do this?
Upvotes: 1
Views: 10801
Reputation: 829
This is done in java, but its still the same logic to be used in python.
I made use of a split
, to split the numbers, then i converted each of the splitted number to an integer, and added them up. I now used an if statement
to check if the sum is greater than or equal to 10. if so, then i repeat the process again.
This is the program
String num = "19"; //you can make use of any value here
String array1[] = num.split("");
int sum = 0;
for (int i = 1; i <= num.length(); i++) {
sum = sum + Integer.parseInt(array1[i]); //converting each element to an integer then sum it up.
}
if (sum >= 10) {
String newnum = sum + ""; //converting the sum to a string
String newarray[] = newnum.split("");
int newsum = 0;
for (int i = 1; i <= newnum.length(); i++) {
newsum = newsum + Integer.parseInt(newarray[i]);
}
System.out.println( newsum);
}else{
System.out.println(num);
}
Output
1
Upvotes: 0
Reputation: 4196
There are two approaches: the mathematical way and the way that uses the fact that strings are iterables in python.
The mathematical way uses modulo (%
) and integral division (//
) to decompose a number into digits:
number = int(input('What number do you want to start with? '))
while number > 9:
decompose_helper, number = number, 0
while decompose_helper: # != 0 is implied
number += decompose_helper % 10
decompose_helper = decompose_helper // 10
print('Result is', number)
You can improve this code using the divmod
function:
number = int(input('What number do you want to start with? '))
while number > 9:
decompose_helper, number = number, 0
while decompose_helper: # != 0 is implied
decompose_helper, remainder = divmod(decompose_helper, 10)
number += remainder
print('Result is', number)
The iterable string way:
number = input('What number do you want to start with? ')
while len(number) > 1:
number = str(sum(int(digit) for digit in number))
print('Result is', number)
Neither of those code deal with input validation, so if your user enter something else than an integer, the code will crash. You might need to handle that.
I recommend using the mathematical way since it is faster. Timmings (removing input
and print
) are:
>>> timeit.timeit('math_way("4321234123541234")', setup='from __main__ import math_way', number=10000)
0.06196844787336886
>>> timeit.timeit('str_way("4321234123541234")', setup='from __main__ import str_way', number=10000)
0.10316650220192969
Upvotes: 2
Reputation: 67567
There are better ways of doing this but as a programming exercise this should give you some insight.
while digits>10:
t=0
for c in str(digits):
t+=int(c)
digits=t
convert the input number into a string and iterate over the digits, repeat until the answer is single digit.
Upvotes: 0
Reputation: 27
This might not be the most optimized, but it should do it:
def addThem(FNT):
while FNT >= 10:
FNT = sum([int(i) for i in list(str(FNT))])
return FNT
EDIT:
if FNT = 19, list(str(FNT)) returns ['1', '9'].
The list comprehension [int(i) for i in list(str(FNT))] returns [1, 9] (notice these are now integers)
Upvotes: 0
Reputation: 35788
To sum all the digits of a number, you can try a one-liner like this:
sum(int(chr) for chr in str(number))
You can apply that repeatedly until the result is less than 10:
res = number
while True:
res = sum(int(chr) for chr in str(res))
if res < 10:
break
res
now stores your result.
Upvotes: 0