Project Euler | Problem 7 | Find 10001st Prime number in Java?



Problem Description :


By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.What is the 10001st prime number?


For generating prime numbers we are using Sieve of Eratosthenes algorithm. This is very simple and efficient algorithm for generating primes.


Algorithm :


If you want to generate all prime number below N. Please follow below steps.

  • Create boolean array of  length N and assign 'TRUE' to all elements of an array. 
  • Find the sqrt of N because loop will run from 2 to sqrt(N).
  • In loop, check whether element present at current index is 'TRUE'. If yes, then assign 'FALSE' to all elements whose index is multiple of current index.

Java Program :


package com.javamultiplex.projecteuler;

import java.util.Arrays;

/**
 * 
 * @author Rohit Agarwal
 * @category Project Euler Problems
 * @problem 10001st Prime?
 */
public class Problem7 {

 /**
  * 
  * Here for finding prime numbers we are using algorithm Sieve of
  * Eratosthenes.
  * 
  */
 public static void main(String[] args) {

  /**
   * 
   * Minimum boolean array Size should be 104744 because we have to find
   * 10001st prime number.
   * 
   */
  int size = 104744, count = 0, limit = 10001;
  boolean primes[] = new boolean[size];
  // Assigning true to all array elements.
  Arrays.fill(primes, true);
  // Using algorithm Sieve of Eratosthenes.
  generatePrimeNumbers(primes);
  for (int i = 2; i < size; i++) {
   if (primes[i]) {
    count++;
    if (count == limit) {
     System.out.println("The 10001st prime number is : " + i);
    }
   }
  }

 }

 private static void generatePrimeNumbers(boolean[] primes) {

  int size = primes.length;
  int sqrt = (int) Math.sqrt(size);
  for (int i = 2; i < sqrt; i++) {
   if (primes[i]) {
    for (int j = i * i; j < size; j = j + i) {
     primes[j] = false;
    }
   }
  }
 }
}


Output :


Output of Project Euler Problem 7 - 10001st Prime number in Java
Output - Project Euler Problem 7

References :



Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.

Tags : Project Euler Problem 7 solution in Java, Prime numbers, 10001st Prime, Mathematics problems, if else statement, for loop, Eratosthenes Algorithm, Arrays.

About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.


Let's Get Connected: Linkedin | Facebook |

Project Euler | Problem 7 | Find 10001st Prime number in Java? Project Euler | Problem 7 | Find 10001st Prime number in Java? Reviewed by Rohit Agarwal on 1/13/2017 Rating: 5

No comments:

Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.

Powered by Blogger.