//Charles Lillo
//3223H-201
//Primesum
//8/29/11

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

int main ()
{
    FILE * ifp;
    FILE * ifo;
    int * array_num;
    double * array_sum;

    ifp = fopen("primesum.in","r");
    ifo = fopen("primesum-out.txt","w");

    int runtime=0;

    fscanf (ifp,"%d",&runtime);//reads number of times to run
    array_num = (int*) malloc(runtime*sizeof(int)); //declares an array to store values in input

    int fill,find,max=0,num;

    //fills array with numbers in input file
    for (fill = 0; fill<runtime; fill++)
    {
        fscanf (ifp,"%d",&num);
        array_num[fill]= num;
    }

    // Finds the maximum value in the input file.
    max = array_num[0];
    for(find=0; find<runtime; find++)
    {
        if(array_num[find]>max)
            max=array_num[find];

    }

    // Allocate space to store all the sums.
    array_sum = (double*) malloc(max*sizeof(double));//declares the summation array at the max value

    int sum_count,count=2,found=0;

    double sum=0;

    while(found<max)//finds and stores all sums up to max value
    {

        if( count==2 ||count==3||count==5||count==7)//test case for first 4 primes
        {

            sum+=(1.0/(count*1.0));
            array_sum[found+1]=sum;
            found++;


        }

        else if ((count%2!=0) && (count%3!=0) && (count%5!=0) && (count%7!=0))//sieve for next primes and pseudoprimes
        {

            sum+=(1.0/(count*1.0));
            array_sum[found+1]=sum;
            found++;
        }

        count++;
    }

    int printer=0;

    for(printer=0; printer<runtime; printer++)//prints to output file
    {
        fprintf(ifo,"Sum of %d terms is approximately %.9lf.\n",array_num[printer],array_sum[array_num[(printer)]]);
    }

    free(array_sum);
    free(array_num);
    fclose(ifp);
    fclose(ifo);
    return 0;
}
