Efficiently generating Stern's Diatomic Sequence - python

Stern's Diatomic Sequence can be read about in more details over here; however, for my purpose I will define it now.
Definition of Stern's Diatomic Sequence
Let n be a number to generate the fusc function out of. Denoted fusc(n).
If n is 0 then the returned value is 0.
If n is 1 then the returned value is 1.
If n is even then the returned value is fusc(n / 2).
If n is odd then the returned value is fusc((n - 1) / 2) + fusc((n + 1) / 2).
Currently, my Python code brute forces through most of the generation, other than the dividing by two part since it will always yield no change.
def fusc (n):
if n <= 1:
return n
while n > 2 and n % 2 == 0:
n /= 2
return fusc((n - 1) / 2) + fusc((n + 1) / 2)
However, my code must be able to handle digits in the magnitude of 1000s millions of bits, and recursively running through the function thousands millions of times does not seem very efficient or practical.
Is there any way I could algorithmically improve my code such that massive numbers can be passed through without having to recursively call the function so many times?

With memoization for a million bits, the recursion stack would be extremely large. We can first try to look at a sufficiently large number which we can work by hand, fusc(71) in this case:
fusc(71) = fusc(35) + fusc(36)
fusc(35) = fusc(17) + fusc(18)
fusc(36) = fusc(18)
fusc(71) = 1 * fusc(17) + 2 * fusc(18)
fusc(17) = fusc(8) + fusc(9)
fusc(18) = fusc(9)
fusc(71) = 1 * fusc(8) + 3 * fusc(9)
fusc(8) = fusc(4)
fusc(9) = fusc(4) + fusc(5)
fusc(71) = 4 * fusc(4) + 3 * fusc(5)
fusc(4) = fusc(2)
fusc(3) = fusc(1) + fusc(2)
fusc(71) = 7 * fusc(2) + 3 * fusc(3)
fusc(2) = fusc(1)
fusc(3) = fusc(1) + fusc(2)
fusc(71) = 11 * fusc(1) + 3 * fusc(2)
fusc(2) = fusc(1)
fusc(71) = 14 * fusc(1) = 14
We realize that we can avoid recursion completely in this case as we can always express fusc(n) in the form a * fusc(m) + b * fusc(m+1) while reducing the value of m to 0. From the example above, you may find the following pattern:
if m is odd:
a * fusc(m) + b * fusc(m+1) = a * fusc((m-1)/2) + (b+a) * fusc((m+1)/2)
if m is even:
a * fusc(m) + b * fusc(m+1) = (a+b) * fusc(m/2) + b * fusc((m/2)+1)
Therefore, you may use a simple loop function to solve the problem in O(lg(n)) time
def fusc(n):
if n == 0: return 0
a = 1
b = 0
while n > 0:
if n%2:
b = b + a
n = (n-1)/2
else:
a = a + b
n = n/2
return b

lru_cache works wonders in your case. make sure maxsize is a power of 2. may need to fiddle a bit with that size for your application. cache_info() will help with that.
also use // instead of / for integer division.
from functools import lru_cache
#lru_cache(maxsize=512, typed=False)
def fusc(n):
if n <= 1:
return n
while n > 2 and n % 2 == 0:
n //= 2
return fusc((n - 1) // 2) + fusc((n + 1) // 2)
print(fusc(1000000000078093254329870980000043298))
print(fusc.cache_info())
and yes, this is just meomization as proposed by Filip Malczak.
you might gain an additional tiny speedup using bit-operations in the while loop:
while not n & 1: # as long as the lowest bit is not 1
n >>= 1 # shift n right by one
UPDATE:
here is a simple way of doing meomzation 'by hand':
def fusc(n, _mem={}): # _mem will be the cache of the values
# that have been calculated before
if n in _mem: # if we know that one: just return the value
return _mem[n]
if n <= 1:
return n
while not n & 1:
n >>= 1
if n == 1:
return 1
ret = fusc((n - 1) // 2) + fusc((n + 1) // 2)
_mem[n] = ret # store the value for next time
return ret
UPDATE
after reading a short article by dijkstra himself a minor update.
the article states, that f(n) = f(m) if the fist and last bit of m are the same as those of n and the bits in between are inverted. the idea is to get n as small as possible.
that is what the bitmask (1<<n.bit_length()-1)-2 is for (first and last bits are 0; those in the middle 1; xoring n with that gives m as described above).
i was only able to do small benchmarks; i'm interested if this is any help at all for the magitude of your input... this will reduce the memory for the cache and hopefully bring some speedup.
def fusc_ed(n, _mem={}):
if n <= 1:
return n
while not n & 1:
n >>= 1
if n == 1:
return 1
# https://www.cs.utexas.edu/users/EWD/transcriptions/EWD05xx/EWD578.html
# bit invert the middle bits and check if this is smaller than n
m = n ^ (1<<n.bit_length()-1)-2
n = m if m < n else n
if n in _mem:
return _mem[n]
ret = fusc(n >> 1) + fusc((n >> 1) + 1)
_mem[n] = ret
return ret
i had to increase the recursion limit:
import sys
sys.setrecursionlimit(10000) # default limit was 1000
benchmarking gave strange results; using the code below and making sure that i always started a fresh interperter (having an empty _mem) i sometimes got significantly better runtimes; on other occasions the new code was slower...
benchmarking code:
print(n.bit_length())
ti = timeit('fusc(n)', setup='from __main__ import fusc, n', number=1)
print(ti)
ti = timeit('fusc_ed(n)', setup='from __main__ import fusc_ed, n', number=1)
print(ti)
and these are three random results i got:
6959
24.117448464001427
0.013900151001507766
6989
23.92404893300045
0.013844672999766772
7038
24.33894686200074
24.685758719999285
that is where i stopped...

Related

Reduce time /space complexity of simple loop

So basically if i have an iteration like this in python
Ive editted the question to include my full code
class Solution:
def myPow(self, x: float, n: int) -> float:
temp = [];
span = range(1,abs(n))
if n ==0:
return 1
if abs(n)==1:
temp.append(x)
else:
for y in span:
if y == 1:
temp = []
temp.append(x*x)
else:
temp.append(temp[-1] * x)
if(n < 0):
return 1/temp[-1]
else:
return temp[-1]
The problem link is : Pow(x,n)-leetcode
How can I modify this to conserve memory and time. Is there another data structure i can use. Im just learning python....
------------EDIT------------
ive modified the code to use a variable instead of a list for the temp data
class Solution:
def myPow(self, x: float, n: int) -> float:
span = range(1,abs(n))
if n ==0:
return 1
if abs(n)==1:
temp = x
else:
for y in span:
if y == 1:
temp = x*x
else:
temp = temp * x
if(n < 0):
return 1/temp
else:
return temp
I still have a problem with my time complexity.
Its working for many testcases, however when it trys to run with x = 0.00001 and n = 2147483647. The time limit issue arises
To reduce the time complexity you can divide the work each time by taking x to the power of 2 and dividing the exponent by two. This makes a logarithmic time algorithm since the exponent is halved at each step.
Consider the following examples:
10^8 = 10^(2*4) = (10^2)^4 = (10*10)^4
Now, there is one edge case. When the exponent is an odd number you can't integer divide it by 2. So in that case you need to multiply the results by the base one additional time.
The following is a direct recursive implementation of the above idea:
class Solution:
def myPow(self, x: float, n: int) -> float:
sign = -1 if n < 0 else 1
n = abs(n)
def helper(x, n):
if n == 1: return x
if n == 0: return 1
if n % 2 == 1:
return helper(x*x, n // 2) * x
else:
return helper(x*x, n // 2)
res = helper(x, n)
if sign == -1:
return 1/res
else:
return res
Note that we have taken abs of the exponent and stored the sign and deal with it at the end.
Instead of iterating from 1 to n, use divide-and-conquer: divide the exponent by 2 and use recursion to get that power, and then square that result. If n was odd, multiply one time more with x:
class Solution:
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 1
if n == 1:
return x
if n < 0:
return self.myPow(1/x, -n)
temp = self.myPow(x, n // 2)
temp *= temp
if n % 2:
temp *= x
return temp
A simple naive solution might be:
def myPow(x: float, n: int) -> float:
## -----------------------
## if we have a negative n then invert x and take the absolute value of n
## -----------------------
if n < 0:
x = 1/x
n = -n
## -----------------------
retval = 1
for _ in range(n):
retval *= x
return retval
While this technically works, you will wait until the cows come home to get a result for:
x = 0.00001 and n = 2147483647
So we need to find a shortcut. Lets' consider 2^5. Our naïve method would calculate that as:
(((2 * 2) * 2) * 2) * 2 == 32
However, what might we observe about the problem if we group some stuff together in a different way:
(2 * 2) * (2 * 2) * 2 == 32
similarly:
((2 * 2) * (2 * 2) * 2) * ((2 * 2) * (2 * 2) * 2) == 32 * 32 = 1024
We might observe that we only technically need to calculate
(2 * 2) * (2 * 2) * 2 == 32
once and use it twice to get 2^10.
Similarly we only need to calcuate:
2 * 2 = 4
once and use it twice to get 2^5....
This suggests a recursion to me.
Let's modify our first attempt to use this divide and concur method.
def myPow2(x: float, n: int) -> float:
## -----------------------
## if we have a negative n then invert x and take the absolute value of n
## -----------------------
if n < 0:
x = 1/x
n = -n
## -----------------------
## -----------------------
## We only need to calculate approximately half the work and use it twice
## at any step.
## -----------------------
def _recurse(x, n):
if n == 0:
return 1
res = _recurse(x, n//2) # calculate it once
res = res * res # use it twice
return res * x if n % 2 else res # if n is odd, multiple by x one more time (see 2^5 above)
## -----------------------
return _recurse(x, n)
Now let's try:
print(myPow2(2.0, 0))
print(myPow2(2.0, 1))
print(myPow2(2.0, 5))
print(myPow2(2.1, 3))
print(myPow2(2.0, -2))
print(myPow2(0.00001, 2147483647))
That gives me:
1
2.0
32.0
9.261000000000001
0.25
0.0
If you have to loop, you have to lope and there is nothing that can be done. Loops in python are slow. That said you may not have to loop and if you do have to loop, it may be possible to push this loop to a highly optimised internal function. Tell us what you are trying to do (not how you think you have to do it, appending elements to a lis may or may not be needed). Always recall the two rules of program optimisation General Rule: Don't do it. Rule for experts: Don't do it yet. Make it work before you make it fast, who knows, it may be fast enough.

Given N, return M that satisfy the equation: N + M = 2 * (N XOR M)

Problem
Given N, return M that satisfy the equation: N + M = 2 * (N ^ M)
Constraints
1 <= Test Cases = 10^5;
1 <= N <= 10^18
I came across this problem in one of the hiring challenges.
By trial and error method, I have found a pattern that - Such an M exists between N/3 and 3N and that N + M is an Even number. So I code it up and upon submission, my solution only managed to pass only half of the test cases. This is not much of an optimisation as this method's time complexity is same as that of Brute force solution.
I know that my solution is not the Optimal solution.
Here's my solution:
def solve(n):
m = n//3
end = 3*n
# If both m and n are odd/even, their sum will be even
if (m&1 == 1 and n & 1 == 1) or (m&1 == 0 and n&1 == 0):
inc = 2
else:
m += 1
inc = 2
while m <= end:
if (n + m) == 2 * (n ^ m):
return m
m += inc
Could someone provide me some hints/methods/algorithm to get an Optimal Solution. Thanks!
The bottom bit of m is determined (since n+m must be even). Given that bottom bit, the next bit is determined, and so on.
That observation leads to this O(log n) solution:
def solve(n):
b = 1
m = 0
while n + m != 2 * (n ^ m):
mask = 2 * b - 1
if ((n + m) & mask) != ((2 * (n ^ m)) & mask):
m += b
b *= 2
return m
Another way to implement this is to find the smallest bit in which m+n and 2*(n^m) differ, and toggle that bit in m. That results in this very compact code (using the new walrus operator, and some bit-twiddling tricks):
def solve(n):
m = 0
while r := n + m ^ 2 * (n ^ m):
m |= r & -r
return m

writing recursive function to compute no. of graphs

I am trying to write a code in python, to calculate the maximum graphs sets by using this formula for n vertices:
2**(n(n-1)/2) (i hope i wrote it correctly)
i am trying to do it in the lowest complexity/running time buy using % of 1000000007- is recursive the right way? or iterative?
I've read the Wikipedia article regarding exponential squaring- but couldn't make the leap from there to my problem :(
after some pen and pen and paper work I've discovered that in this case- n(n-1)/2 is always even - so i am removing the block that deals with odd n values.
This is the code a wrote so far for the recursion-
x for the base (2) and n for the number of vertices:
def graphs_num(x, n):
n = (n*n-n)/2
if n == 0:
return 0
elif n == 1:
return 1
else:
y = graphs_num(x, n/2)
return y*y
no results so far -can you assist please?
2nd edit: (i forgot the x in the last line)
here's #alec's code:
def count(n, total=1):
n = (n*n-1)/2
if n < 2:
return total
total *= 2 ** (n - 1) % 1000000007
return count(x, n-1, total)
count(4)
now I am looking to have x already defined as 2- so the only input need by the function will be n
I'm not completely familiar with this problem but based on the formula you gave this will return the same result for n >= 0 (I assume you wouldn't have a negative number of vertices).
def count(n, total=1):
if n < 2:
return total
total *= 2 ** (n - 1)
return count(n-1, total)
>>> count(5)
1024
Edit (explanation):
Basically you will notice that the values increase by a factor of 2**n.
>>> [2 ** (n * (n-1) // 2) for n in range(1, 6)]
[1, 2, 8, 64, 1024]
This can be illustrated by the following:
f(1) = 1 = 2**0
f(2) = 2 = 2**1 * 2**0
f(3) = 8 = 2**2 * 2**1 * 2**0
...
And simplified to show how it might be used recursively:
f(1) = 1 = 2**0
f(2) = 2 = 2**1 * f(1)
f(3) = 8 = 2**2 * f(2)
...
By this point, it is easy to notice the relationship is f(n) = 2**(n-1) * f(n-1). In the recursive function, the lines underneath the base case do exactly this:
total *= 2 ** (n - 1)
return count(n-1, total)
I multiple the total by 2**(n-1), and recurse to the next value for n, remembering to decrement by 1. The base case ensures we bottom out below 2, and returns the total.
Now if you want to change the base of the exponent and use a mod, it will behave accordingly.
def count(x, n, total=1):
if n < 2:
return total
total *= x ** (n - 1) % 1000000007
return count(x, n-1, total)
>>> count(2, 3)
8
>>> count(3, 3)
27

Sum of range(1,n,2) values using recursion

I'm trying to translate a loop to a recursive algorithm. Fairly simple, I've just hadn't been able to make it ignore the n value when summing up the values, like range does.
This is the iterative function:
def function(n):
total=0
for i in range(1,n,2):
total += i
print(total)
function(5) # Output: 4
This is the recursive I've tried:
def function1(n):
if n==1:
return n
else:
return n+function1(n-2)
function(5) # Output: 9
So function1 does sum the n when it should be ignored. Cause range() does not include the stop number.
Then, I tried:
def f1(n):
def f_recursive(n):
if n==1 or n==2:
return 1
elif n==0:
return 0
else:
return n + f_recursive(n - 2)
return f_recursive(n) - n
print(f1(5)) # Output: 4 Yeiii!!
But then I realised, that only works for odd numbers. Not for even. If f1(6) then you get 4 when it should be 9, because it ends up being 11-6= 9.
So silly me I tried:
def f1(n):
def f_recursive(n):
if n==1 or n==2:
return 1
elif n==0:
return 0
elif n%2 == 0:
return n + f_recursive(n - 3)
elif n%2 == 1:
return n + f_recursive(n - 2)
return f_recursive(n) - n
print(f1(6))
Which of course also did not work. Am I not understanding recursion properly here?
The tricky part is excluding the upper bound. If the upper bound is your only parameter n, you have to know when it's the first call, and when it's an intermediate (recursive) call. Alternatively, if inner functions are okay, you could instead just count from 1 up until you hit n:
def function1(n):
def inner(i):
return 0 if i >= n else i + inner(i + 2)
return inner(1)
You want to compute the sum of all odd integers from 1 up to, but not including, n.
This leaves 2 possibilities:
If n is <= 1, there are no numbers to sum, so the sum is 0.
The highest number that might be included in the list is n-1, but only if it is odd. Either way, the rest of the sum is "the sum of all odd integers from 1 up to, but not including, n-1" (sound familiar?)
This translates to:
def f1(n):
if n <= 1:
return 0
else:
isOdd = (n-1)%2==1
return f1(n-1) + (n-1 if isOdd else 0)
The problem with your recursion is that you're returning n rather than the value in the range (list) that you're currently on, this poses a problem since n is not inclusive within the range and should not be added to the final total
Ideally you need to reverse the logic and traverse it the same way your range does
def func(start,end, step):
if(start >= end):
return 0
return start + func(start + step, end, step)
You just have to recognize the three types of ranges you might be adding up.
range(1, n, 2) where n <= 1: The empty range, so the sum is 0
range(1, n, 2) where n > 1 and n is even: the range is 1, ..., n-1. (E.g. range(1, 6, 2) == [1, 3, 5])
range(1, n, 2) where n > 1 and n is odd: the range is 1, ..., n-2 (E.g., range(1, 5, 2) == [1, 3]
Translating this to code is straightforward:
def f_recursive1(n):
if n <= 1:
return 0
elif n % 2 == 0:
return n - 1 + f_recursive1(n-2)
else: # n odd
return n - 2 + f_recursive1(n-2)
However, this does more work than is strictly necessary, since subtracting 2 from n will never change its parity; you don't need to check n is even or odd in every recursive call.
def f_recursive2(n):
def f_helper(x):
if x <= 0:
return 0
return x + f_helper(x-2)
if n % 2 == 0:
return f_helper(n-1)
else:
return f_helper(n-2)
If we are allowed multiplication and division, I hope you realise that this particular task does not require more than just a base case.
Python code:
def f(n):
total=0
for i in range(1,n,2):
total += i
return total
def g(n):
half = n // 2
return half * half
for n in xrange(100):
print f(n), g(n)
Since
*
* * *
* * * * *
* * * * * * *
can be seen as nested, folded rows. Here are the top two folded rows:
*
* * *
* * * *
* * * *
Let's rotate counterclockwise 45 degrees
* * * *
* * * *
* *
* *
and add the other two folded rows,
*
* *
* * * *
* * * *
* * * *
* * *
and
*
to get
* * * *
* * * *
* * * *
* * * *
the area of a square.

broken memoization code

I have a series of numbers I need to find the sum of. The value of the first iterative operation is 1, the second is 20. Every iteration which follows then uses the previous result in the formula n * (n + 1) / 2, so the third iteration, say i03 = 20 * (20 + 1) / 2, and the fourth, i04 = i03 * (i03 + 1) / 2. This continues until the 20th iteration of i20 = i19 * (i19 + 1) / 2. I want to do this using memoization. This is my code:
def outFun():
def sumFun(squares, total = 0, CONST = 20):
if squares > 2:
total = sumFun(squares - 1) * int((sumFun(squares - 1) + 1) / 2)
elif not squares - 2:
total = CONST
return total
return 1 + sumFun(20)
What am I doing wrong?
Here is how I understand your problem: You have a formula x_n = x_{n-1} * (x_{n-1} + 1)/2 with recursion base defined as x_1 = 20 (or x_2 = 20? Not clear from your description). The most efficient way to solve the recursion is bottom-up approach, when you start with x_1, then calculate x_2, etc. Alternative is to use dynamic programming/memorization:
mem={}
def f(x):
if x == 1: # base case
return 20
if not x in mem: # if we did not calculate it before - calculate
mem[x] = f(x-1) * (f(x-1) +1) / 2
return mem[x] # otherwise return it
print f(1)
print f(2)
print f(3)
prints
20
210
22155
f(20) is a little large to print, so I will print the number of digits in it:
print "number of digits: %s" % len(str(f(20)))
number of digits: 530115
The code took about 9 seconds to run on my desktop:
import timeit
mem={}
print "Execution time: %s" % timeit.Timer("len(str(f(20)))",
setup = "from __main__ import f").timeit(1)
you're calling
sumFun(squares - 1)
twice!
Why not introduce a variable to store the result? Something like:
if squares > 2:
nextResult = sumFun(squares - 1)
total = nextResult * ((nextResult + 1) / 2)

Categories

Resources