Write an OCaml function named is_square that takes as input an integer, say n, (which may be positive or negative) and returns true if that number is square. That is, it returns true if there exists an an integer, say m, if m * m = n.
Next, write a function named count_squares that calls count from above to determine the number of squares in a list of integers. For example,
import math
# function to check if given number is perfect square
def OCaml(x):
# Find floating point value of
# square root of x.
sr = math.sqrt(x)
# If square root is an integer
return ((sr - math.floor(sr)) == 0)
# function to count the number of elements in the list which are perfect square
def count_squares(lst):
cnt=0
for i in lst:
if i<0:
continue
if (OCaml(i)):
cnt+=1
print('Number of perfect squares: ')
print(cnt)
# Driver code
x = int(input('Enter the number: '))
if x<0:
print("No")
elif (OCaml(x)):
print("Yes")
else:
print("No")
lst = []
# number of elemetns as input
n = int(input("Enter number of elements : "))
print('Enter the numbers to be inserted in the list')
# iterating till the range
for i in range(0, n):
ele = int(input())
lst.append(ele) # adding the element
print('\n')
count_squares(lst)
I have made two functions:
The first inputted number is checked for being a perfect square.
then, a list of numbers are inputted and then count of numbers among them are found which are perfect squares.
PLEASE LIKE THE SOLUTION :))
IF YOU HAVE ANY DOUBTS PLEASE MENTION IN THE COMMENT
Get Answers For Free
Most questions answered within 1 hours.