Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8962747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:12:04+00:00 2026-06-15T16:12:04+00:00

the assignment is to find all Mersenne primes with p <= 31 and display

  • 0

the assignment is to find all Mersenne primes with p <= 31 and display in a table:

p     2^p-1
---   ---- 
2      3

3      7

5      31

...

My result so far is this code:

public class PE28MersennePrimeVer2 {

    public static void main(String[] args) {

        System.out.println("p\t2^p - 1");

        for (int number = 2; number <= 31; number++) {
            if (isPrime(number)) {
                int mersennePrime = (int)(Math.pow(2, number)) - 1;
                if (isPrime(mersennePrime)) {
                    System.out.print(number + "\t" + mersennePrime + "\n");
                }
            }
        }
    }

    public static boolean isPrime(int number) {

        if ((number == 1) || (number == 2)) {
            return true;
        }

        for (int i = 2; i <= number/2; i++) {
            if (number % i == 0) {
                return false;
            }
        }

        return true;
    }
}

The output is for p up to 19, never reach 31. What I’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:12:05+00:00Added an answer on June 15, 2026 at 4:12 pm

    The problem is that

    (int)Math.pow(2,31) - 1;
    

    evaluates to 2147483646 instead of 2147483647.

    Don’t use floating point math when dealing with integers. In Java, using (1 << number) - 1 works (1 << 31 would be undefined behaviour in C due to overflow, but it’s defined in Java).

    If you cannot use bit-shifts, you can write your own integer-power function. For the small exponents under consideration, the straightforward

    long pow(long base, long exponent) {
        long result = 1;
        while(exponent > 0) {
            result *= base;
            --exponent;
        }
    }
    

    is good enough (note: I used long instead of int to avoid overflows; although the overflow behaviour is defined in Java, avoiding overflow is cleaner).

    With that,

    mersennePrime = (int)(pow(2,number) - 1);
    

    does its job (although you should consider using long also for the other variables, not only for the intermediate pow result).

    For larger exponents (although that would only be relevant for using BigIntegers – which have their own implementation in the standard library – or modular exponentiation), exponentiation by repeated squaring

    long pow(long base, long exponent) {
        long aux = 1;
        while(exponent > 0) {
            if (exponent % 2 == 1) {
                aux *= base;
            }
            base *= base;
            exponent /= 2;
        }
        return aux;
    }
    

    would give a big performance advantage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this model in legacy code, with RoR 2.3.11: class Assignment < ActiveRecord::Base belongs_to
This sample uses where to find all products that are out of stock. public
This is part of a homework assignment. I've got several questions asking find the
I'll preface this by saying it's a homework assignment. I don't want code written
So my assignment is to find all the magic numbers within a range of
I'm working on an assignment for class and the question asks to find matches
The assignment, for my data structures class, is to find the shortest path from
This is a homework assignment, just for all that want to know. I'm writing
I am working on a class assignment this morning and I want to try
First of all, thank you for looking at this question. For a school assignment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.