### Three more problems!

#### by Gilbert Keith

Solved 6 totals problems today!

Here are a couple:

Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed.

3736 35 34 33 3231

381716 15 141330

39 1854312 29

40 19 6 1 2 11 28

41 2078 9 10 27

42 21 22 23 24 25 26

4344 45 46 47 48 49It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13 62%.

If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?

public class problem58 { public static void main (String[] args) { long start = System.currentTimeMillis(); boolean found = false; //will be set to true when we find the ring // used to keep a track of the integer we are checking for primality; int val = 1; //used to keep a track of what ring we're on. The answer will be (ringCounter * 2 + 1) double ringCounter = 0.00; //will be incremented by 4 per ring. double countOfDiagNums = 1.00; //will be incremented by 1 when val is prime. double primeCount = 0.00; //probability will be set to primeCount/countOfDiagNums double probability = 0.00; while (!found) { ringCounter++; //increment ring count by 1. countOfDiagNums += 4; //increment diagonal number count by 4. for (int j=1; j<5; j++) { //increment val by (2*ringCounter); //eg. when ringCounter = 1 (square side = 3), val will be 3, 5, 7, 9. //when ring counter = 2 (square side = 5) val will be 13,17,21,25 val += 2*ringCounter; //if val is prime, then increment the primeCount by 1. if (isValPrime(val)) primeCount++; } probability = primeCount/countOfDiagNums; //calculate probability if (probability < 0.10) { found = true; //if probability is less than 0.10, we have found our ring. } } //the length of the side of the square is (2*ringCounter + 1) System.out.println("The answer is: " + (2*(ringCounter) + 1)); long end = System.currentTimeMillis(); System.out.println("Time: " + (end-start) + " ms"); } private static boolean isValPrime(int value) { boolean yesOrNo = true; for (int j=2; j*j <= value; j++) { if (value % j == 0) { yesOrNo = false; break; } } return yesOrNo; } }

And here’s another one:

A googol (10

^{100}) is a massive number: one followed by one-hundred zeros; 100^{100}is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.Considering natural numbers of the form,

a, where^{b}a, b100, what is the maximum digital sum?

import java.math.BigInteger; public class problem56 { public static void main(String[] args) { long start = System.currentTimeMillis(); int digitalSum = 0; //will hold the value for each number we're interested in; int highestSum = 0; //will cache the highest value we calculated. for (int j = 99; j > 1; j--) { for (int k = 99; k > 1; k--) { //use BigInteger to calculate the power we are interested in; BigInteger power = BigInteger.valueOf(j).pow(k); //convert the BigInteger to a string. String numString = power.toString(); //if the (length of the string*9) is less than the highest value, //then for that base we will never be able to exceed the highest //digital sum; stop calculating more values and break out of the //second for loop!; if (numString.length()*9 < highestSum) break; digitalSum = sumOfDigits(numString); // if (digitalSum > highestSum) { highestSum = digitalSum; } } } System.out.println("Answer is: " + highestSum); long end = System.currentTimeMillis(); System.out.println("Time: " + (end - start) + " ms"); } private static int sumOfDigits(String number) { int sum=0; int num; for (int l = 0; l < number.length(); l++) { num = Integer.parseInt(number.substring(l,l+1)); sum += num; } return sum; } }ec