1

I have to write a program that finds out whether or not a number is a perfect square. The terms are I don't use a sqrt function or an exponent (**)

I previously showed my teacher my solution using exponent (**) and she told me not to include that there.

num=int(input("Enter a positive integer: "))
base=1
while num/base!=base:
    base=base+1
if (num/base)%1==0:
    print(num,"is a square")
else:
    print(num,"is not a square")

It works fine with perfect squares but when they're not, it won't work because I can't find a way to get it out of the while loop even though it's not a perfect square.

Ari
  • 43
  • 1
  • 3

7 Answers7

2

You can iterate till finding a value bigger than you number:

You are sure the while will finish since you have a strictly increasing sequence.

def is_perfect_square(x):
    i = 1
    while i*i < x:
        i += 1
    return i*i == x

print(is_perfect_square(15))
# False
print(is_perfect_square(16))
# True
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
2

You have to change

while num/base!=base:

to

while num/base>base:

and it will work.

zomega
  • 1,538
  • 8
  • 26
1

The sum of the first odd integers, beginning with one, is a perfect square. See proof

1 = 1
1 + 3 = 4
1 + 3 + 5 = 9
1 + 3 + 5 + 7 = 16
and so on .....

So here, we can make use of this important fact to find a solution without using pow() or sqrt() in-built functions.

num = int(input("Enter a positive integer: "))
odd = 1

while num > 0:
num -= odd
odd += 2
if num == 0:
print('It is a pefect square')
else:
print('It is not a pefect square')
0

Instead of dividing and taking the remainder, multiply the base and see if it matches the number you are testing. For instance.

for i in range(1, num + 1):
    sq = i * i
    if sq == num:
        print(f"{i} squared is exactly {num}")
        break
    if sq  > num:
        print(f"{num} is not a perfect square")
        break

You might get a better mark if you do something more clever than increment from 1. A binary search would speed things up a lot for large numbers.

Deepstop
  • 3,627
  • 2
  • 8
  • 21
0

I guess it is not a place to answer such questions, but here is a super straightforward solution without using any tricks.

num=int(input("Enter a positive integer: "))
for i in range(num + 1): # in case you enter 1, we need to make sure 1 is also checked
    pow = i * i
    if pow == num:
        print('%d is a pefect square' % num)
        break
    elif pow > num:
        print('%d is not a pefect square' % num)
        break

0

Here is something that I came up with:

from math import *
num = eval(input('Enter a number: '))
sq = sqrt(num)
sq1 = sq%1 #here we find the decimal value and then..
if sq1 == 0.0: #if the value = 0 it is a perfect square else it is not, only perfect 
squares will be whole numbers.
    print(f'{num} is a perfect square')
else:
    print(f'{num} is not a perfect square')
Ruli
  • 2,592
  • 12
  • 30
  • 40
0

Hey Buddy Try This Code,

num=int(input("Enter a positive integer: "
base=1
while num/base>base:
    base=base+1
    if (num/base)%1==0:
        print(num,"is a square")
    else:
        print(num,"is not a square")

It should work I tried it Jai hind jai bharat

Aditya
  • 1,132
  • 1
  • 9
  • 28