Question

Find the number of integers (which may be BigIntegers!) in the BigIntPrimes.txt file that are prime....

Find the number of integers (which may be BigIntegers!) in the BigIntPrimes.txt file that are prime. Report that count as your answer to this quiz.

Again, write your own "isPrime" function that accepts a BigInteger as the parameter. The only BigInteger operations permitted are compareTo, add, mod, and sqrt. You may not use the BigInteger isProbablePrime() method.

this is what we have on the file:

81255
60000
40788
540
87507
50890
11556
54614
63146
4538
5419
39965
65894
57760
33168
55232
22928
16397
67014
96812
57520
82701
92419
30692
8518
83922
87250
26590
13588
49749
23484
60513
87201
52640
74415
31209
43388
61431
88597
8633
47366
17039
78165
60080
69557
64868
62014
67142
16661

Advance java

"Please I need the hold program which is find all the BigInteger on a file and output the number of  BigInteger within that file."

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program screenshots:

Sample output:

Code to copy:

// Import the required packages.

import java.math.BigInteger;

import java.io.*;

// Define the Main class.

class Main

{

    // Define the method to check if the number

    // is prime or not.

    public static boolean isPrime(BigInteger bi)

    {

        // Compute the square root of the number.

        BigInteger root = bi.sqrt();

        // Declare the required variables/objects.

        int i = 2;

        BigInteger divisor = BigInteger.valueOf(i);

        // Start the loop while the value of the divisor

        // is less than or equal to the root.

        while (divisor.compareTo(root) <= 0)

        {

            // Return false if the value of bi mod divisor

            // is equal to 0.

            if (bi.mod(divisor).compareTo(BigInteger.ZERO) == 0)

            {

                return false;

            }

            // Otherwise, increment the value of i and

            // update the value of the divisor with the

            // incremented value of the variable i.

            i = i + 1;

            divisor = BigInteger.valueOf(i);

        }

        // Return true if there are no factors of the

        // given number.

        return true;

    }

    // Define the main() method.

    public static void main(String[] args) throws IOException

    {

        // Create an object of the File class.

        File infile = new File("BigIntPrimes.txt");

        // Open the file in read mode.

        BufferedReader br = new BufferedReader(new FileReader(infile));

        // Declare the required variables.

        String line;

        int count = 0;

        // Start the loop to read the content

        // of the file.

        while ((line = br.readLine()) != null)

        {

            // Create an object of the BigInteger class

            // of the current value.

            BigInteger value = new BigInteger(line);

            // Increment the value of count if the

            // method isPrime() returns true for

            // the current value.

            if (isPrime(value) == true)

            {

                count++;

            }

        }

        // Close the input file.

        br.close();

        // Display the number of primes that are found in the file.

        System.out.println("The number of integers in the BigIntPrimes.txt file that are prime = " + count);

    }

}

Sample input file ("BigIntPrimes.txt"):

81255

60000

40788

540

87507

50890

11556

54614

63146

4538

5419

39965

65894

57760

33168

55232

22928

16397

67014

96812

57520

82701

92419

30692

8518

83922

87250

26590

13588

49749

23484

60513

87201

52640

74415

31209

43388

61431

88597

8633

47366

17039

78165

60080

69557

64868

62014

67142

16661

Add a comment
Know the answer?
Add Answer to:
Find the number of integers (which may be BigIntegers!) in the BigIntPrimes.txt file that are prime....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • 1l. modulo p -251 (which is a prime number) and base r - 2. Suppose then use Eulers are a 27 and ...

    1l. modulo p -251 (which is a prime number) and base r - 2. Suppose then use Eulers are a 27 and b-28. Find their shared key. (Hint: You mamo n (10 points) Suppose Alice and Bob want to use Diffie-Hellman method with the published uppose their private componen If a and n are relatively prime nonzero integers, then an(mod mad P つ「 1l. modulo p -251 (which is a prime number) and base r - 2. Suppose then use...

  • JAVA please! Prime numbers are interesting numbers. A prime number is one that is only divisible...

    JAVA please! Prime numbers are interesting numbers. A prime number is one that is only divisible by 1 and itself. For hundreds of years mathematicians have looked for the largest prime number. In the year 1456 the largest known prime was 8191. By the year 1588 it was a 6-digit number: 131,071. The famous mathematician Leonhard Euler discovered the 10-digit number 2,147,483,647 in 1772. Over the years larger and larger primes have been found. There are contests to find the...

  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

  • Your assignment is to write a grade book for a teacher. The teacher has a text file, which includ...

    Your assignment is to write a grade book for a teacher. The teacher has a text file, which includes student's names, and students test grades. There are four test scores for each student. Here is an example of such a file: Count: 5 Sally 78.0 84.0 79.0 86.0 Rachel 68.0 76.0 87.0 76.0 Melba 87.0 78.0 98.0 88.0 Grace 76.0 67.0 89.0 0.0 Lisa 68.0 76.0 65.0 87.0 The first line of the file will indicate the number of students...

  • Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...

    Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

  • Need Help! Please Solve using JAVA! TopResult Task: There are a number of situations in which we ...

    Need Help! Please Solve using JAVA! TopResult Task: There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is closest to the end (or the front) of a list, etc. We can imagine writing a simple class which will do this: a class which will let us enter new results one by one, and at any point will...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT