Tuesday, 5 June 2018

Project Euler Problem 4:Largest palindrome product

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.


Solution:-


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Problem_4{

     public static void main(String []args){
        
        int pal=0;
        int fdigit=0;
        int sdigit=0;
        
        for(int i=100;i<999;i++){
            
            for(int j=100;j<999;j++){
                
                int temp= i*j;
                if(isPalindrome(temp)){
                    if(temp>pal){
                        pal=temp;
                        fdigit=i;
                        sdigit=j;
                    }
                }
                
            }
            
        }
        
        System.out.println("Largest palindrome is "+pal+"\n First Digit is"+fdigit+"\n Second Digit is"+sdigit);
     }
     
     static boolean isPalindrome(int a){
         int rev=0;
         int temp=a;
         while(temp>0){
             rev=temp%10 + rev*10;
             temp=temp/10;
         }
         
         if(rev==a)
            return true;
        else
            return false;
         
         
     }
     
}

Monday, 4 June 2018

Project Problem 3: Largest prime factor

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

______________________________________________________________________________


Solution:


public class Problem_3{
    
    public static void main(String[] args){
        
        long a=600851475143l;
        long prime=0;
        
        for(long i=1l;i<a;a++){
            long temp=0l;
            if(a%i==0){
                if(isPrime(i)){
                    System.out.println("Prime factor is "+i);
                }
            }
        }
    }
    
    static boolean isPrime(long a){
        for(long i=2l;i<a;i++){
            if(a%i==0)
                return false;
        }
        
        return true;
    }
    
}

Project Euler Problem 2: Even Fibonacci numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

______________________________________________________________________________


Solution:-


public class Problem_2{

    public static void main(String[] args){
        
        int prev =1;
        int result=0;
        double sum=0;
        
        while(i<4000000){
            result=i+prev;
            prev=i;
            i=result;
            
            if(result(%2==0){
                sum=sum+result;
                System.out.print(","+i);
            }
        }
        
        System.out.println("\nSum is "+sum);
    }
}