001package net.n1da.dev.euler;
002
003import net.n1da.dev.euler.core.*;
004import net.n1da.dev.euler.helper.*;
005
006/**
007 * Solution for problem 57
008 * "<strong>Square root convergents</strong>"
009 * completed on Sun, 30 Oct 2016, 06:46.
010 * 
011 * @author <a href="mailto:nico@danneberg.de">Nico Danneberg</a>
012 * @since 26.10.2016 19:01:38
013 * 
014 * @see <a href="https://projecteuler.net/problem=57" target="_blank">Problem @ Project Euler</a>
015 * @see <a href="http://n1da.net/specials/projekt-euler/problem-57-quadratwurzelkonvergenzen/" target="_top">Solution @ Ni-Da-Net</a>
016 */
017public class PE0057 extends Problem {
018
019        /**
020         * A simple constructor to set {@link Problem#number number}
021         * and {@link Problem#title title}.
022         */
023        public PE0057() {
024                super( 57, "Square root convergents" );
025        }
026
027        /**
028         * This method just runs a loop for the 1.000 steps and calculates
029         * the numerator and denominator for every iteration. If the numerator
030         * has more digits than the denominator a counter is increased.
031         * 
032         * @return the count of cases where numerator has more digits than the denominator
033         */
034        @Override
035        public String solve() {
036                
037                long count = 0;
038                LargeNumber num = new LargeNumber( 3 );
039                LargeNumber den = new LargeNumber( 2 );
040                LargeNumber nnum;
041                LargeNumber nden;
042                
043                for( int i = 1; i < 1000; i++ ) {
044                        nnum = num.add( den.mult( 2 ) );
045                        nden = num.add( den );
046                        if( nnum.getLength() > nden.getLength() ) {
047                                IO.debugln( "found " + nnum + " / " + nden );
048                                count++;
049                        }
050                        den = nden;
051                        num = nnum;
052                }
053                
054                return IO.l2s( count );
055        }
056}