Thursday, 27 June 2013

PROJECT EULER SOLUTION # 41

Solution to problem number 41 of Project Euler.
Question # 41
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also a prime.
What is the largest n-digit pandigital prime that exists?

Solution # 41
/*****************************************************************************/
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

int count_digits(long);
int digit_sum(long);
int is_prime(long);
int contains_repeated(long );
int is_pandigital(long);

int main()
{
       long i,j;
       int digits;
       i=987654321;

       while(1)
       {
              digits=count_digits(i);
              if(digit_sum(i)%3==0)
              {
                     i=i%(long)pow(10.0,digits-1);
                     continue;
              }

              for(j=i;j>pow(10.0,digits-1);j--)
              {
                     if(is_pandigital(j)&&is_prime(j))
                     {
                          
                           printf("Answer = %ld",j);
                           printf("\nEXECUTION TIME = %f\n",clock()/(float)CLK_TCK);
                           system("pause");
                           return;
                     }
              }
       }
}

int count_digits(long num)
{
       int counter=0;
       while(num)
       {
              counter++;
              num/=10;
       }
       return counter;
}

int digit_sum(long num)
{
       int sum=0;
       while(num)
       {
              sum+=num%10;
              num/=10;
       }
       return sum;
}


int is_prime(long num)
{
       long i;
       for(i=2;i<=(long)sqrt((double)num);i++)
              if(num%i==0)
                     return 0;
       return 1;
}


int is_pandigital(long num)
{
       long c_num;
       int digit,max_digit=0;
       if(contains_repeated(num))
              return 0;
      
      
       digit=count_digits(num);
       c_num=num;
       while(c_num)
       {
              if(c_num%10>max_digit)
                     max_digit=c_num%10;
              c_num/=10;
       }

       if(max_digit!=digit)
              return 0;

       return 1;
}


int contains_repeated(long num)
{
       long c_num;
       c_num=num;
       while(c_num)
       {
              if(c_num%10==0)
                     return 1;
              c_num/=10;
       }
       c_num=num;
       while(num)
       {
              while(c_num)
              {
                     if(num%10==(c_num/10)%10)
                           return 1;
                     c_num/=10;
              }
              num/=10;
              c_num=num;
       }
       return 0;
}     

/*******************************************************************************/

PROJECT EULER SOLUTION # 25

Solution to problem number 25 of Project Euler.
Question # 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?

Solution # 25
/*****************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<time.h>

int get_digits(long double);

void main()
{

            long double a,b,sum,i=0;
            int digits,counter=2;

            clrscr();
            a=1;
            b=1;
            sum=0;
            while(1)
            {
                        counter++;
                        sum=a+b;
                       
                        a=b;
                        b=sum;

                        digits=get_digits(sum);

                        if(digits==1000)
                                    break;
                        i++;
            }
            printf("Answer = %d",counter);
            printf("\nEXECUTION TIME = %f",clock()/(float)CLK_TCK);
            getch();
}

int get_digits(long double n)
{
            int flag=0;
            while(n>1)
            {
                        flag++;
                        n/=10;
            }
            return flag;
}

/*******************************************************************************/
Note that this program will not run successfully in visual studio because for VS we have the range of double and float exactly same and that is insufficient for this program.

Tuesday, 25 June 2013

PROJECT EULER SOLUTION # 19

Solution to problem number 19 of Project Euler.
Question # 19
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)?

Solution # 19
/***********************************************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

long count_sunday(long,long,long);

void main()
{
       long i,j,counter=0;
       for(i=1901;i<2001;i++)
              for(j=1;j<=12;j++)
                     if(count_sunday(1,j,i))
                           counter+=count_sunday(1,j,i);
       printf("\nNumber of sundays = %ld\n",counter);
       printf("\nEXECUTION TIME = %f\n",clock()/(float)CLK_TCK);
       system("pause");
}


long count_sunday(long d2,long m2,long y2)
{
       long int d,m,y,d1,m1,y1,tot,lavish,i,counter=0;

       d1=1;
       m1=1;
       y1=0;

       y=y2-y1;
       m=m2-m1;
       d=d2-d1;

       tot=y*365+m*30+d;

       if(m2>2)
       {
              for(i=y1;i<=y2;i++)
              {
                     if(i%4==0)
                           tot++;

                     if(i%100==0)
                           if(i%400!=0)
                                  tot--;
              }
       }
       else
       {
              for(i=y1;i<y2;i++)
              {
                     if(i%4==0)
                           tot++;

                     if(i%100==0)
                           if(i%400!=0)
                                  tot--;
              }
       }


       if(m2>m1)
       {
              for(i=m1;i<m2;i++)
              {
                     if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
                           tot++;

                     if(i==2)
                           tot=tot-2;
              }
       }

       lavish=tot%7;

       if(lavish==1)
       {
              return 1;
       }
       return 0;
}

/***********************************************************************************************************/