001package net.n1da.dev.euler;
002
003import net.n1da.dev.euler.core.*;
004import net.n1da.dev.euler.helper.*;
005
006/**
007 * Solution for problem 56
008 * "<strong>Powerful digit sum</strong>"
009 * completed on Wed, 26 Oct 2016, 00:43.
010 * 
011 * @author <a href="mailto:nico@danneberg.de">Nico Danneberg</a>
012 * @since 29.08.2016 21:20:19
013 * 
014 * @see <a href="https://projecteuler.net/problem=56" target="_blank">Problem @ Project Euler</a>
015 * @see <a href="http://n1da.net/specials/projekt-euler/problem-56-maechtige-quersummen/" target="_top">Solution @ Ni-Da-Net</a>
016 */
017public class PE0056 extends Problem {
018
019        /**
020         * A simple constructor to set {@link Problem#number number}
021         * and {@link Problem#title title}.
022         */
023        public PE0056() {
024                super( 56, "Powerful digit sum" );
025        }
026
027        /**
028         * This method solves the given problem by running two nested loops
029         * to check all ten-thousand combinations of a and b.
030         * 
031         * @return the maximum found digit sum
032         */
033        @Override
034        public String solve() {
035                
036                LargeNumber max = new LargeNumber( 1 );
037                
038                for( int a = 1; a < 100; a++ ) {
039                        LargeNumber lA = new LargeNumber( a );
040                        LargeNumber cur = lA.clone();
041                        for( int b = 1; b < 100; b++ ) {
042                                LargeNumber sum = cur.sumOfDigits();
043                                if( max.compareTo( sum ) == -1 ) {
044                                        IO.debugln( "found new max = " + sum + " for " + a + "^" + b );
045                                        max = sum;
046                                }
047                                cur = cur.mult( lA );
048                        }
049                }
050
051                return max.toString();
052        }
053}