Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

All the digits to massive Mersenne primes are contained in text files. Your job

ID: 3678838 • Letter: A

Question

All the digits to massive Mersenne primes are contained in text files. Your job is to write a C program that takes one of these text files as a command line argument and displays how many times each digit appears in the prime number as well as the total number of digits. ZIP files containing such text files can be found at: http://www.mersenne.org/primes/. For example, the most recently discovered Mersenne prime can found at: http:// www.mersenne.org/primes/digits/M74207281.zip. You'll need to download the ZIP file and unzip it to access the text file.

Sample Run:

Command Prompt$ ./a.out M74207281.txt

0s: 2233259

1s: 2233437

2s: 2234193

3s: 2232135

4s: 2232328

5s: 2236279

6s: 2234254

7s: 2233628

8s: 2234257

9s: 2234848

Total: 22338618

Explanation / Answer

#include <iostream>

#include <fstream>//Library included to be able to use file-input feature.

using namespace std;

void getdata(int lst[],int &n)//This function is used to get "n" numbers from input file and asignin them to lst[] array

{                             //Values of lst[] are affected directlly in the main() func. and n is outputted("&" sign).

    ifstream input("input.txt");//define the input function called "input" which uses the file "input.txt"

    input>>n;//assign the first value of file to the variable "n" which stores the number of numbers being processed.

    for(int i=0;i<n;i++) input>>lst[i];//Add each of "n" numbers to the array lst[i]. This loop gets executed "n" times.

}

bool isprime(int num)//This function returns a boolean value for each int number "num" whenever it's prime or not.

{

    for (int i=2; i*i<=num; i++)//The Loop used to prove if the number is prime or not.

      if(num%i==0) return false;//If the number gets fully divided by a smaller number different from itself,funct.

                                //function generates "false"

    return true;//If not , it generates "true" value.

}

bool mersennePrime(int n)//Head of the function which will prove if the number processed is a Mersenne Number or not

{

    n++;//from the formula N=2^n-1,The equation is changed to : N+1=2^n. This is why I incremented the number by one.

while(n%2==0)//While loop used to divide the number by two each time.Whenever it cannot be fully divided,it exits.

{

      n=n/2;

}

if(n==1) return true;//This is the only value of n which cannot be fully divided by two.For ex: 8/2/2/2=1

                        //If the condition is true return "true" value

return false;//If not , return "false" value.

}

int main()

{

    int n,cnt=0,lst[1000]={0};//Two integer variables declared."n" will contain the number of numbers in the list.

        //"cnt" will increment each time a mersenne prime number is found

        //lst[1000]={0} is the initialization of the array which will contain the numbers and whose "positions" get 0

    getdata(lst,n);//Get data function gets called using two variables :"lst" and "n".

    for(int i=0;i<n;i++)//For loop which will check each number of the lst[] array if it's mersenne prime or not.

    {

       if (isprime(lst[i])&&mersennePrime(lst[i])) cnt++;//if both of conditions are fulfilled, increase the counter by one

    }

   cout<<cnt;//output the value of cnt

    return 0;

}

Note-If required modication can be done.