We just started learning python this year in school, and so far we are only doing basic stuff with tkinter canvas (drawing houses, flowers etc.) and our teacher kinda sucks so I had to learn everything I know by now myself.
I've been thinking about this idea of writing a function to find the digital sum of a number (without recursion preferably) so I came up with this:
def sum(n):
total=0
for letter in str(n):
total+=int(letter)
return total
But then, I had an idea about using recursion to find the "absolute dig. sum of a number", for instance 99=9+9=18 =>1+8=9
and I came up with this
total=0
def suma(n):
global total
def part_sum(n):
global total
total_part=0
for letter in str(n):
total_part+=int(letter)
total=total_part
if total<10:
print(total)
else:
part_sum(total)
part_sum(n)
which basically makes a sum of n, checks if it is lower than n, if it is not it runs it again (basic recursion), but it uses a bunch of variables and global variables (which are for some reason bad as I've heard) and I am well aware it could be done much more efficiently using classes and objects, but I've only watched couple of vidoes on that so I'm not very good at it.
Could someone please edit my code and paste it here with some notes to help me understand it?
Recursion is often overused. I believe you can write suma() much more clearly without it. Build on what you have already created.
def sum(n):
total=0
for letter in str(n):
total+=int(letter)
return total
def suma(n):
while n >= 10:
n = sum(n)
return n
Here's my solution
def rec_dig_sum(n):
total = str(sum(map(int, str(n))))
while len(total)>1:
total = str(sum(map(int, total)))
return int(total)
You can think of map as taking map(function, [input1, input2, input3]) and returning [function(input1), function(input2), function(input3)]
Instead of saving a variable and updating it with each iteration of a loop (the normal way to do this):
def iterative_sum(num):
while num >= 10:
num = int(sum(map(int, str(num))))
return num
Send it as an argument to a new function call:
def recursive_sum(num):
if num >= 10:
return recursive_sum(sum(map(int, str(num))))
return num
You can also make a recursive function to do the initial summation, and use that in the function that keeps doing so until it's only one digit:
def rc(n, c=0):
if n:
return rc(n//10, c+n%10)
return c
def rs(n):
if n >= 10:
return rs(rc(n))
return n
Related
I'm given the task to define a function to find the largest pyramidal number. For context, this is what pyramidal numbers are:
1 = 1^2
5 = 1^2 + 2^2
14 = 1^2 + 2^2 + 3^2
And so on.
The first part of the question requires me to find iteratively the largest pyramidal number within the range of argument n. To which, I successfully did:
def largest_square_pyramidal_num(n):
total = 0
i = 0
while total <= n:
total += i**2
i += 1
if total > n:
return total - (i-1)**2
else:
return total
So far, I can catch on.
The next part of the question then requires me to define the same function, but this time recursively. That's where I was instantly stunned. For the usual recursive functions that I have worked on before, I had always operated ON the argument, but had never come across a function where the argument was the condition instead. I struggled for quite a while and ended up with a function I knew clearly would not work. But I simply could not wrap my head around how to "recurse" such function. Here's my obviously-wrong code:
def largest_square_pyramidal_num_rec(n):
m = 0
pyr_number = 0
pyr_number += m**2
def pyr_num(m):
if pyr_number >= n:
return pyr_number
else:
return pyr_num(m+1)
return pyr_number
I know this is erroneous, and I can say why, but I don't know how to correct it. Does anyone have any advice?
Edit: At the kind request of a fellow programmer, here is my logic and what I know is wrong:
Here's my logic: The process that repeats itself is the addition of square numbers to give the pyr num. Hence this is the recursive process. But this isn't what the argument is about, hence I need to redefine the recursive argument. In this case, m, and build up to a pyr num of pyr_number, to which I will compare with the condition of n. I'm used to recursion in decrements, but it doesn't make sense to me (I mean, where to start?) so I attempted to recall the function upwards.
BUT this clearly isn't right. First of all, I'm sceptical of defining the element m and pyr_num outside of the pyr_num subfunction. Next, m isn't pre-defined. Which is wrong. Lastly and most importantly, the calling of pyr_num will always call back pyr_num = 0. But I cannot figure out another way to write this logic out
Here's a recursive function to calculate the pyramid number, based on how many terms you give it.
def pyramid(terms: int) -> int:
if terms <=1:
return 1
return terms * terms + pyramid(terms - 1)
pyramid(3) # 14
If you can understand what the function does and how it works, you should be able to figure out another function that gives you the greatest pyramid less than n.
def base(n):
return rec(n, 0, 0)
def rec(n, i, tot):
if tot > n:
return tot - (i-1)**2
else:
return rec(n, i+1, tot+i**2)
print(base(NUMBER))
this output the same thing of your not-recursive function.
I have a line code like this -
while someMethod(n) < length and List[someMethod(n)] == 0:
# do something
n += 1
where someMethod(arg) does some computation on the number n. The problem with this code is that I'm doing the same computation twice, which is something I need to avoid.
One option is to do this -
x = someMethod(n)
while x < length and List[x] == 0:
# do something
x = someMethod(n + 1)
I am storing the value of someMethod(n) in a variable x and then using it later. However, the problem with this approach is that the code is inside a recursive method which is called multiple times. As a result, a lot of excess instances of variables x are being created which slows the code down.
Here's the snipped of the code -
def recursion(x, n, i):
while someMethod(n) < length and List[someMethod(n)] == 0:
# do something
n += 1
# some condition
recursion(x - 1, n, someList(i + 1))
and this recursion method is called many times throughout the code and the recursion is quite deep.
Is there some alternative available to deal with a problem like this?
Please try to be language independent if possible.
You can use memoization with decorators technique:
def memoize(f):
memo = dict()
def wrapper(x):
if x not in memo:
memo[x] = f(x)
return memo[x]
return wrapper
#memoize
def someMethod(x):
return <your computations with x>
As i understand your code correctly you are looking for some sort of memorization.
https://en.wikipedia.org/wiki/Memoization
it means that on every recursive call you have to save as mush as possible past calculations to use it in current calculation.
I am writing a program that gives me an overflow error. I realized that the cause of that was due to my input which could do inputs till 2147483646. I figured this using the sys.maxint. Anything beyond that gave me an overflow error. How I can take in inputs for large values? For value, 2147483646 my system hangs. How to deal with such an error. The statement of the problem here is given, A number is called lucky if the sum of its digits, as well as the sum of the squares of its digits is a prime number. How many numbers between A and B are lucky?
I am attaching the sample code here.
class luckynumbers():
#required numbers are only 0-9, their values can be stored in a lookup table
def __init__(self):
self.squarelist=[0,1,4,9,16,25,36,49,64,81]
def isEven(self, n):
if n%2 ==0:
return True
else:
return False
def isPrime(self,n):
return_val=True
if n==2:
return_val= True
if not self.isEven(n):
for i in xrange(2,n/2):
if n%i ==0:
return_val=False
break
else:
return_val= False
return return_val
def sumofDigits(self,n):
return sum(map(int, n))
def generateSquares(self, n):
return map(lambda x: self.squarelist[x], map(int,n))
def satisfy(self,n):
return self.isPrime(self.sumofDigits(n)) and self.isPrime(sum(self.generateSquares(n)))
def main():
luckyno=luckynumbers()
t=int(raw_input().rstrip())
count = []
for i in xrange(t):
counts = 0
a,b = map(int, raw_input().rstrip().split())
if a==1:
a=2
for j in xrange(a,b+1,1):
if luckyno.satisfy(str(j)):
counts+=1
count.append(counts)
for i in count:
print i
if __name__=='__main__':
main()
I am still looking at long integers document in python. But haven't figured a way yet. I have tried to use optimization as far as I can think of. Anything more, I will really grateful.
The xrange() function is limited to ints (not longs). The code works as expected if you replace the xrange() call where OverflowError occurs with just a range() call.
I'm trying to calculate the numbers of the fibonacci Sequence under 100, but the code I made doesn't work. What I have is:
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
num=0
while(num<100):
print (fib(num))
num+=1
I think this should work, but it doesn't, so it's definitely my issue with my coding. Could anyone resolve this?
So what about this code is not working? It looks like the implementation is correct, but it's of course, slow. You can try to store the numbers you compute in some kind of data structure as you go along to reduce your stack trace and prevent having to recalculate fib of 23 when you're trying to calculate fib of 24.
I am writing a program that must find if a number is even or not. It needs to follow this template. I can get it to find if a number is even or not recursively
The key is that you need to return a boolean value:
def isEven(num):
if (num <= 0):
return (num == 0)
return isEven(num-2)
For larger numbers though this quickly exceeds the default maximum recursion depth for Python. That can be remedied by calling sys.setrecursionlimit(n) where n is the number of recursive calls you want to allow. n in turn is limited by the platform you are on.
Try this, it works for integer values with 0 <= n <= sys.getrecursionlimit()-2:
def even(n):
return True if n == 0 else odd(n - 1)
def odd(n):
return False if n == 0 else even(n - 1)
It's a nice example of a pair of mutually recursive functions. Not the most efficient way to find the answer, of course - but nevertheless interesting from an academic point of view.
This template will help. You need to fill in the commented lines. The one you have in the question won't work - you aren't passing anything into isEven. This will only work if n >= 0, otherwise it will crash your program. Easy enough to fix if you ever need to deal with negative numbers.
def isEven(n):
if n == 0:
# Number is even
elif n == 1:
# Number is odd
else:
# Call the function again, but with a different n
Taking up wim's challenge to find a "different" way to do this: The prototypical recursive pattern is foo(cdr(x)), with a base case for the empty list… so let's write it around that:
def isEven(num):
def isEvenLength(l):
if not l:
return True
return not isEvenLength(l[1:])
return isEvenLength(range(num))
A really dumb use case for recursion, but here is my version anyway
import random
def isEven(num):
if random.random() < 0.5:
# let's learn about recursion!
return isEven(num)
else:
# let's be sane!
return num % 2 == 0
disclaimer: if you submitted this you'd probably tick off the teacher and come across as a smartypants.