What is Brun’s constant
Viggo Brun in 1919 devised a theorem which states that the sum of reciprocals of twin prime numbers reaches towards a value which is called Brun’s constant.
Twin prime numbers are prime numbers which differ by 2 such as 3 and 5, 5 and 7, 11 and 13 etc. Mathematically, above theorem can be expressed as (1/2 + 1/3 + 15 +1/7 + ……) = Brun’s Constant.

A number which is divisible either by 1 or by itself is called a prime number.

Calculating Brun’s constant in java

Brun’s constant involves adding up twin prime numbers and since such numbers have no upper limit, we need to place a limit on the highest prime number that will be considered when calculating Brun’s constant.

Logic

Program logic involves following steps :

  1. Read the highest number as an input from the user.
  2. Initialize a variable to hold the sum of reciprocals.This will hold Brun’s constant at the end of program
  3. Initiate a loop starting from 2(since it is the lowest prime number) till the input number.
  4. In each iteration of the loop check whether the current number and (number + 2) are both prime or not. If they are both prime then sum up their reciprocals and them to the variable which was initialized on Step 2.
  5. Print the value of the variable in Step 2.

Java program written on the above algorithm is as given below.

import java.util.Scanner;
 
public class BrunsConstantCalculator {
  /**
    * Main method
    */
   public static void main(String[] args) {
	// initialize reader
	Scanner reader = new Scanner(System.in);
	System.out.print("Enter a number: ");
	// read limit number
	int number = reader.nextInt();
	double result = calulateBrunsConstant(number);
	System.out.println("Brun's Constant of prime numbers till " + number +
                             " = " + result);
	// close reader
	reader.close();
   }
 
  /**
    * Check if the number is prime
    * 
    * @param numberToCheck
    * @return
    */
   public static boolean isPrime(int numberToCheck) {
	boolean isPrime = true;
	for (int i = 2; i < numberToCheck; i++) {
	   // if number is divisible by any other number
	   if (numberToCheck % i == 0) {
	      // it is not prime
	      isPrime = false;
	      break;
	   }
	}
	return isPrime;
   }
 
  /**
    * Calculates Brun's constant
    * @param number
    * @return
    */
   public static double calulateBrunsConstant(int number) {
	System.out.println("Prime numbers(twin) used in calculation are :");
	System.out.println("---------------------------------------------");
	// initialize variable to hold sum
	double sum = 0.0;
	// loop till the number
	for (int i = 2; i < number; i++) {
	   int firstNumber = i;
	   int secondNumber = i + 2;
	   // check if both numbers are prime
	   if (isPrime(firstNumber) && isPrime(secondNumber)) {
	      System.out.println(firstNumber + " " + secondNumber);
	      // calculate sum of reciprocals and add them to total
	      sum = sum + (double) 1 / firstNumber + (double) 1 / secondNumber;
	   }
	}
	return sum;
   }
}

Output

Above program produces following output.

Enter a number: 23
Prime numbers(twin) used in calculation are :
———————————————
3 5
5 7
11 13
17 19
Brun’s Constant of prime numbers till 23 = 1.1554777523817772

Hope this post was useful. Keep visiting for more such stuff !!!

0

Leave a Reply

Mark Your Impression

  Subscribe  
Notify of
Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.