Showing posts with label Project Euler. Show all posts
Showing posts with label Project Euler. Show all posts

Thursday, 5 January 2017

Project Euler #47: Distinct primes factors

Program:-

#include <bits/stdc++.h>
using namespace std;
 
void SieveOfEratosthenes(int n, int k,int m)
{
    vector<int>prime(n+1,0);
    for (int p=2; p<=n; p++)
    {
        if (prime[p] == 0)
        {
            for (int i=p*2; i<=n; i += p)
                prime[i]++;
        }
    }
    
    vector<int>vec;
    for (int p=2; p<=n; p++){
       if (prime[p]==k){ 
         if(vec.size()==0) {vec.push_back(p);}
           else if(vec[vec.size()-1]==p-1) vec.push_back(p);
               else {vec.resize(0);vec.push_back(p);}
       }
        if(vec.size()==k && vec[0]<=m){
            cout<<vec[0]<<endl;
            vec.erase(vec.begin(),vec.begin()+1);
        }
    }
}
 
int main()
{
    int n,k;
    cin>>n>>k;
    SieveOfEratosthenes(n+k+5,k,n);
    return 0;
}

Wednesday, 14 December 2016

10001st prime

Program:-

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int arr[10003];
    arr[1]=2;
    int n, i = 3, count, c;

   for ( count = 2 ; count <= 10003 ;  )
   {
      for ( c = 2 ; c <= i - 1 ; c++ )
      {
         if ( i%c == 0 )
            break;
      }
      if ( c == i )
      {
        arr[count]=i;
         count++;
      }
      i++;
   }
   
  int temp;
    scanf("%d",&temp);
    for(int i=0;i<temp;i++)
        {
        scanf("%d",&n);
        printf("%d\n",arr[n]);
        }
    return 0;
}