int is_prime(int n)
{
if (n <= 1) return 0;
if (n % 2 == 0 && n > 2) return 0;
for (int i = 3; i < n / 2; i += 2)
if (n % i == 0)
return 0;
return 1;
}
What do the three lines in this function mean?
Program:
import java.io.*;
import java.util.Scanner;
public class TestPrime { // Here taking class name as
TestPrime
public int is_prime(int n) { // Definition of the method
is_prime()
/*if (n <= 1)
return 0; //The above condition is true then returning 0 to the
main()
if (n % 2 == 0 && n > 2)
return 0; //The above condition is true then returning 0 to the
main() */
for (int i = 3; i < n / 2; i += 2)
if (n % i == 0) //Here checking the condition if remainder is
zero
return 0; //The above condition is true then returning 0 to the
main()
return 1;
}
public static void main(String args[]) // Start of main()
{
int m=0; // Here initializing the value of integer varaible called
yr
TestPrime ob = new TestPrime(); // Here creating the object for
class TestPrime
Scanner in = new Scanner(System.in);
System.out.println("Enter Value of n: "); // Here taking Input from
the User
m = in.nextInt(); // Here scanning the input
System.out.println(ob.is_prime(m)); // Here calling the leapyear()
method
}// End of main()
} // End of class TestPrime
Output:
C language int is_prime(int n) { if (n <= 1) return 0; if (n % 2 == 0 && n > 2) return 0; for (int i = 3; i < n / 2; i += 2) if (n % i == 0) return 0; return 1; } What do the three lines in this function mean?
(a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0) return 0; 3 return (n-1 + g(n-1)); 4} (b) Consider the following C++ function: 1 bool Function (const vector <int >& a) { 2 for (int i = 0; i < a. size ()-1; i ++) { 3 for (int j = i +1; j < a. size (); j ++) { 4 if (a[i] == a[j]) return false; 5 6 } 7 return...
How to prove G(n)=n+1 in this algorithm? 1. if (n 0) 2. return 1 3. else if (n1) f 4. return 2 5. else if (n 2) 6. return 3 7. else if (n3) t 8. return 4 else f 9. int OGnew int[n 11 10. G[O]1 12. G[2]3 13. G[3]4 14. int i:-4 15. while (i<n) t 16. if (i mod 20) else ( 20. return G[n] 1. if (n 0) 2. return 1 3. else if (n1) f...
Int h(int n) { if (n == 0) return 0; else return h(n/10) + n%10;} What is the output for each function call (a) Function call h(736). Output: (b) Function call h(12345). Output: Answer options: 16,15 26,25 36,35 46,45
#include <stdio.h> int josephus(int n, int k) { if (n == 1) return 1; else /* The position returned by josephus(n - 1, k) is adjusted because the recursive call josephus(n - 1, k) considers the original position k%n + 1 as position 1 */ return (josephus(n - 1, k) + k-1) % n + 1; } // Driver Program to test above function int main() { int n = 14; int k = 2; printf("The chosen place...
The following function is_prime() is not a very efficient prime number test: #include <stdio.h> int is_prime(int n) { int d; for (d = 2; d < n; d++) { if (!(n % d)) return 0; } return 1; } int main() { if (is_prime(7)) printf("Seven is Prime!\n"); return 0; } It is unnecessary to divide n by all numbers between 2 and n - 1 to determine if n is prime. Only divisors up to and including need to be...
b) Consider the following code. public static int f(int n) if (n == 1) return 0; else if (n % 2 == 0). return g(n/2); else return g(n+1); public static int g(int n) int r = n % 3; if (r == 0) return f(n/3); else if (r == 1) return f(n+2); else return f(2 * n); // (HERE) public static void main(String[] args) { int x = 3; System.out.println(f(x)); (1) (5 points) Draw the call stack as it would...
Q5 (25pts) Consider the code: int foo(int N){ if (N <= 3) return 2; int res1 = 3*foo(N-4); int res2 = foo(N-2); return res1-res2; } a) (6 points) Write the recurrence formula for the time complexity of this function (including the base cases) for N>=0. You do NOT need to solve it. b) (5 points) Draw the tree that shows the function calls performed in order to compute foo(8) (the root will be foo(8) and it will have a child...
(c) int sum(int n) un { int sum=0; for (int i=0; i<n; i++) for(int j=0; j<i/2; j++) for(int k=0; k<min(j,5); k++) { sum=sum+1; } return sum; }
How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...