The Programming Project

Saturday, August 30, 2014

1000-digit Fibonacci number : Problem 25

1000-digit Fibonacci number : Problem 25

The Fibonacci sequence is defined by the recurrence relation:
Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
Hence the first 12 terms will be:
F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144
The 12th term, F12, is the first term to contain three digits.
What is the first term in the Fibonacci sequence to contain 1000 digits?

Problem Source: Euler Project

Python Code

def ThousandDigitFiboNumbers():
    a = 1
    b = 1
    nextSum = 0
    totalSum = 2
    while True:
        nextSum = a+b
        a = b
        b = nextSum
        totalSum = totalSum + 1
        if len(str(nextSum)) == 1000:
            break
    print "first term in the Fibonacci sequence to contain 1000 digits:",totalSum       
ThousandDigitFiboNumbers()

Power digit sum : Problem 16 Python Code

Power digit sum : Problem 16 Euler Project

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 21000?

Python Code

def powerDigitSum():
    l = 2**1000
    strNumb = str(l)
    sumFact = 0
    for i in range(len(strNumb)):
        sumFact = sumFact + int(strNumb[i])
    print "sum of the digits in the number! is",sumFact
powerDigitSum()

Factorial digit sum : Problem 20 Euler Project Pyhton Code

Factorial digit sum : Problem 20 Euler Project

n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!


Python Code

def main():
    x=input("Enter a natural number:")
    x1 = x
    l=1
    for i in range(x):
        l=l*x
        x=x-1
    print l
    strNumb = str(l)
    sumFact = 0
    for i in range(len(strNumb)):
        sumFact = sumFact + int(strNumb[i])
    print "sum of the digits in the number",x1,"! is",sumFact
main()

Counting Sundays Problem : Euler Project : Problem 19

Counting Sundays : Problem 19 Euler Project

You are given the following information, but you may prefer to do some research for yourself.
  • 1 Jan 1900 was a Monday.
  • Thirty days has September,
    April, June and November.
    All the rest have thirty-one,
    Saving February alone,
    Which has twenty-eight, rain or shine.
    And on leap years, twenty-nine.
  • A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?


JAVA Code

import java.util.*;
import java.io.*;
public class euler19 {
    public static void main(String[] args) throws IOException {
        int day=1,month=1,year=1900;
        int day1=31,month1=12,year1=2000;
        DaysCalculation date = new DaysCalculation();
        DaysCalculation date1 = new DaysCalculation();
        date.setDate(day,month,year);   
        System.out.println("Number of Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000):"+date.countDays(day1,month1,year1));       
        }
    }
class DaysCalculation {
    public void setDate(int d, int m, int y ){
        day = d;
        month = m;
        year = y;
        sundayCount = 0;
        }
    public int countDays(int day1, int month1, int year1 ){
        boolean small = false;
        int weekDpointer=0;
        if (year == year1)
            if (month == month1)
                if (day < day1)
                    small = true;
                else
                    small = false;       
            else if ( month < month1)
                small = true;
            else
                small = false;           
        else if( year < year1)
            small = true;
        else
            small = false;
        if (small == false) {
            int td,tm,ty;
            td = day;
            day = day1;
            day1 = td;
            tm = month;
            month = month1;
            month1 = tm;
            ty = year;
            year = year1;
            year1=ty;
            }   
        while (year < year1+1) {
            /*if(weekDpointer == 6 && day == 1) {
                System.out.println(day+"/"+month+"/"+year+weekDays[weekDpointer]);
                sundayCount++;
            }*/
            day++;
            weekDpointer++;
            if(weekDpointer >= 7)
                weekDpointer %=7;
            if (checkLeap(year) == true) {
                if (day > ldays[month-1]) {
                    day = 1;
                    month ++;
                    if(weekDpointer == 6 && day == 1 && year != 1900) {
                        System.out.println(day+"/"+month+"/"+year+"~~"+weekDays[weekDpointer]);
                        sundayCount++;
                        System.out.println();
                    }
                    if (month > 12) {
                    month = 1;   
                    year++;
                    }
                }
            }   
            else {
                if (day > mdays[month-1]) {
                    day = 1;
                    month ++;
                    if(weekDpointer == 6 && day == 1 && year != 1900) {
                        System.out.println(day+"/"+month+"/"+year+"~~"+weekDays[weekDpointer]);
                        sundayCount++;
                        System.out.println();
                    }
                    if (month > 12) {
                    month = 1;   
                    year++;
                    }
                }   
            }
        } // end of while   
        return sundayCount;       
        }
    private boolean checkLeap(int year) {
        if(year%400==0)
           leap=true;
        else if (year%100==0)
           leap=false;
        else if (year%4==0)
           leap=true;
        else
           leap=false;
           return leap;
        }    
    private boolean flag;
    private static boolean leap;   
    private int day,month,year;
    private int sundayCount;
    String[] weekDays ={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
    int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
    int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};   
    }       

Summation of primes : Python Code Problem 10 Euler Project

Summation of primes : Problem 10

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

Problem Source: Euler Project

Python Code

import math
def sumPrime():
    primeSum = 17
    def checkPrime(numb):
        prime = True
        for i in range(int(math.sqrt(numb))):
            i = i+2
            if numb%i == 0:
                prime = False
                break
        if numb == 2:
            return True
        else:           
            return prime
    for i in range(2000000-1):
        i = i+2
        if i%2 == 0 or i%3 == 0 or i%5 ==0 or i%7 == 0:
            continue
        if checkPrime(i) == True:
            primeSum = primeSum+i
            
    print "Sum of all the primes below two million:",primeSum        
sumPrime()

Special Pythagorean triplet : Python Code : Euler Project : Problem 9

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.

Problem Source: Euler Project

Python Code

def specialPythagoreanTriplet():
    product = 1
    flag = False
    for  a in range (1000):
        a = a+1
        for b in range(1000):
            b = b+1
            if a+b > 1000 or a == b:
                continue
            for c in range(1000):
                c = c+1
                if a+b+c != 1000 or c%4 == 2 or c%4 == 3:
                    continue
                if a**2+b**2 == c**2 and a+b+c == 1000:
                    product = a*b*c   
                    print a,b,c
                    flag = True
                    break
            if flag == True:
                break       
        if flag == True:
            break       
    print "The prouct abc:",product       
specialPythagoreanTriplet()

Thursday, August 28, 2014

Smallest multiple : Euler Project

Smallest multiple : Problem 5

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?

The answer to this problem is to find the l.c.m of the numbers 1,2,3,.......,18,19,20

Python Code

def smallestMultiple():
    lcm = 2
    tup = [0 for i in range(20)]
    for i in range(20):
        tup[i] = i+1
    while lcm > 1:
        count = 0
        for i in range(20):
            if lcm%tup[i] == 0:
                count = count+1
            else:
                break   
        if count != 20:
            lcm = lcm+1       
        else:
            break
    print lcm
smallestMultiple()