Question

Write an OCaml function named is_square that takes as input an integer, say n, (which may...

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,

  • count_squares [2;3;5] = 0
  • count_squares [1;2;3;4;5;6;7;8;9] = 3

Homework Answers

Answer #1
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:

  • OCaml: This function is used to to check if given number is perfect square
  • count_squares: function to count the number of elements in the list which are perfect square

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write an Oz program (having an Oz recursive function/procedure) which takes in the input a list,...
Write an Oz program (having an Oz recursive function/procedure) which takes in the input a list, say L, and two positive integers, M and N. The function should return another list L’, which represents the sub-list of L containing all the elements from index M to index N in reverse order. For example, if the input list is [a 2 g 5 4 k] and M is 2 and N is 5, then the output list should be [4 5...
In R- Studio : Write a function that takes as an input a positive integer and...
In R- Studio : Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Write a recursive function, do not use any of the loop commands in your code.
design an algorithm in a c++ function that takes as input a list of n integers...
design an algorithm in a c++ function that takes as input a list of n integers and find the location of the last even integer in the list or returns 0 if there are no even integers in the list
In C++ Using recursion write a program that takes a positive integer number and returns whether...
In C++ Using recursion write a program that takes a positive integer number and returns whether there is 6. For example, if the input number is 7068, the function returns true
C programing Write a function that takes in an integer as input and returns an integer...
C programing Write a function that takes in an integer as input and returns an integer array of zeros.
Write a function called TaylorSin.m that takes as input an array x, and positive integer N,...
Write a function called TaylorSin.m that takes as input an array x, and positive integer N, and returns the Nth Taylor polynomial approximation of sin(x), centered at a = 0. The first line of your code should read function s = TaylorSin(x,N) HINT: in computing k!, use kfact = k*(k-1)*kfact since you are counting by 2
Write a function called odd_rms that returns orms, which is the square root of the mean...
Write a function called odd_rms that returns orms, which is the square root of the mean of the squares of the first nn positive odd integers, where nn is a positive integer and is the only input argument. For example, if nn is 3, your function needs to compute and return the square root of the average of the numbers 1, 9, and 25. You may use built-in functions including, for example, sum and sqrt, except for the built-in function...
Write a function called odd_rms that returns orms, which is the square root of the mean...
Write a function called odd_rms that returns orms, which is the square root of the mean of the squares of the first nn positive odd integers, where nn is a positive integer and is the only input argument. For example, if nn is 3, your function needs to compute and return the square root of the average of the numbers 1, 9, and 25. You may use built-in functions including, for example, sum and sqrt, except for the built-in function...
In C programming language write a function that takes an integer n as input and prints...
In C programming language write a function that takes an integer n as input and prints the following pattern on the screen: 1 (n times) 2 (n-1 times) .n (1 time) For example, if n was 5, the function should print 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
C Programming: Write a function that takes in an array of integers and an integer containing...
C Programming: Write a function that takes in an array of integers and an integer containing the count of elements, then have it returns the sum of all the even values inside the array.