Thursday, January 19, 2012

Pythagoras

/*

 * bhatt.umang7@gmil.com

 */

/*

 * 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.

 */



#include<iostream>

#include<math.h>

using namespace std;

bool isPerfectSquare(int no )
{

    bool ans = true ;
    for(int i = 2 ; i < no ; i++)
    {

        if ( no % i == 0 )
        {

            ans = false;
            break;
        }
    }
    return ans;
}

int main()

{

    int ans ;
    for (int i = 1 ; i <= 1000 ; i++ )
    {

        for (int j = 1 ; j <= 1000 ; j++ )
        {

            for (int k = 1 ; k <= 1000 ; k++ )
            {

                if (((i*i) + (j*j)) == (k*k) )
                {

                    if( i < j < k)
                    {
                        if(i+j+k==1000)    
                        {    
                            ans = i * j * k ;

                            goto show_ans;
                        }
                    }
                }
            }
        }
    }
    show_ans:

    cout << ans << "\n";

}

Find the greatest product of five consecutive digits in the 1000-digit number.

/*

 * bhatt.umang7@gmil.com

 */

/*

 * Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

 */



#include<iostream>

#include<stdlib.h>

using namespace std;

long getDigit(string s,int pos)

{

    char arr[1];
    arr[0] = s.at(pos);

    long l  =  atol(arr);
    return l ;

}

int main()

{

    string s = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
    long ans = -1 ;

    for(int i = 0 ; i < 1000-4 ; i++ )
    {

        long mul =  getDigit(s , i ) * getDigit(s ,  i+1 ) * getDigit(s , i+2 ) * getDigit(s , i+3 ) * getDigit(s , i+4 )  ;

        if (mul > ans )
        {
            ans = mul;
        }

    }

    cout << ans << "\n";

}


10001st prime number

/*

 * bhatt.umang7@gmil.com

 */

/*

 * 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 10001st prime number?

 */



#include<iostream>


using namespace std;

bool isPrime(long no)
{
    bool bval = true;

    for(long i = 2 ; i < no ; i++)
    {

        if ( no % i == 0 )
        {

            bval = false;
            break;
        }
    }
    return bval;
}

int main()

{

    long count = 0 ;

    long no = 2 ;
    while (true)
    {

        if (isPrime(no)==true)
        {
            count++;
            if (count == 10001)
            {

                break;
            }
        }
        no++;
    }
    cout << no << "\n";

}

number program

/*

 * bhatt.umang7@gmil.com

 */

/*

 * 2520 is the smallest number that can be divided

 * by each of the numbers from 1 to 10 without any remainder.

 * What is the smallest positive number that is evenly

 * divisible by all of the numbers from 1 to 20?

 */



#include<iostream>


using namespace std;

int main()

{

    long n1 = 100;

    int ans1 = 0;
    int ans2 = 0 ;

    int ans = 0 ;
    for(int i = 1 ; i <= n1 ; i++)
    {

        ans1 += i;
    }
    ans1 = ans1 * ans1;

    for(int i = 1 ; i <= n1 ; i++)
    {

        ans2 += (i*i);
    }
    ans = ans1 + ans2;

    cout<< "Ans is "  <<  ans1-ans2 ;

    return 0;

}

smallest no divisble by numbers between 1 and 20

/*
 * bhatt.umang7@gmil.com
 */
/*
 * 2520 is the smallest number that can be divided
 * by each of the numbers from 1 to 10 without any remainder.
 * What is the smallest positive number that is evenly
 * divisible by all of the numbers from 1 to 20?
 */

#include<iostream>

using namespace std;

int main()

{

    long n1 = 20;

    long ans =1 ; 

    bool b = true ;

    int i = 0;

    while ( true )

    {

        b = true;

        for ( i = 1 ; i <= n1 ; i++ )

        {

            if (ans%i==0)

            {

            }

            else

            {

                b = false;

                break;

            }

        }

        if (b == true)

        {

            break;

        }

        ans++;

    }

    cout<< "Ans is "  <<  ans ;

    return 0;

}

largest palindrome made by mul of three digits

/*
 * bhatt.umang7@gmail.com
 */

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


public class Problem4

{
    static long reverse(long no)
    {
        long reverse = 0 ;

        while (no  > 0 )
        {
            reverse *=10;

            reverse += no% 10;
            no/=10;
        }

        return reverse;    
    }
    public static boolean isPalindrome(long no)
    {
        long reverse = reverse(no);

        boolean returnVal = false ; 
        if (reverse == no )
        {

            returnVal = true;
        }
        return returnVal;
    }
    public static void main(String args[])
    {

        
        int n1 = 1000;
        int n2 = 1000;

        long ans =0 ;
        for (int i = 0 ; i < n1 ; i++ )
        {

            for (int j = 0 ; j < n1 ; j++ )
            {

                long temp =  i * j ;
                if (isPalindrome(temp))
                {

                    if (ans < temp)
                    {
                        ans = temp ;
                    }
                }
            }
        }

    
        System.out.println("Ans is "+ ans );
    }
}

largest prime factor of the number 600851475143

/*
 * bhatt.umang7@gmail.com
 */

/*
 * The prime factors of 13195 are 5, 7, 13 and 29.
 * What is the largest prime factor of the number 600851475143 ?
 */
import java.util.ArrayList;
import java.util.List;

public class Problem3
{

    /**
     * This method will return the ArrayList containing the 
     * prime-factors of the number passed to the method.
     @param number is the number for which you want to find the prime factors
     @return will return a list which is having prime factors of the number
     * passed to the method
     */
    public static List<Long> primeFactors(long number)
    {

        long n = number; // n is the number passed.
        List<Long> factors = new ArrayList<>()
        // the list which will hold the prime factors of the number passed.

        /*
         * go up to n \ i increment i
         */
        for (long i = 2; i <= n / i; i++)
        {

            // if the reminder is zero then it is a valid prime factor
            while (n % i == 0)
            {

                // add the currunt number to the list of prime factors
                factors.add(i);

                n /= i; // n = n / i
                // we are changing the nummber itself for 
                //which we are finding the prime factors
            }
        }
        if (n > 1)
        {
            factors.add(n);
        }
        return factors; // return the list to the caller
    }

    public static void main(String args[])
    {
        long n = 600851475143L;

        // get the list of prime factors by calling the method
        List<Long> li = primeFactors(n);

        // store the last item of list in the ans variable
        long ans = li.get(li.size() 1);

        System.out.println("Ans is " + ans);
        // print the answer on the console
    }
}

sum of even Fibonacci numbers less than 4 million

/*
 * bhatt.umang7@gmail.com
 */

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

public class Problem2
{

    public static void main(String args[])
    {
        long ans = 0  ; 
        long sum = 0 ;

        long max = 4000000;
        long prev_prev = 0 ;

        long prev = 1 ;
    
        while (sum< max)
        {

            sum = prev_prev  + prev ;
            prev_prev = prev ; 
            prev = sum ;

            if (sum%2 == 0)
            {
                ans += sum ;
            }
        }

        System.out.println("Ans is "+ ans );
        // ans is 
    }
}

sum of no who are multiple of 5 or 3

/*
 * bhatt.umang7@gmail.com
 */

/*
 * If we list all the natural numbers below 10 that are multiples of 3 or 5,
 * we get 3, 5, 6 and 9. The sum of these multiples is 23.
 * Find the sum of all the multiples of 3 or 5 below 1000.
 */

public class Problem1
{

    public static void main(String args[])
    {
        int n = 1000 ;

        int sum = 0 ;
        for (int i = 1 ; i < n ;i++)
        {

            if ((i%3 == 0 ) || (i%5==0) )
            {

                sum+=i;
            }
        }
        System.out.println("Sum is "+ sum );

        // ans is 233168
    }
}