Use Standard ML code (SML) 1.Write a function Factors that for a given natural n
ID: 2247184 • Letter: U
Question
Use Standard ML code (SML)
1.Write a function Factors that for a given natural number N , and numbers low and high , returns a list of the numbers between
low and high (both included) that divide N.
2. Write a function CoPrimethat for two given natural numbers p and q returns true if p and q are co-prime (have no factors in common except 1).
Example inputs and outputs:
Factors (24 ,3 ,9);
v a l i t = [ 3 , 4 , 6 , 8 ] : i n t l i s t
CoPrime (24 ,25);
v a l i t = true : bool
CoPrime (22 ,33);
v a l i t = f a l s e : bool
Explanation / Answer
Hi...
#include <stdio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;
printf("Enter an integer: ");
scanf("%d",&n);
// show error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else
{
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}
2.--------------------
/**
*************************************************************************
* Description:
*
* Given a 2 * N Grid of numbers, choose numbers such that the sum of the
* numbers is maximum and no two chosen numbers are adjacent horizontally,
* vertically or diagonally, and return it.
* Example:
* Grid:
* 1 2 3 4
* 2 3 4 5
* so we will choose 3 and 5 so sum will be 3 + 5 = 8
*
* Note that you can choose more than 2 numbers
*
*************************************************************************
* @date : Aug 3, 2015
* {@link http://www.interviewbit.com/courses/programming/topics/dynamic-programming/problems/adjacent/ }
*/
package _04_Adjacent;
import java.util.ArrayList;
/** see test {@link _04_Adjacent.SolutionTest } */
public class Solution {
public int adjacent(ArrayList<ArrayList<Integer>> a) {
if (a.size() == 0 || a.get(0).size() == 0) {
return 0;
}
// initialize
int cols = a.get(0).size();
int offset = 3;
int[] dp = new int[cols + offset];
int result = 0;
// dp
for (int i = 0; i < cols; i++) {
int max = Math.max(a.get(0).get(i), a.get(1).get(i));
int count = max + Math.max(dp[i], dp[i + 1]);
dp[i + offset] = count;
result = Math.max(count, result);
}
return result;
}
tthanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.