Printing Mersenne Numbers up to ‘n’ Terms in BlueJ

In this tutorial post, we will explore how to print the Mersenne numbers up to ‘n’ terms using the BlueJ programming language. Mersenne numbers are generated by calculating ((2 raised to power n) – 1).The Mersenne numbers will be printed horizontally. Let’s delve into the code!

What is a Mersenne number?

A Mersenne number is a positive integer that can be represented in the form of (2 raised to power n) – 1, where ‘n’ is also a positive integer. In other words, it is a number that is obtained by subtracting 1 from a power of 2.

Mersenne numbers are named after the French mathematician Marin Mersenne, who studied them extensively in the 17th century. They have intriguing properties and connections to various areas of mathematics, particularly in number theory and the theory of prime numbers.

The Mersenne numbers have a special significance due to their relationship with prime numbers. It has been observed that if a Mersenne number is also a prime number, then its corresponding ‘n’ value must also be a prime number. However, not all Mersenne numbers are prime.

For example, let’s consider the Mersenne number where ‘n’ is 3. The calculation would be:

Mersenne number = (2 raised to power 3) – 1 = (2 * 2 * 2) – 1 = 8 – 1 = 7

Therefore, in this case, the Mersenne number is 7. It is noteworthy that 7 is a prime number.

By exploring and printing Mersenne numbers, we can observe fascinating patterns and properties related to prime numbers and number theory.

Code Example

public class MersenneNumbers {
public static void main(String[] args) {
int n = 10; // Number of terms in the series

System.out.println(“Printing Mersenne numbers up to ” + n + ” terms:”);

for (int i = 1; i <= n; i++) {
int mersenneNumber = (int) (Math.pow(2, i) – 1);
System.out.print(mersenneNumber + ” “);



  • The n variable represents the number of terms we want to print in the series.
  • We use a for loop to iterate from 1 to n.
  • Inside the loop, we calculate each Mersenne number by raising 2 to the power of the current loop variable i, and then subtracting 1.
  • To convert the result of Math.pow to an integer, we use the cast operator (int).
  • The Mersenne number is printed horizontally using System.out.print.
  • Finally, the loop continues until i reaches n, and all Mersenne numbers up to n terms are printed.


Printing Mersenne numbers up to 10 terms:
1 3 7 15 31 63 127 255 511 1023


In this tutorial post, we explored the concept of Mersenne numbers and learned how to print them up to ‘n’ terms using the BlueJ programming language. Mersenne numbers are obtained by subtracting 1 from a power of 2, and they hold significance in number theory and prime number research.

We encourage you to further explore the properties and characteristics of Mersenne numbers, as they continue to captivate mathematicians and computer scientists alike. By experimenting with different values of ‘n’, you can generate a sequence of Mersenne numbers and analyze their prime properties.

Now armed with the knowledge and code provided in this post, you can embark on further mathematical and programming adventures with Mersenne numbers in the BlueJ environment. Enjoy the journey of exploration and discovery!

Remember, mathematics and programming offer endless possibilities, and the world of Mersenne numbers is just one fascinating avenue to explore. Happy coding with BlueJ!