Wednesday, 4 September 2013

PROJECT EULER SOLUTION # 50

Solution to problem number 50 of Project Euler.
Question # 50

The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13
This is the longest sum of consecutive primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?
Solution # 50
/***********************************************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

int is_prime(long);

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

int main()
{
       long i,j,sum=0,temp_terms=0,terms=0,ans=0,temp,num=1000,a,actual_sum=0;
       for(i=2;i<num && ans<num;i++)
       {
              if(is_prime(i))
              {
                     sum=0;
                     temp_terms=0;
                     a=0;
                     for(j=i;;j++)
                     {
                          
                           temp=sum;
                    
                           if(is_prime(j))
                           {
                                  a++; // a counts the sequence of primes...
                                  sum+=j;
                                  if(sum>=num)
                                         break;
                                  if(is_prime(sum))
                                  {
                                         actual_sum=sum;
                                         temp_terms=a;
                                  }
                           }
                     }
                     if(temp_terms>terms)
                     {
                           terms=temp_terms;
                           ans=actual_sum;
                     }
              }
       }

       printf("%d\n",ans);
       printf("Execution time = %f\n",clock()/(float)CLK_TCK);
       system("pause");
}
/***********************************************************************************************************/


Tuesday, 3 September 2013

PROJECT EULER SOLUTION # 40



Solution to problem number 40 of Project Euler.
Question #40
An irrational decimal fraction is created by concatenating the positive integers:
0.123456789101112131415161718192021...
It can be seen that the 12th digit of the fractional part is 1.
If dn represents the nth digit of the fractional part, find the value of the following expression.
d1 ×d10 ×d100 ×d1000 ×d10000 ×d100000 ×d1000000

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

long count_digits(long);
long count_digits_upto(long);
int d(long);
int ith(long,long);

int main()
{
       long product=1;
       int i;
       for(i=0;i<7;i++)
              product*=d((long)pow(10.0,i));
       printf("%ld\n",product);
       printf("EXECUTION TIME = %f\n",clock()/(float)CLK_TCK);
       system("pause");
}

int d(long n)
{
       int ans;
       long i,tmp;
       i=getNumber(n);
       tmp=count_digits_upto(i-1);
       ans=ith(n-tmp,i);
       return ans;
}

int ith(long i,long n)
{
       int digits,j;
       digits=count_digits(n);
       for(j=0;j<digits-i;j++)
              n/=10;
       return n%10;
}
long count_digits_upto(long n)
{
       long counter=0,i;
       for(i=1;i<=n;i++)
              counter+=count_digits(i);
       return counter;
}

long getNumber(long n)
{
       long i,counter=0,product;
       for(i=1;i<=n;i++)
       {
              counter+=count_digits(i);
              if(counter>=n)
                     break;
       }
       return i;
}

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