Implement a function in OCaml that takes a number as an input and checks whether it is a prime number. The result should be a Boolean value.
Hello,
Before giving answer to the question first check what is prime number?
Ans : Prime number is a number which is divisible by 1 or itself.
Ex; 2,3,5,7
Code:
let prime : int -> bool
= fun number ->
match number with
0 -> false
| 1 -> false
| _ -> let a = (number - 1) in
let rec checkZero a number =
match a with
1 -> true
| _ -> match number mod a with
0 -> false
| _ -> checkZero (a - 1) number
in
checkZero a number
;;
Code Explanation: Step 1: First check if a number is 0 or 1 return false. because they are not a prime number. Step 2: We need to divide an input number, say 17 from values 2 to 17 and check the remainder. If remainder is 0 number is not prime. No number is divisible by more than half of itself. Test Result:

Please let me know if you have any questions in comments section.
Thanks.
# let prime: int -> bool # = fun number -> # match number with # 0 -> false 1-> false >let a = (number - 1) in let rec checkZero a number = match a with 1 -> true .->match number mod a with 0 -> false -> checkZero (a - 1) number in checkZero a number # ;; val prime int -> bool = <fun> # prime 5;; : bool = true # prime 6;; : bool = false
Implement a function in OCaml that takes a number as an input and checks whether it...
2a Please code in Ocaml: TODO: Implement an OCaml function sum_top_4 : int list -> int list, which takes a list of integers and adds the sum of the top four elements to the head of the list (e.g. the input list [1;1;1;1;3;2] should become the output list [4;1;1;1;1;3;2]). starter code: let sum_top_4 (xs : int list) : int list = (* your work here *) [ ]
New to Ocaml, suppose to implement a function which takes two tuples (a,b) (c,d) and determine if a/b equal to c/d. abcd are all int. Stuck on the point that a/b , c/d are different type... Can someone help me implement this small piece of code in Ocaml?
2b Please code in language: Ocaml TODO: Implement a recursive OCaml function ascending : int -> int list, which accepts an integer n as input (again, assume n ≥ 0), and returns a list of integers from 0 to n in ascending order. starter code: let ascending (n : int) : int list = (* your work here *) [ ]
Write a function “isPrime” that takes an int “n” and checks this number to decide if it is an prime number or not. Python answers only since that's the class I am in.
java Write an application that input a number from the user and checks if all digits are prime numbers using method prime. The number entered can be of any size. If all digits are prime your program should stop. Use do/while for reading the input and any loop format to test if the number is prime. When checking the prime numbers don’t use an if to check numbers from 1 – 9; you need to find an algorithm to check...
Write the function deep_contains. It takes as input a number and a list. That list might contain lists as elements, and those lists might contain lists, etc. The deep_contains' function should return a Boolean indicating whether the number is contained inputted list, or a sublist of it, or a sublist of that, and so forth. For example: deep_contains (3, (1,3,5]) returns True deep_contains(3, 11, [3,5]]) returns True deep_contains (3, 1, [[3,4],5),6]) returns True deep_contains (2, (1,3,5]) returns False This function...
1. Write a program that takes a number as input and check whether the number is positive, negative or zero. 2. Write a C++ program that prompts the user to enter a number and checks whether the entered number is even or odd. 3. Using switch-case statement write a C++ program that prompts the user to enter 1, 2 or 3 and display "Red" if selection is 1, "Yellow" if selection is 2, or "Green" if selection is 3. 4. Write...
Write a stored function that takes a number as its input and returns that number as currency, i.e. it should have a leading dollar sign, only have two decimal places, and have commas for the thousands. If the number is negative the result should be in parenthesis, accounting format, -123.45 returns ($123.45). If the input is a non-number, e.g. “Hello”, the function should return $0.00
Write a program that takes a file as input and checks whether or not the content of the file is balanced. In the context of this assignment “balanced” means that your program will check to make sure that for each left bracket there is a closing right bracket. Examples: [ ( ) ] { } à balanced. [ ( ] ) { } à unbalanced. Here is the list of brackets/braces that program must support: (: left parentheses...
Implement a binary search function that takes an integer query key and determines whether it is in a sorted array of n integers. It should return the index of the key, if found, otherwise it should return a negative number. You should implement the function both iteratively and recursively to be sure you really understand the algorithm. Just for the fun of it, you might want to verify the O(log(n)) complexity of your function by counting the number of comparisons...