user10147255
user10147255

Reputation:

Squaring numbers in a loop

I have this python problem:

Write a program that asks the user for a limit, and then prints out the sequence of square numbers that are less than or equal to the limit provided.

Max: 10

1

4

9 ​

Here the last number is 9 because the next square number (16) would be greater than the limit (10).

Here is another example where the maximum is a square number:

Max: 100 1

4

9

16

25

36

49

64

81

100

But I don't exactly know how to do this. So far I have

maximum = int(input("Max: "))

for i in range(1, maximum):

But don't really know how to process the numbers and squaring them.

Thanks

Edit: I have

maximum = int(input("Max: "))

for i in range(1, maximum):
  if i*i <= maximum:
    print(i*i)

Upvotes: 0

Views: 12804

Answers (6)

Sudarshan G
Sudarshan G

Reputation: 21

THE BELOW PROGRAM IS TO FIND SQUARE VALUE OF GIVE NUMBERS

  1. Enter the value you want to find.(Give a range)
  2. the value you give runs and goes to r command.
  3. i have used for loop in this case.
  4. output will be displayed

give the input

maximum = input("Enter Max: ")

r = range(1, maximum)

Square = maximum * maximum

Execute THE loop

for i in r:

if i * i <= Square:

  print (i * i),

Upvotes: 0

KaiserKatze
KaiserKatze

Reputation: 1569

'''
Ask the user input a limit and
convert input string into integer value
'''
limit = int(input("Please input the limit: "))

'''
Extract the squre root of `limit`.
In this way we discard every number (i) in range [0, limit]
whose square number ( i * i ) is not in range [0, limit].
This step improves the efficiency of your program.
'''
limit = int(limit ** .5)

'''
`range(a, b)` defines a range of [a, b)
In order to exclude zero,
we assign `a = 1`;
in order to include `limit`,
we assign `b = limit + 1`;

thus we use `range(1, limit + 1)`.
'''
for i in range(1, limit + 1):
    print(i * i)

Upvotes: 4

Olivier Melan&#231;on
Olivier Melan&#231;on

Reputation: 22314

You got a few good, detailed answers.

But let's also have some fun, here is a one-line solution:

print(*(x**2 for x in range(1, 1 + int(int(input('Limit: '))**(1/2)))))

Upvotes: 2

user10147255
user10147255

Reputation:

I have decided to post the answer that works. Thanks all for the help.

maximum = int(input("Max: "))

for i in range(1, maximum + 1):
  if i*i <= maximum:
    print(i*i)

Upvotes: 1

abarnert
abarnert

Reputation: 365717

First, the simplest change to your existing code is to get rid of that nested loop. Just have the for loop and an if:

for i in range(1, maximum+1):
    if i*i > maximum:
        break
    print(i*i)

Or just have the while loop and increment manually:

i = 1
while i*i <= maximum:
    print(i*i)
    i += 1

One thing: Notice I used range(1, maximum+1)? Ranges are half-open: range(1, maximum) gives us all the numbers up to but not including maximum, and we need to include maximum itself to have all the numbers up to maximum squared, in case it's 1. (That's the same reason to use <= instead of < in the while version.


But let’s have a bit more fun. If you had all of the natural numbers:

numbers = itertools.count(1)

… you could turn that into all of the squares:

squares = (i*i for i in numbers)

Don’t worry about the fact that there are an infinite number of them; we’re computing them lazily, and we’re going to stop once we pass maximum:

smallsquares = itertools.takewhile(lambda n: n<=maximum, squares)

… and now we have a nice finite sequence that we can just print out:

print(*smallsquares)

Or, if you’d prefer if all on one line (in which case you probably also prefer a from itertools import count, takewhile):

print(*takewhile(lambda n: n<=maximum, (i*i for i in count(1)))

But really, that lambda expression is kind of ugly; maybe (with from functools import partial and from operator import ge) it’s more readable like this:

print(*takewhile(partial(ge, maximum), (i*i for i in count(1)))

Upvotes: 2

Deepak Saini
Deepak Saini

Reputation: 2910

I think a while loop may be better suited for this problem.

maximum = int(input("Max: "))

i = 1
while(i*i <= maximum):
   print(i*i) 
   i+=1

Upvotes: 3

Related Questions