Question

In: Computer Science

Prime Sum C program !! Dynamically allocated memory Let P(n) denote the sum of the first...

Prime Sum C program !! Dynamically allocated memory
Let P(n) denote the sum of the first n prime numbers. For example, P(1) = 2 and P(3) = 10, since the first three prime numbers are 2, 3 and 5, respectively. Write a program to determine the value of the function P(n) for different values of n. The first few prime sums are 2, 5, 10, 17, 28, 41, 58 and 77. Input The first line of the input file contains a single positive integer, t (t ≤ 20000), representing the number of test cases.
The following t lines contain one positive integer n (n ≤ 10000), representing the prime sum to be computed for the case. Output Write out a single integer on a line by itself for each test case, indicating P(n), for the corresponding input value n.
Sample Input
3
1
6
8
Sample Output
2
41
77
Assignment Details
Even though this assignment can be coded with statically allocated arrays, please write your solution using dynamically allocated arrays.
(Your programs should have either malloc/calloc calls, as well as a call to free.)
In order to efficiently generate the first 10000 primes, please use the Sieve of Eratosthenes. From there, just generate each of the prime sums instead of recomputing for each test case.

Solutions

Expert Solution

The following C code prints the sum of "n" prime numbers using a dynamic array.

There is one hash define (#define LIMIT 10000), that can be replaced by unsigned long LIMIT if the number of prime numbers needed is greater than 6 (then, according to Rosser's theorem, LIMIT would be n*log(n)+n*log(log(n))).

Compile the source code using -lm option as math.h is included.

$ gcc -lm sourceCode.c

Source code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define LIMIT 10000
//unsigned long LIMIT;

void sieve_of_eratosthenes(int P[], int n){
int i;
int j;

for(i=2; i<LIMIT; i++)
    if(P[i])
      for(j=i; i*j<LIMIT;j++)
        P[i*j]=0;
}

int main()
{
    int N=0; //Number
    int T=0; //Test case
    int sum=0; //Sum of prime numbers
    int *P;
    int i;
    int count;
   
    scanf("%d",&T);
  
    while(T--){
       scanf("%d",&N);

        // The following line only works if N>6.
        //LIMIT=N*log(N)+ (N*log(log(N)));

        P = (int *)malloc((sizeof(int)*LIMIT)+1);
        
        if(P==NULL){
            printf("Error! Memory not allocated!");
            exit(0);
        }

        // All values in P are initialized by 1 (index 2 to N)
        for(i=2; i<=LIMIT; i++)
            P[i]=1;

       sieve_of_eratosthenes(P, N);

        count=0; sum=0;
        for(i=2; i<=LIMIT; i++){
          if(P[i]){
            count++;
            sum+=i;
            if(count>=N)
              break;
          }
        }
        free(P);
       printf("%d\n",sum);
    }
  
    return 0;
}


Related Solutions

Let τ (n) denote the number of positive divisors of n and σ(n) denote the sum...
Let τ (n) denote the number of positive divisors of n and σ(n) denote the sum of the positive divisors of n (as in the notes). (a) Evaluate τ (1500) and σ(8!). (b) Verify that τ (n) = τ (n + 1) = τ (n + 2) = τ (n + 3) holds for n = 3655 and 4503. (c) When n = 14, n = 206 and n = 957, show that σ(n) = σ(n + 1).
PYTHON Let n denote an integer entered by the user. Write a program to print n...
PYTHON Let n denote an integer entered by the user. Write a program to print n multiples of 5 in the descending order, with the last number being 5. Print the average of those n multiples
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory so you store that number of integers. Write integers in order starting from 1 until you fill all that memory. Print the addresses and values of the first and the last integer stored in the memory.
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory so you store that number of integers. Write integers in order starting from 1 until you fill all that memory. Print the addresses and values of the first and the last integer stored in the memory. Help ASAP!!!
Write a modified version of the program below. In this version, you will dynamically allocate memory...
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings).   Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory! #include <iostream>...
11.4 Let p be a prime. Let S = ℤ/p - {0} = {[1]p, [2]p, ....
11.4 Let p be a prime. Let S = ℤ/p - {0} = {[1]p, [2]p, . . . , [p-1]p}. Prove that for y ≠ 0, Ly restricts to a bijective map Ly|s : S → S. 11.5 Prove Fermat's Little Theorem
Let X denote a random variable that follows a binomial distribution with parameters n=5, p=0.3
Let X denote a random variable that follows a binomial distribution with parameters n=5, p=0.3, and Y denote a random variable that has a Poisson distribution with parameter λ = 6. Additionally, assume that X and Y are independent random variables. (a) What are the possible values for (X, Y ) pairs. (b) Derive the joint probability distribution function for X and Y. Make sure to explain your steps. (c) Using the joint pdf function of X and Y, form...
Let X denote a random variable that follows a binomial distribution with parameters n=5, p=0.3, and...
Let X denote a random variable that follows a binomial distribution with parameters n=5, p=0.3, and Y denote a random variable that has a Poisson distribution with parameter λ = 6. Additionally, assume that X and Y are independent random variables. What are the possible values for (X, Y ) pairs. Derive the joint probability distribution function for X and Y. Make sure to explain your steps. Using the joint pdf function of X and Y, form the summation /integration...
Let X denote a random variable that follows a binomial distribution with parameters n=5, p=0.3, and...
Let X denote a random variable that follows a binomial distribution with parameters n=5, p=0.3, and Y denote a random variable that has a Poisson distribution with parameter λ = 6. Additionally, assume that X and Y are independent random variables. What are the possible values for (X, Y ) pairs. Derive the joint probability distribution function for X and Y. Make sure to explain your steps. Using the joint pdf function of X and Y, form the summation /integration...
Let E(n) denote the expected return on asset i and B. denote the corresponding beta. In addition, let E(rm) denote the expected
Let E(n) denote the expected return on asset i and B. denote the corresponding beta. In addition, let E(rm) denote the expected return on the market portfolio and Bm denote the corresponding beta. Define and sketch the Security Market Line (SML). Hint: Use E(rm) - r = 8%, r = 3%, B. = 1.25 and B2 = 0.6.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT