Skip to main content

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 patel
     *
/
    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 patel

     */

    public static void main(String[] args) {
        int count =0,num=2;
        while(count!=10001){
            if(isPrime(num)){
                count++;
            }
            if(count==10001){
                System.out.println(num);
            }
            num++;
        }

    }

//Prime method.
    public static boolean isPrime(int n){
        boolean result = true;
        for (int num=2; num*num<=n; num++){
            if (n % num == 0){
                result= false;
                break;
            }
        }
        return result;
    }
}
 

8) Problem 8- Project Euler Solution in Java


Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
Okay, This one looks is ugly. But, it works!!
You could save the numbers in text file and can do it that way also. But, I wanted make it simple so I did it using  string. In fact, the following problem can be done many ways, you could also do it using Two-D array. Leave comment below for any improvment.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Largestproduct {


   /*
     * @euler project 8
     * @author Niraj patel

     */

    public static ArrayList<Long> values = new ArrayList();
    public static ArrayList<Integer> multiply = new ArrayList();
    public static void main(String[] args) {
        String numbers ="73167176531330624919225119674426574742355349194934" +
                "96983520312774506326239578318016984801869478851843" +
                "85861560789112949495459501737958331952853208805511" +
                "12540698747158523863050715693290963295227443043557" +
                "66896648950445244523161731856403098711121722383113" +
                "62229893423380308135336276614282806444486645238749" +
                "30358907296290491560440772390713810515859307960866" +
                "70172427121883998797908792274921901699720888093776" +
                "65727333001053367881220235421809751254540594752243" +
                "52584907711670556013604839586446706324415722155397" +
                "53697817977846174064955149290862569321978468622482" +
                "83972241375657056057490261407972968652414535100474" +
                "82166370484403199890008895243450658541227588666881" +
                "16427171479924442928230863465674813919123162824586" +
                "17866458359124566529476545682848912883142607690042" +
                "24219022671055626321111109370544217506941658960408" +
                "07198403850962455444362981230987879927244284909188" +
                "84580156166097919133875499200524063689912560717606" +
                "05886116467109405077541002256983155200055935729725" +
                "71636269561882670428252483600823257530420752963450";
        String nums;
        int limit =13,val = 0;
        long multVal = 1,maxVal = 0;
        //This can be improved further if you wish.
        for (int i=0;i<1000-limit+1;i++){
            multVal = 1;
            //breaks string into 13 digits.
            nums = numbers.substring(i,i+limit);
            for (int j=0;j<limit;j++) {
                //takes each element and convert it into digit.
                val = Integer.parseInt(nums.substring(j,j+1));   
                multiply.add(val);
            }
            for (int j=0;j<=multiply.size()-1;j++){
                multVal = multVal*multiply.get(j);
            }
            multiply.clear();
            values.add(multVal);
        }
        System.out.println(Collections.max(values));
    }
}
 

 

9) Problem 9- Project Euler Solution in Java

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

public class main {
    /**
     * @euler project 9
     * @author Niraj patel
     * @Finding pythagorean triplet
     */
    public static void main(String[] args) {
        // Find a number a+b+c=1000 where a<b<c.
        int a = 0, b = 0, c = 0;
        int limit = 1000;
        boolean isFound = false;
        for (a = 1; a < limit; a++) {
            for (b = a; b < limit; b++) {
                // as given a+b+c=1000 --> c must equal c = 1000-a-b
                c = limit - a - b;
                if (a * a + b * b == c * c) {
                    isFound = true;
                    break;
                }
            }
            if (isFound) {
                System.out.println(a*b*c);
                break;
            }
        }
    }


10) Problem 10- Project Euler Solution in Java



The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.

public class sumOfTwoM {


   /*
     * @euler project 10
     * @author Niraj patel
     *

     */
    public static void main(String[] args) {
        long sum =2;
        boolean isPrime = true;
        for (int i=3; i<2000000; i++) {
            isPrime = true;
            //There is no need to go beyond the square root of number to check weather it is prime or not.
            //we only need to test for factors less than or equal to the square root.
            for (int j=2; j<=(int)Math.sqrt(i); j++){
                if (i % j == 0){
                    isPrime = false;
                    break;
                }
            }
            if(isPrime){
                sum +=i;
            }
        }
        System.out.println("Sum  = "+sum);
    }

}

Comments

Popular posts from this blog

Non-decreasing Array - LeetCode

Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n). Example 1: Input: [4,2,3] Output: True Explanation: You could modify the first 4 to 1 to get a non-decreasing array.   Example 2: Input: [4,2,1] Output: False Explanation: You can't get a non-decreasing array by modify at most one element. Note: The n belongs to [1, 10,000]. Source: LeetCode At first glance, it looks like an easy and straightforward problem, but believe me it's greedy problem. In the problem, you are allowed to make atmost "one" modification to make a non-decreasing array. Nondecreasing means that every element is greater than or equal to the one before it e.g. [1,3,4,4,5,6,9]. For example, [2,2,3,2,4] can this become non-decreasing? Yes, by replacing 3 with 2 =>[2,2,2,2,4]. What about this:

RLE Encoding and Decoding in C++

Given an input string, write a regular recursive function that returns the decoded (uncompresseed form) Run Length Encoded   (is a simple form of data compression where repeated character are replaced by count followed by the character repeated) string for the input string. Below are some examples: decode("*") =>* decode("3+") =>+++ decode("11*") =>*********** decode("101+") =>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ decode("abcde10+10*10+10x") =>abcde++++++++++**********++++++++++xxxxxxxxxx decode("\\") =>\ decode("\1\2\3") =>123 decode("13\7x") =>7777777777777x decode("5\\") =>\\\\\ decode("4\12\23\3") =>111122333 decode("4\\2\3") =>\\\\33 NOTE: To represent a single backslash, it’s necessary to place double backslashes (\\) in the input string to obtain the desired in

Merge Two Binary Trees

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree. Example 1: Input: Tree 1 Tree 2 1 2 / \ / \ 3 2 1 3 / \ \ 5 4 7    Output: Merged tree: 3 / \ 4 5 / \ \ 5 4 7   Note: The merging process must start from the root nodes of both trees.  Source: LeetCode Solution: We traverse both trees in a preorder fashion. In