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?
Given function actually finds whether given integer is a positive prime number or not, if its apositive prime number it returns 1 and 0 otherwise.
first line [if (n <= 1)] checks whether given integer is less than 1 then it returns 0.
second line [if(n%2==0 && n>2)] checks for even number greater than 2 and returns 0 for those even numbers.
Now consider the bold 3 lines of code:
for loop starts with value of i=3, each iteration it increments i by 2 until n/2.
i.e. i values are 3,5,7,...n/2
In each iteration it checks whether given number divides i, so its not prime and returns 0.
Now after termination of for loop all cases to check nnot prime is over so the number is prime and returns 1.
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...
in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){ sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() { int score, highest; //missing portion of the program return 0; } 1(c). Find the missing portion of the following code, so the...
The language has to be in C program 1. (a) rite a function, int factorial (int n), which returns n (the factorial of n, i e. 1 x2 x3 x...xn.) (b) Using int factorial (int n) above, write a program to compute 2. We want to find one of the roots of a cubic equation given by which is between 0 and 1 by an iterative method. Modify the equation above to 1 x Start with an appropriate initial value...
Question 1 In the following incomplete C program: #include stdlib.h> #include <stdio.h> int main () { int i, n, max; int array[100]; ... } return 0; } an array of random int values is populated with n random values. Write only the code to find the location of the maximum value in the array. Question 2 Follow these instructions in your Linux account: 1. Create a file called data.txt in the root folder in your account. 2. Create a new...
#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...
(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 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
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...