Question

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 largest prime. Over the years there have been prizes of $50,000, $100,000, and $150,000 awarded for finding the biggest prime number. Currently the largest prime number is 282,589,933 − 1 with 24,862,048 digits, found in December 2018. This project is to determine if a 10-digit phone number is a prime number. If the number is a prime number, then print a message to that effect. If the number is not a prime number, then print the prime factors of the number. Allow the user to continue entering numbers for as long as he or she wishes. We will discuss possible algorithms and determine the most efficient one to be used by everyone. This project is a two-part project. The first part is to write a program that will create a list of prime factors that can be stored in an ArrayList. This list will be sent to a file. The second part of the project is to write a program that will read from a file the list of prime factors created in part one and use it to determine if the phone number is prime and if not, what are its prime factors. The program will work with positive numbers up to 10 billion. The largest positive 32-bit integer is not large enough to hold a 10-digit phone number. The largest positive 32-bit integer is 2,147,483,647. To hold a 10-digit phone number we will need to use a long which is a 64-bit integer which has a positive range to 9,223,372,036,854,775,807. This code will read a long value: System.out.println("please enter a 10-digit phone number");

long phoneNumber = scan.nextLong();

This code will read the prime factors from the file and put them in an ArrayList.

while(scanFile.hasNextInt())

{

primeArray.add( scanFile.nextInt());

}

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

1 import java.io.*; import java.util.*; 2 3 4 public class PrimeDemo { 5 6 public static final String FACTORS_FILE = factors

27 28 29 30 static void loadFactors (ArrayList<Integer> factors) throws FileNotFoundException { Scanner sc = new Scanner(new

import java.io.*;

import java.util.*;

public class PrimeDemo {

    public static final String FACTORS_FILE = "factors.txt";

    // Method to create factors on file, you should call this method only once to create the factors file.

    static void printPrimesToFile() throws FileNotFoundException {

        // Taking max 6 digit number, since our mob is 10 digits long hence factors can be restricted till square root only.

        List<Integer> nums = IntStream.rangeClosed(2, 100000).boxed().collect(Collectors.toList());

        for (int index = 0; index < nums.size(); index++) {

            // Remove all numbers which are divisible by current number

            int curr = nums.get(index);

            nums = nums.stream().filter(x -> (x == curr || x % curr != 0)).collect(Collectors.toList());

        }

        // Now we have all the prime factors in list now.

        PrintWriter writer = new PrintWriter(new File(FACTORS_FILE));

        for (int x : nums) {

            writer.println(x);

        }

        writer.close();

    }

    static void loadFactors(ArrayList<Integer> factors) throws FileNotFoundException {

        Scanner sc = new Scanner(new File(FACTORS_FILE));

        while (sc.hasNextInt()) {

            factors.add(sc.nextInt());

        }

        sc.close();

    }

    public static void main(String[] args) throws FileNotFoundException {

        printPrimesToFile();

        ArrayList<Integer> factors = new ArrayList<Integer>();

        loadFactors(factors);

        Scanner in = new Scanner(System.in);

        System.out.println("Enter mobile number: ");

        long mob = in.nextLong();

        boolean prime = true;

        for (int x : factors) {

            if (mob % x == 0) {

                prime = false;

                break;

            }

        }

        if (prime) {

            System.out.println("Mobile number is prime");

        } else {

            System.out.println("Mobile number is not prime");

        }

    }

}

PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT SECTION

Add a comment
Know the answer?
Add Answer to:
JAVA please! Prime numbers are interesting numbers. A prime number is one that is only divisible...
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
  • A prime number is a method that is evenly divisible by only itslef and 1. Write...

    A prime number is a method that is evenly divisible by only itslef and 1. Write a method called isPrime, which takes only one integer as an argument and returns true if the number is prime or false if the number is not prime. Create a method to store a list of all the prime numbers from 1 to 100 in a file called Primes.txt. Use the package primes; at the beginning of your code and be sure to document...

  • a prime number is a number that is only evenly divisible by itself and 1. for...

    a prime number is a number that is only evenly divisible by itself and 1. for example the number 5 is prime because it can only be evenly divided by 1 and 5 the number 6 however is not prime because it can be divided evenly by 1,2,3 and 6. write a function name isPrime which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. use this functuion in a...

  • 17. Prime Numbers A prime number is a number that is only evenly divisible by itself...

    17. Prime Numbers A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and S. The number 6, how- ever, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a Boolean function named is_prime which takes an integer as an argument and returns true if the argument is a prime number, or...

  • A Prime Number is an integer which is greater than one, and whose only factors are...

    A Prime Number is an integer which is greater than one, and whose only factors are 1 and itself. Numbers that have more than two factors are composite numbers. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. The number 1 is not a prime number. Write a well-documented, Python program - the main program, that accepts both a lower and upper integer from user entries. Construct a function, isPrime(n), that takes...

  • Write a java project that reads a sequence of up to 25 pairs of names and...

    Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes, street address, city, state, and 10-digit phone number for individuals. Store the data in an object designed to store a first name (string), last name (string), and postal code (integer), street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' . Assume each...

  • java Part 1 Create a NetBeans project that asks for a file name. The file should...

    java Part 1 Create a NetBeans project that asks for a file name. The file should contain an unknown quantity of double numeric values with each number on its own line. There should be no empty lines. Open the file and read the numbers. Print the sum, average, and the count of the numbers. Be sure to label the outputs very clearly. Read the file values as Strings and use Double.parseDouble() to convert them. Part 2 Create a NetBeans project...

  • Description For this program, you are going to convert decimal (integer) numbers into their octal number...

    Description For this program, you are going to convert decimal (integer) numbers into their octal number (integer) equivalents. Make sure that you create a new Project and Java class file for this assignment. Your Repl.It file should be named “Main.java”. You can read about octal-to-decimal number conversions from wikepedia or another website Instructions The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its...

  • Write a java program to print all the prime numbers below a certain given number. A...

    Write a java program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input...

  • Write a C program which computes the count of the prime and perfect numbers within a...

    Write a C program which computes the count of the prime and perfect numbers within a given limit Land U, representing the lower and upper limit respectively. Prime numbers are numbers that have only 2 factors: 1 and themselves (1 is not prime). An integer number is said to be perfect number if its factors, including 1 (but not the number itself), sum to the number. Sample Inputi: 1 100 Sample Outputs: Prime: 25 Perfect: 2 Sample Input2: 101 10000...

  • 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...

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
ADVERTISEMENT