Sunday 25 September 2016

Chapter 10 Exercise 19, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

Chapter 10 Exercise 19:

10.19 (Mersenne prime) A prime number is called a Mersenne prime if it can be writ- ten in the form 2p - 1 for some positive integer p.
Write a program that finds all Mersenne primes with p … 100 and displays the output as shown below. (Hint: You have to use BigInteger to store the number, because it is too big to be stored in long. Your program may take several hours to run.)


import java.math.BigDecimal;

public class Exercise_19 {

    public static void main(String[] args) {

        System.out.printf("%-10s%7s\n", "P", "2 * P -1");
        int numberOfPrimes = 0;
        for (int i = 2; numberOfPrimes < 100; i++) {
            if (isPrime(i)) {
                numberOfPrimes++;
                System.out.printf("%-10d%-10s\n", i, getMersennePrime(i));
            }
        }

    }

    public static boolean isPrime(long n) {

        long square = (long) Math.sqrt(n);
        for (int i = 2; i <= square; i++) {

            if (n % i == 0) {
                return false;
            }
        }

        return true;
    }

    public static BigDecimal getMersennePrime(int p) {

        return new BigDecimal(2).pow(p).subtract(BigDecimal.ONE);
    }
}

2 comments :

  1. Hello ghost henry
    Your profile says you are from Pakistan.
    Mei bhi Pakistan sai hoon.
    Solutions ke liye shukriya.
    Meri uni mei yeh book hum use kartai hai. Mujhe java programming mei thodi mushkil ho rahi hai kyunki yeh mera first time hai java programming mai aur maini pehlai programming nahi ki. Agar mujhe solutions mai doubt ho kya mei aapko pooch sakti hoon? Aur Kya apke paas iss book kei leyai instructor solution manual hai? Mujhe instructors manual ke zaroorat hai..

    ReplyDelete
  2. @Anon i don't have solution manual for the book. And you can always ask me questions. I am here to help people out.

    ReplyDelete