In this question I wanted to find min(m+k) such that mCk =n, I wrote a code to brute force for my sequence, The problem was that my code isn't fast enough so I want to decrease the length of the for loop as k
progress
import math
print("the input is the maximum number $n$ to check for i.e for all u between 1, n the code gives the min m+k suck that mCk =n , the output is in the for of $(n , min (m+k : mCk=n)) $")
u=int(input())
for n in range (2,u+1):
C=[]
stop=1
l=0
for m in range (1, n+1):
s=0
if stop==-1:
break
for k in range(1, math.floor(m/2)+1-l):
if math.comb(m,k)>n:
l=l+1
break
if math.comb(m,k)==n:
C.insert(s,m+k )
s=s+1
if min(C)<n+1:
stop=-1
break
print(f"({n},{min(C)}), ")
These are the first initial values that the code gives: ( 2, 3), ( 3, 4), ( 4, 5), ( 5, 6), ( 6, 6), ( 7, 8), ( 8, 9), ( 9, 10), ( 10, 7), ( 11, 12), ( 12, 13), ( 13, 14), ( 14, 15), ( 15, 8), ( 16, 17), ( 17, 18), ( 18, 19), ( 19, 20), ( 20, 9), ( 21, 9), ( 22, 23), ( 23, 24), ( 24, 25), ( 25, 26), ( 26, 27), ( 27, 28), ( 28, 10), ( 29, 30), ( 30, 31), ( 31, 32), ( 32, 33), ( 33, 34), ( 34, 35), ( 35, 10)
The problem is writing something like this :
k=0
for i in range (0,100-k+1):
if i%2==0
k=k+1
doesn't decrease the length of the range , which something I need to do with my code
I tried to search for solutions to this problem but I only found unrelated stack overflow questions.
You have:
k=0
for i in range (0,100-k+1):
if i%2==0
k=k+1
Ultimately you cannot use the range
function. Since k
is 0 initially, your range
function could have been simplified to range(0, 101)
. In your loop's body you have k=k+1
. It appears your intention is to reduce the ending value of your range by 1. So we have:
i = 0
max_i = 101
while i < max_i:
print(i) # For demo purposes
if i % 2 == 0:
max_i -= 1 # Reduce max_i
i += 1 # Increment i for next iteration
This will print out 0, 1, 2, ... 66