001package net.n1da.dev.euler;
002
003import net.n1da.dev.euler.core.*;
004import net.n1da.dev.euler.helper.*;
005
006/**
007 * Solution for problem 7
008 * "<strong>10001st prime</strong>"
009 * completed on Thu, 9 Apr 2015, 23:34.
010 * 
011 * @author <a href="mailto:nico@danneberg.de">Nico Danneberg</a>
012 * @since 09.04.2015 23:25:27
013 * 
014 * @see <a href="https://projecteuler.net/problem=7" target="_blank">Problem @ Project Euler</a>
015 * @see <a href="http://n1da.net/specials/projekt-euler/problem-7-10001-primzahl/" target="_top">Solution @ Ni-Da-Net</a>
016 */
017public class PE0007 extends Problem {
018
019        /**
020         * The maximum number to search to.
021         */
022        private int max; 
023        
024        /**
025         * The count of found primes.
026         */
027        private int cnt;
028        
029        /**
030         * The storage for the "Sieve of Eratosthenes".
031         * 
032         * @see Sieve
033         */
034        private Sieve sieve;
035        
036        /**
037         * A simple constructor to set {@link Problem#number number}
038         * and {@link Problem#title title}.
039         */
040        public PE0007() {
041                super( 7, "10001st prime" );
042        }
043
044        /**
045         * Initializes the {@link #sieve} with the maximum of {@link #max}
046         * 
047         * @see Sieve#Sieve(int)
048         */
049        @Override
050        public void prepare() {
051                super.prepare();
052                
053                max = 500000; 
054                cnt = -1;
055
056                sieve = new Sieve( max );
057        }
058        
059        /**
060         * Runs the algorithm of the "Sieve of Eratosthenes" and count every
061         * found prime factor. If the 10001. is found, it is returned.
062         *  
063         * @return the 100001. prime factor
064         * @see Sieve#next()
065         * @see <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes @ Wikipedia</a>
066         */
067        @Override
068        public String solve() {
069                
070                while( sieve.hasNext() && cnt < 9999 ) {
071                        cnt++;
072                        IO.infoln( cnt + ". prime = " + sieve.next() );
073                }               
074                
075                return IO.i2s( sieve.next() );
076        }
077
078}