how to write a function that recursively sum the digits - python

I need a function that takes variables and then computes these variables (example for x and y xy) and recursively finds the sum of points there is two left.
But,
The function needs to print all the calculation.
For an example
Function is going to print this,53
Sorry for my bad english and good luck with the question.

Not very beautiful, but does the job
def f(x, y):
tmp = x**y
sum = 0
while tmp >= 1:
sum = sum + tmp % 10
tmp = (tmp // 10)
return sum

This might do it for you.
Not the most pretty code either:
def func(x,y):
res1 = str(x**y)
to_print = "{0}^{1} = {2}".format(x,y,res1)
while len(res1) > 1:
to_print += " = " + res1[0]
res2 = int(res1[0])
for n in res1[1:]:
to_print += " + " + n
res2 += int(n)
to_print += " = " + str(res2)
res1 = str(res2)
print(to_print)
Basically I created a string from the value of x**y so it would be easy to iterate(res1 = str(x**y)).
to_print saves the final output and is used to concatenate all the values.
to_print = "{0}^{1} = {2} = {3}".format(x,y,res1, res1[0])
I used a format string, if you are unfamiliar with this check this.
res2 saves the sum of the digits:
res2 = int(res1[0])
for n in res1[1:]:
res2 += int(n)

Related

Implementing memoization within a Collatz algorithm (Python)

I am trying to perform a Collatz algorithm on the following code. It works fine when I use a range of 1-10 etc... However, if the range is for example 1-500,000 it's too slow and won't ever show me the output of the longest sequence.
numberArray = []
s=int(1)
f=int(10)
def collatz(n):
global count
if n == 1:
count += 1
numberArray.append(count)
return True
elif (n % 2) == 0:
count += 1
collatz(n/2)
else:
count += 1
collatz(3*n+1)
for i in range (s, f+1):
count = 0
ourNumber = i
collatz(i)
print(max(numberArray))
Stef means something like this, which uses a dictionary to memorise the values that have already been counted:
s = 1
f = 10000000
def collatz(n):
if n in collatz.memory:
return collatz.memory[n]
if (n % 2) == 0:
count = collatz(n//2)+1
else:
count = collatz((3*n+1)//2)+2
collatz.memory[n] = count
return count
collatz.memory = {1:0}
highest = max(collatz(i) for i in range(s, f+1))
highest_n = max(collatz.memory, key=collatz.memory.get)
print(f"collatz({highest_n}) is {highest}")
Output:
collatz(8400511) is 685
Use lru_cache decorator. Its function to memorize (cache) the returned value of function called with specific argument.
Also read how to write clean code in python
The next code solves your problem
from functools import lru_cache
number_array = []
s = 1
f = 500000
#lru_cache
def collatz(n: int):
if n == 1:
return 1
elif n % 2 == 0:
return 1 + collatz(n // 2)
else:
return 1 + collatz(3 * n + 1)
for i in range(s, f + 1):
number_array.append(collatz(i))
print(number_array)

function that prints counding down numbers and up but in a pattern

I already know how to count down and up using reccurssive function but I cant figure out how to define another function to have spaces in a pattern like this:
Desired output
My code for counting down and up:
def f(n):
if n==0:
print (n)
return
print (n)
f(n-1)
print (n)
a = 5 #you can take any value, it will be actually taken from the user
def f(n):
global a
if n == 0:
print(' '*a + str(n))
return
print(' '*(a-n), + str(n))
f(n-1)
print(' '*(a-n), + str(n))
a = int(input())
f(a)
I am posting this to contrast the other answers provided here -
def f(n, s = ""):
if n <= 0:
print(s, n)
else:
print(s, n)
f(n - 1, s + " ")
print(s, n)
f(5)
5
4
3
2
1
0
1
2
3
4
5
The program could be further simplified, if desired -
def f(n, s = ""):
print(s, n)
if n > 0:
f(n - 1, s + " ")
print(s, n)
f(5)
Default parameter s could be an integer instead of a string, if desired -
def f(n, s = 0):
print(" "*s, n)
if n > 0:
f(n - 1, s + 1)
print(" "*s, n)
f(5)
Output is the same.
print adds a space between the printed arguments. To remove this, we can print a single formatted value -
def f(n, s = ""):
print(s + str(n)) # <- single value
if n > 0:
f(n - 1, s + " ")
print(s + str(n)) # <- single value
Or you can use a formatted string literal -
def f(n, s = ""):
print(f"{s}{n}") # <- formatted string
if n > 0:
f(n - 1, s + " ")
print(f"{s}{n}") # <- formatted string
This is probably not exactly what you need. But it gives you an idea :)
As you can see it is based on #Samudra Ganguly's answer
def f(n,x):
if n < 0:
return
p(n,x-n)
f(n-1,x)
if n !=0:
p(n,x-n)
def p(m,x):
print(" ", end = "")
if x>0:
p(m,x-1)
else:
print(m, end = "\n")
f(8,8)
Also you can change the function like this:
k=4;
def f(n):
if n < 0:
return
p(n,k-n)
f(n-1)
if n !=0:
p(n,k-n)
def p(m,x):
print(" ", end = "")
if x>0:
p(m,x-1)
else:
print(m, end = "\n")
f(k)

How to write an adding function that prints each number being added, up to a certain number

I want to create a function that will multiply each number starting from one, up to the number that is specified as a parameter in the function by itself and then add them up together to the parameter number, the trick is, that I want also to write the multiplication as an adding equation.
Here's what I have so far:
def adding(num):
summ = 0
for x in range(1, num+1):
summ += x*x
return summ
So far I can show the total result, but I can't figure out a way to print each number being added as I showed above.
def adding(num):
summ = 0
for x in range(1, num+1):
if x > 1:
print(' + ', end='')
print('+'.join([str(x) for _ in range(x)]), end='')
summ += x*x
print(' =', summ)
return summ
This way is not beautiful by any means but I think it illustrates pretty simply what you're trying to do
def adding(num):
summ = 0
string = ''
for x in range(1, num + 1):
if x > 1:
# append to string x, x times #
string += ' + ' + (str(x) + '+') * x
# remove final character in string #
string = string[:-1]
else:
string += str(x)
summ += x * x
if x == num:
string += ' = ' + str(summ)
print(string)
return summ

Binary Addition using a carry

Im trying to add two binary numbers without converting the two numbers, S and T, into a base 10,using recursion and I'm having difficulty incorporating the carry into the code. Also, I'm not exactly sure what to do if one binary number is longer than the other.
def addB(S,T):
'''adds binary number without converting to base 10'''
def addBhelper(S,T,carry):
if S=='' and T=='':
return ''
if S[-1] + T[-1]+carry==0:
return addBhelper(S[:-1],T[:-1],0) + str((carry+int(S[-1]) + int(T[-1]))% 2)
if S[-1] + T[-1]+carry==1:
return addBhelper(S[:-1],T[:-1],1) + str((carry+int(S[-1]) + int(T[-1])) % 2)
if S[-1] + T[-1]+carry==2:
return addBhelper(S[:-1],T[:-1],2) + str((carry+int(S[-1]) + int(T[-1])) % 2)
if S[-1] + T[-1]+carry==3:
return addBhelper(S[:-1],T[:-1],2) + str((carry+int(S[-1]) + int(T[-1])) % 2)
return addBhelper(S,T,0)
---- updated to fix code formatting
Here's a cleaner version that uses some Python syntactic sugar:
def add(a,b,c=0):
if a == '' and b == '':
return str(c)
a = a or '0'
b = b or '0'
n = int(a[-1]) + int(b[-1]) + c
return add(a[:-1],b[:-1],n//2) + str(n%2)
Use default value of carry c=0 to get rid of the inner function
a = a or '0' sets a to '0' if it's ''
You forgot to convert string to integer before adding them
n//2 get the carry
Let’s start with the first part, which is making sure the two strings are the same length. Since they're numbers, all you have to do is '0' pad the shorter number
max_len = max(len(S), len(T))
# the more simple to understand way
while len(S) < max_len: S = '0' + S
while len(T) < max_len: T = '0' + T
# or you can use this python trickery
S = ('0' * (max_len - len(S))) + S
T = ('0' * (max_len - len(T))) + T
For the carries, your carries should be as follows:
For sum = 0, carry = 0
For sum = 1, carry = 0
For sum = 2, carry = 1
For sum = 3, carry = 1
Hope that helps,

Factoring, Substituting and printing roots of a polynomial

We've been asked to make a program that takes in polynomial coefficients(i.e. [1, 2, 1] representing x^2 + 2x + 1 and use the rational root theorem to find and print the roots in a list of fractional strings. We were only allowed to use import math. The problem with my code is it only works if I input [1, 2, 1] and when I input other polynomials, it prints a blank list.
import math
roots = []
pr = []
pf = []
qf = []
count = 1
t = 0
co = raw_input('Input Coefficients: ')
co = co.split(' ')
p = co[0]
q = co[-1]
def gcd(a,b): #function for computing gcf between two numbers. will be used to simplify fractions into lowest terms
r = a%b
if r == 0:
return b
else:
return gcd(b,r)
def rmvvalues(coefficients, val): #remove the zeroes from the list because zero is not factorable
for i in range(coefficients.count(val)):
coefficients.remove(val)
while int(p) == 0: #if the constant was 0, it means 0 is automatically a root
roots.append(0)
rmvvalues(co, str('0'))
p = co[0]
q = co[-1]
while count <= math.fabs(int(p)): #factors of the constant
if int(p) % count == 0:
pf.append(count)
pf.append(count*(-1))
count = count + 1
else:
count = count + 1
count = 1
while count <= math.fabs(int(q)): #factors of the last term
if int(q) % count == 0:
qf.append(count)
qf.append(count*(-1))
count = count + 1
else:
count = count + 1
count = 1
for i in range(len(pf)): #every element in the first list of factors is to be divided by every element of the other list of factors
for j in range(len(qf)):
result = 0
for c in range(len(co)): #and each pf/qf is to be checked if it would make the polynomial zero
result = int(result) * (int(pf[i])/int(qf[j])) + int(co[c])
if result == 0: #if it makes it zero, it appends the answer in fraction string to the list of roots
if (int(pf[i]) / int(qf[j])) == 1: #add 1 to the list of roots if p/q == 1 and would make the equation zero
roots.append(1)
elif int(pf[i])/int(qf[j]) == -1: #add -1 to the list of roots if p/q == -1 and would make the equation zero
roots.append(-1)
else: #if they would be fractions
a = int(pf[i]) / int(gcd(int(pf[i]),int(qf[j])))
b = int(qf[j]) / int(gcd(int(pf[i]),int(qf[j])))
roots.append(str(a) + '/' +str(b))
roots = sorted(set(roots))
print roots
p.s.(I just copy/pasted the code from my editor so the indentation may be a little off)
I have an implementation to propose, but not a debugging of yours.
The fact is that you are writing a big one bloc code that I don't understand.
As I want to help you I give you an example of implementation that I hope you will find helpful AND readable. My big suggest when coding is to split the code in little function easy to understand with meaningful name, and then add an algorithm function that call all the sub-functions. This make the algorithm to looks like pseudocode and be mode understandable.
Additionally, more specifically related to python, your code is supposed to follow some rule. Runtime code is supposed to be placed after if __name__ == "__main__": bloc.
Hope it helps you (take a look at the findSolution method):
class Polynom():
""" A class representing a polynom that provides methods to find roots of the polynom. """
#-----------------------------------------------------------------------------------------
def __init__(self, coefficients):
"""
Constructor of the class.
Takes a coeficient list [n, n-1, ..., n1, n0] that are the coefficients of a polynom.
Example :
Polynom([1,2,3]) stand for : x² + 2x + 3
"""
self.coefficients = coefficients
#-----------------------------------------------------------------------------------------
def _dividers(self, coefficient):
"""Returns the list of the divider of a number by filtering the values in [1, 2, ... , |coefficient| ] that divide coefficient. """
def filter_dividers(coefficient, candidate_values):
# lambda function explanations : http://www.diveintopython.net/power_of_introspection/lambda_functions.html
return filter(lambda number : coefficient%number == 0, candidate_values)
return list(filter_dividers(coefficient, range(1, abs(coefficient)+1)))
#-----------------------------------------------------------------------------------------
def _gcd(self, x, y):
""" Returns the greatest common diviser of the integers x and y """
if y == 0:
return abs(x)
else:
r = x%y
return self._gcd(y, r)
#-----------------------------------------------------------------------------------------
def _positiveAndNegativeCombinations(self, p_values, q_values):
"""
Returns the list of positives and negatives combination of (p,q) pairs.
Example :
[1,2]
-> [(1,4), (-1,4), (2,4), (-2,4), (1,5), (-1,5), (2,5), (-2,5)]
[4,5]
"""
def combinations(p, q_values):
if len(q_values) == 1:
return [(p, q_values[0])]
else:
return [(p, q_values[0])] + combinations(p, q_values[1:])
result = []
for p in p_values:
p_combinations = combinations(p, q_values)
for combination in p_combinations:
result += [combination, (-1*combination[0], combination[1])]
return result
#-----------------------------------------------------------------------------------------
def __repr__(self):
""" Representation of the object in a string for printing purpose."""
def from_number_to_string_exposant(number):
"""
Returns a string that is the number given as exposant.
Example : 1 -> "¹"
"""
utf8_exposant = {"0":"⁰", "1":"¹", "2":"²", "3": "³", "4":"⁴", "5":"⁵", "6":"⁶", "7":"⁷", "8":"⁸", "9":"⁹"}
string = str(number)
result = ""
for digit in string:
result += utf8_exposant[digit]
return result
result = ""
degree = 0
coefficients = self.coefficients
while coefficients != [] :
coef, coefficients = coefficients[-1], coefficients[0:-1]
result = "+ " +str(coef)+"x"+ from_number_to_string_exposant(degree) + result
degree+=1
return "<Polynom :" + result[1:] + " = 0 >"
#-----------------------------------------------------------------------------------------
def valueFor(self, value):
""" Returns ture or false depending on the fact that the given value is or not a polunom's solution. """
total_value = 0
degree = 0
coefficients = self.coefficients
while coefficients != [] :
coef, coefficients = coefficients[-1], coefficients[0:-1]
total_value += coef*(value**degree)
degree += 1
return total_value
#-----------------------------------------------------------------------------------------
def isSolution(self, value):
""" Returns true or false depending if the given value is a polynom solution or not """
return (self.valueFor(value) == 0)
#-----------------------------------------------------------------------------------------
def findSolution(self):
"""
Return a pair (p,q) that verify that p/q is a solution of this polynom. If no valid pair is find, return None.
Call to this function come with log printing.
"""
print("Search solution for ", self)
a0 = self.coefficients[-1]
aN = self.coefficients[0]
if a0 == 0 or aN == 0:
return None #algorithm is not applicable in this case.
else:
p_values = self._dividers(a0)
q_values = self._dividers(aN)
print("finded dividers of p :", p_values)
print("finded dividers of q :", q_values)
candidate_solutions = self._positiveAndNegativeCombinations(p_values,q_values)
print("(p,q) pairs to evaluate are : \n\t",candidate_solutions)
for candidate in candidate_solutions :
candidate_value = 1.0 * candidate[0] / candidate[1]
print("pair : ",candidate, " | value : ", candidate_value)
if self.isSolution(candidate_value):
print()
return candidate
else:
print("The pair ", candidate, "is invalid, replacing x by it leads to say that 0=", self.valueFor(candidate_value))
return None
#-----------------------------------------------------------------------------------------
if __name__ == "__main__":
poly = Polynom([2,1,-6])
print(poly.findSolution())
Execute it with python3 (or change the print calls).
Arthur.
https://code.activestate.com/recipes/577974-polynomial-factoring-using-rational-root-theorem/
Here's a working link.
from math import ceil
listOfFactors = lambda n: {i for i in range(1,ceil(abs(n)/2)+1) if n%i == 0}
def removeDuplicates(mylist):
if mylist:
mylist.sort()
last = mylist[-1]
for i in range(len(mylist)-2, -1, -1):
if last == mylist[i]:
del mylist[i]
else:
last = mylist[i]
return mylist
def polyRoots(polyListCoeff):
allFactors = set()
allFactorsListOld = list(allFactors.union(listOfFactors(polyListCoeff[0]),{polyListCoeff[0]},listOfFactors(polyListCoeff[-1]),{polyListCoeff[-1]}))
allFactorsListOld.extend([-1*i for i in allFactorsListOld])
allFactorsList = list()
for k in allFactorsListOld:
for j in allFactorsListOld:
allFactorsList.append(k/j)
allFactorsList = removeDuplicates(allFactorsList)
polyListCoeff.reverse()
roots = [i for i in allFactorsList if sum([pow(i,j)*polyListCoeff[j] for j in range(0,len(polyListCoeff))]) == 0]
factorList = list()
for i in roots:
if i<0:
factorList.append("(x+{})".format(-i))
else:
factorList.append("(x-{})".format(i))
return "".join(factorList)
if __name__ == "__main__":
polyRoots([1,0,-4])
# '(x+2.0)(x-2.0)'

Categories

Resources