Python: Combinatoric Selections
Python: Combinatoric Selections
Problem 53 @ projecteuler
Completed on: Mon, 1 Jul 2019, 11:32
This is one of the tasks that based on equations. To solve this, first let me Simplify it.
Simplifying the task:
if 1<= n <= 100 , and r <= n
using the equation of C(n,r) is
c(n,r) = n! / (r! *(n-r)!
How many values of c(n,r) greater than one-million
Since we need the factorial of n (n!) and (r!) then I will re-use our function get_factorial(num), and I will write another function to calculate the C(n,r) as in the equation above, I will call it combi(n,r), also I will use a while loop, and we need to set some variables to help us in the execution. In this part, I will use a list called values_over_1m to store all the values in it, although this is Not required in the Problem 53, but i will keep it so later we can print them all to the screen or just get the total values of it. And so we did .. and we get the right answer.
![]() |
The Code:
# Combinatoric selections
# Problem 53 @ projecteuler
# Completed on Mon, 1 Jul 2019, 11:32
”’
if 1<= n <= 100 , and r <= n
using the equation of C(n,r) is
c(n,r) = n! / (r! *(n-r)!
How many values of c(n,r) greater than one-million
'''
# Function to get the Factorials of a number.
def get_factorial(num):
if (num == 0) :
return 1
else:
return num * get_factorial(num-1)
def combi(n,r):
fn = get_factorial(n)
fr = get_factorial(r)
fnr = get_factorial(n-r)
return fn / (fr*fnr)
# To store the values more than 1 million.
values_over_1m =[]
nn = 1
rr = 1
start_value= combi(nn,rr)
stop_me = False
while stop_me == False :
nn +=1
rr =2
while rr < = nn :
start_value = combi(nn,rr)
if start_value >1000000 :
values_over_1m.append([nn,rr])
rr +=1
if nn > 99 :
stop_me = True
print(‘Ther is ‘,len(values_over_1m),’ Values meeting the equation.’)
![]() |