Skip to main content

Posts

Showing posts from 2016
16) Problem 16- Project Euler Solution in Java 2 15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 2 1000 ? Using the BigInteger() class :         int sum =0;         BigInteger big = new BigInteger("2");         big = big.pow(1000);         String result =big.toString();         for(int i=0;i<result.length();i++){             sum+=Character.getNumericValue(result.charAt(i));         }         System.out.println("sum : "+sum ); 

Project Euler Problem 11,12,13,14,15 in Java

11) Problem 11- Project Euler Solution in Java What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid? So, I basically went through every possible way we could do the multiplication in the 20X20 grid. However, the greatest product can be found found easily if I had done multiplication diagonally rather than attempt all the trials.  But, anyway it was a good practice with 2-D arrays. public static void main(String[] args) throws IOException {         FileReader ff = new FileReader("Grid File (20X20).txt");         Scanner in = new Scanner(ff);         int[][] num = new int[20][20];         ArrayList<Integer> list = new ArrayList();         //reading the numbers into 2D array         for(int i=0;i<20;i++){             for(int j=0;j<20;j++){                 num[i][j] = in.nextInt();             }           }         // Multiplying numbers vertically in downward direction         for
Project Euler Problem 6,7,8,9,10  in Java 6) Problem 6- Project Euler Solution in Java Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. My approach to the problem: public class squareDifference {    /*      * @euler project 6      * @author Niraj pate l      * /     public static void main(String[] args) {         int sum=0,sumSquare=0;         for(int num =1;num<101;num++){             sum+=num;             sumSquare+=num*num;         }         System.out.println(sum*sum - sumSquare);     } } 7) Problem 7- Project Euler Solution in Java 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 10 001st prime number? public class Prime {     /*      * @euler project 7      * @author Niraj pate l      */  
Project Euler Problem 1,2,3,4,5 in Java NOTE : Don't look at the solution if you had not tried it yourself. Because real learning is an active process and seeing how it is done is a long way from experiencing that epiphany of discovery.So you try possible solution but something doesn't work and it's hours now when that could be done in minutes. There is nothing wrong in looking at the solution if you have been doing it for long and couldn't figure it out. After looking the solution you may realize what bit of changes you could do for better result. It might be something very silly or stupid thing that you forgot and it's very frustrating.No matter how frustrating problems are, there is almost certainly a solution out there on web. These are my attempts or solution to the problems it might be simple or complex depending on how well you understand my algorithm. I have solved each problem using Java. It took me less than hour in some problems to design efficient