Uneven Sharing in Python Performance Algorithm - python

A group of k boys, should be paid marbles as follows: they sit in a circle, and each boy will take 1 marble more than the boy to his right just did, and pass the bag with the remainder of the marbles to the boy on his left.
The leader starts by taking out 1 marble for himself. He passes the bag to the boy on his left, who then removes 2 marbles for himself, and passes the bag to his left. That boy then takes 3 coins and passes the bag on to his left, and so on. This process continues until the bag is empty (the last boy to take from the bag may not get as many marbles as he should).
I want to get the total number of marbles that the LEADER receives at the end of the process.
This is what I have, it works but it is too slow:
def countMarbles(n, k):
c = 0
leader = 0
while n>0:
for i in range(k):
c+=1
if i == 0:
if c<=n:
leader += c
else:
leader += n
n -= c
return leader

The marbles you are speding are 1, then 2, then 3...
There is a formula for this kind of sum, the sum of 1 to x is (x)(x + 1) / 2.
Now you are given n, and want to know how many passes of the bag you can make. This means getting highest x such that (x)(x + 1) / 2 is lower or equal to n.
We can get this by solving 0 = x^2 + x - 2n. We might get a decimal result there, so we should take the floor value of the positive answer to the equation.
Once we found the correct x, we just know that every k passes of the bag, 1 goes to the leader. He first gets 1 marble, then he gets k + 1 marbles, then 2k + 1...
If there were x passes, ceil of x / k went to the leader. Taking out the first pass which is always 1, we get l = ceil(x / k) - 1 passes that have a k coefficient greater than 0: ((k + 1) + (2k + 1) + ... + (lk + 1)) = (1 + 2 + 3 + ... + l) * k + l = (l * (l + 1) / 2) * k + l.
Considering leader started with 1, the solution is (l * (l + 1) / 2) * k + l + 1
The only problem is what happens with the remainding marbles that were left in the bag. In the case that those should have gone to leader, we also need to take them into account. For that to happen, x must be a multiple of k, meaning that we finished the round so the next should have been leader, but there were not enough marbles to make another pass.
Here is a python implementation:
import math
def solve (n, k):
x = math.floor((-1 + math.sqrt(1 + 8*n)) / 2)
l = math.ceil(x / k) - 1
sol = (l * (l + 1) / 2) * k + l + 1
if x % k == 0 :
sol += n - (x * (x + 1) / 2)
return int(sol)

Related

Count Number of Ways

Assume you have a fruit and some vegetables. You gain 1 calorie from eating a vegetable. You can eat any number of vegetables to compensate the remaining gain from eating a fruit or no fruit.
Find the no of ways in which you can eat to gain exactly n calories.
m - no. of calories gained by eating a fruit
def totalWays(n, m):
lookup = [1,1,2] + [0]*(n-2)
for i in range(3, n + 1):
j = 1
while j <= m and (i - j) >= 0:
lookup[i] += lookup[i - j]
j = j + 1
return lookup[n]
for example, totalWays(5,3) should gives us 4 meaning we can have vvvvv, vvf, vfv, fvv instead it gives 13. I think instead of taking vegetable gain as 1, it is assuming all possible values like 3vvv,2v2vv, v2v2v...
Can someone help where I'm going wrong ?!
If only one fruit is allowed, then you can either eat one fruit or eat no fruits.
a) eating no fruits
The # of results is 1, as it is v n times -> vvvvv for n=5, etc.
b) eating one fruit
The # of results is n - m + 1, because you remove m vs from the previous case, leaving n - m vs, which yields n - m + 1 ways to place the f.
Therefore, if n is larger than or equal m, the result is n - m + 2. For n < m, you cannot substitute the vegetables for a fruit, and the result is then always 1.
def total_ways(n, m):
if n < m:
return 1
else:
return n - m + 2

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

Project 2 Human Pyramid Calculations

For simplicity we will assume that everyone in the pyramid weighs exactly 200 pounds. Person
A at the top of the pyramid has no weight on her back. People B and C are each carrying half of
person A's weight. That means that each of them is shouldering 100 pounds.
Now, let's look at the people in the third row. Let’s begin by focusing on person E. How much
weight is she supporting? Well, she’s directly supporting half the weight of person B (100
pounds) and half the weight of person E (100 pounds), so she’s supporting at least 200 pounds.
On top of this, she’s feeling some of the weight that people B and C are carrying. Half of the
weight that person B is shouldering (50 pounds) gets transmitted down onto person E and half
the weight that person C is shouldering (50 pounds) similarly gets sent down to person E, so
person E ends up feeling an extra 100 pounds. That means she’s supporting a net total of 300
pounds.
Write a recursive function (using no loops), weightOn(r,c), which returns the weight
on the back of the person in row r and and column c. Rows and columns are 0-based,
so the top position is (0,0), for example, and person H is in position (3,1). The following
also hold:
weightOn(0,0) == 0.00
weightOn(3,1) == 425.00
Weights should be floating-point numbers.
I have already tried a lot. I will include my most recent code below.
t = 0.0
x = 0.0
def weightOn(r, c):
global t
if r < 0:
print('Not valid')
elif r == 0 and c == 0:
return t
elif r > 0 and c == 0:
t += 200 / (2 ** r)
return weightOn(r - 1, 0)
elif r > 0 and c == r:
t += 200 / (2 ** r)
return weightOn(r - 1, 0)
elif r > c > 0:
mid(r, c)
return t
def mid(r, c):
global x
x = weightOn(r - 1, c - 1) + weightOn(r - 1, c)
'''I have also tried: x = (((weightOn(r - 1, c - 1) + 200) / 2) + ((weightOn(r - 1, c) + 200) / 2))'''
return x
r = int(input('r: '))
c = int(input('c: '))
weightOn(r, c)
if r > c > 0:
print(x)
else:
print(t)
It always brings up the wrong output. I can correctly pull up all of the edges (when c == 0 or c == r). But other than that it won't work.
Ex. Input (3, 1) outputs 500
(3, 2) outputs 550
Using global variables suggests that you haven't considered this recursively.
Each person shoulders half the weight of the persons on each shoulder. The effective weight of each person is what they shoulder, plus 200 pounds. If a person is on the edge, then the "person" on the other shoulder has 0 weight.
So ...
def weight(r, c):
# Code base cases
if r < 0: # Past the pyramid top; no such person
return 0
if c < 0 or c > r: # Off the edge; no such person
return 0
return 200 + (weight(r - 1, c - 1) + weight(r - 1, c)) / 2
Then weightOn is simply the above routine without the 200 +.
That's your outline; can you take it from there?
def weight_on (r,c):
second_person = 200 #finds out if there is a second person on top or not
if c - 1 < 0 or c > r - 1 :
second_person = 0
if c < 0 or c > r:
return 0
elif r <= 0:
return 0
else:
return (second_person + 200 + weight_on (r - 1,c - 1) + weight_on (r - 1,c))/2

Efficiently generating Stern's Diatomic Sequence

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...

Alternating counters in Python (Fibonacci Plot)

I've been assigned a project in my computing class to do a report on some area of mathematics in LaTeX, using Python 2.7 code - I chose the Fibonacci sequence.
As part of my project I wanted to include a plot of the Fibonacci 'spiral' which is actually comprised of a series of quarter-circles of increasing radii. As such, I've tried to define a function to give a loop that returns the centres of these quarter-circles so I can create a plot. Using pen and paper I have found the centres of each quarter-circle and noticed that with each new quarter-circle there's an exchange of coordinates - ie. if n is even, the x-coordinate of the previous centre remains the x-coordinate for the nth centre; similarly, when n is odd, the y-coordinate remains the same.
My problem arises with the other coordinate. They work on an alternating pattern of + or - the (n-2)th Fibonacci number to the y-coordinate (for even n) or x-coordinate (for odd) of the previous centre.
I've created the following loop in SageMathCloud, but I think I've deduced that my counters aren't incrementing when I wanted them to:
def centrecoords(n):
k = 0
l = 1
if fib(n) == 1:
return tuple((0,-1))
elif n % 2 == 0 and k % 2 == 0:
return tuple((centrecoords(n-1)[0], centrecoords(n-1)[1] + ((-1) ** k) * fib(n - 2)))
k += 1
elif n % 2 == 0:
return tuple((centrecoords(n-1)[0], centrecoords(n-1)[1] + ((-1) ** k) * fib(n - 2)))
elif n % 2 != 0 and l % 2 == 0:
return tuple((centrecoords(n-1)[0] + ((-1) ** l) * fib(n - 2), centrecoords(n-1)[1]))
l += 1
else:
return tuple((centrecoords(n-1)[0] + ((-1) ** l) * fib(n - 2), centrecoords(n-1)[1]))
cen_coords = []
for i in range(0, 21):
cen_coords.append(centrecoords(i))
cen_coords
Any help in making the k counter increment with its if statement only, and the same with the l counter would be greatly appreciated.
Your problem is that k and l are local variables. As such they are lost every time the function exits, and re-start at zero and one respectively when is called again (yes, even when it's called from itself).
Nick's code aims to store a single instance each of k and l in the top-level function, sharing them with the recursive calls.
Another reasonable approach might be to rewrite your recursion as a loop, and yield the sequence. This makes it trivial to keep the state of k and l, as your locals are preserved.
Or, you could re-write your function as a class method, and make k and l instance variables. This behaves similarly, with the instance storing your intermediate state between calls to centrecoords.
Apart from all of these, your code looks like it requires each call to centrecoords to receive the next value of n. So, even if you fix the state problem, this is a poor design.
I'd suggest going the generator route, and taking a single argument, the maximum value of n. Then you can iterate over range(n), yielding each result in turn. Note also that your only recursive call is for n-1, which is just your previous iteration, so you can simply remember it.
Quick demo: I haven't tested this, or checked the corner cases ...
def fib(n):
if n < 2:
return 1
return fib(n-1) + fib(n-2)
def centrecoords(max_n):
# initial values
k = 0
l = 1
result=(0,-1)
# note fib(0) == fib(1) == 1
for n in range(2,max_n):
if n % 2 == 0:
result = (result[0], result[1] + ((-1) ** k) * fib(n - 2))
yield result
if k % 2 == 0:
k += 1
else:
result = (result[0] + ((-1) ** l) * fib(n - 2), result[1])
yield result
if l % 2 == 0:
l += 1
cen_coords = list(centrecoords(21))
Expanding on my comment. Your code could look something like the one below. But please not that you might need to adjust starting values of k and l to -1 and 0 correspondingly, because k and l are incremented before recursion calls (opposite to your code which implied that first a recursion is called and only then k and l are increased).
I also deleted tuple, it is unnecessary in python and hard to read, to create a tuple use comma syntax, e.g.: 1, 2.
Also n == 0 (fib(n) == 0) should be considered as special case, or you program will enter infinite recursion and crash when centrecoords called with n=0.
I have no account on SageMathCloud to test it, but it at least should fix counters increment.
def centrecoords(n, k=0, l=1):
if n == 0:
return 0, 0 # this is pure guess and most likely incorrect, but n == 0 (or fib(n) == 0 should be handled separatly)
if fib(n) == 1:
return 0, -1
elif n % 2 == 0 and k % 2 == 0:
k += 1
return centrecoords(n-1, k, l)[0], centrecoords(n-1, k, l)[1] + ((-1) ** k) * fib(n - 2)
elif n % 2 == 0:
return centrecoords(n-1, k, l)[0], centrecoords(n-1, k, l)[1] + ((-1) ** k) * fib(n - 2)
elif n % 2 != 0 and l % 2 == 0:
l += 1
return centrecoords(n-1, k, l)[0] + ((-1) ** l) * fib(n - 2), centrecoords(n-1, k, l)[1]
else:
return centrecoords(n-1, k, l)[0] + ((-1) ** l) * fib(n - 2), centrecoords(n-1, k, l)[1]
cen_coords = []
for i in range(0, 21):
cen_coords.append(centrecoords(i))
cen_coords

Categories

Resources