简体   繁体   中英

Using iterations to find smallest k in python

I'm new to programming and I'm trying to work on my iterations.

An example of what I'm trying to do is:

Enter a positive integer: 100

19 is the smallest n such that 1+3+5+7+...+n >= 100

So from the integer inserted, sum in steps of 2 from 1 until the sum amounts to the inserted integer.

I'm getting an error message

   while r <= q:
TypeError: '<=' not supported between instances of 'range' and 'int'

I'm not really sure how to do this without using a range and int. Is there a better way?

What I've tried so far is in the code below but essentially: I've tried to create variables and s as a range from 0 to q in steps of 2, then while s is less than or equal to q it continues to sum through n. I put in r to try and get around the error I'm getting but it's still giving the same error.

Maybe I'm thinking about this the wrong way, any help would be greatly appreciated. If I've put enough information, apologies. Let me know and I'll update the question.

    n = 0
    q = int(input("enter a number"))
    s = range(1,q,2)
    r = s
    while r <= q:
    n= n+1
    s= s+n
    print("smallest N is",n)

The answer

    q = int(input("enter a number "))
    s=[1]   #start from 1
    while sum(s) < q: #check if sum of s is less than input
    s.append(s[-1]+2) #s[-1] will get the last element of list and +2 will ensure odd numbers are inserted in list
    print("smallest N is",s[-1]) #get the last element in the list

Works perfectly Thanks everyone for all the answers, really appreciate it. I'm just curious. If I had

Enter a positive integer: 25
8 is the largest k such that 0+2+4+6+...+k < 25

it should get 8 but it gets 10 `

q = int(input("enter a number "))
s=[0]   #start from 0
while sum(s) < q: 
s.append(s[-1]+2) 
print("smallest N is",s[-1])

`

here's a simple example.

q = int(input("enter a number "))
s=[1]   #start from 1
while sum(s) < q: #check if sum of s is less than input
    s.append(s[-1]+2) #s[-1] will get the last element of list and +2 will ensure odd numbers are inserted in list
print("smallest N is",s[-1]) #get the last element in the list

I propose this solution:

q = int(input("enter a number"))
s = 0
r = iter(range(1,q,2))
while not s >= q:
    n = next(r)
    s += n
print("smallest N is",n)

It modifies your version in following points:

  1. I use s for the accumulated sum and initialize it with 0
  2. I make r an iterator which will give me the next value when I run next() on it
  3. I modified the condition of the while loop to run until the sum s if finally greater or equal the number q given from user input
  4. With in the loop i retrieve the next element of the range iterator, save it to n and add it to the sum s . So I remember the last item added before eventually sum s gets greater or equal q

The range function is quite powerful, but you are not using it correctly (and it really isn't necessary here).

positive_integer = int(input('Enter a positive integer: '))
assert positive_integer >= 1
n = 1
sum = 1
while sum < positive_integer:
    n += 2
    sum += n
print('Smallest n is ', n)

Your code is getting an error because it is impossible to say if an integer is equal or less than a range. For example, I can't determine if 5 is less, equal or more than the numbers between 60 and 110. That just isn't how math works. However, you could determine if the number is less, equal or grater than the smallest number in the range. I would suggest you change your code for this:

while r <= min(1, q, 2):
    n += 1
    s += n
print("Smallest N is " + n)
    

q = int(input("enter a number"))
sum = 0;
n=0;
for i in range(1,q,2):
    sum = sum+i
    if(sum >= q):
        n=i
        break

print("smallest N is",n)

This will do

An example may make it easier to understand: Let's say q = 5 Then s = range(1,5,2) (looping over s will generate 1,3) Therefore, s is not 1 specific number but rather 'multiple' numbers. Setting r = s means r is multiple numbers. Checking if r <= q checks if the value of q is bigger or equal as r which is multiple numbers. Therefore, no comparison can be made.

This should ought to do it. I tried to code it a bit clear for interpretation without altering the code to much.

q = int(input("enter a number"))
r = range(1,q,2)
s = 0
n = 0
for i in r:
    n = n + 1
    s = s + i
    if s >= q:
        break
print("smallest N is",i)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM