Tuesday, 3 April 2018

hackerrank solution: Project Euler #34: Digit factorials

solution : python2

# Enter your code here. Read input from STDIN. Print output to STDOUT
a = [0]*10
a[0]=1

for i in range(1,10):
    a[i] = int(a[i-1] * i)

n = int(input())
ans = 0

for i in range(10,n+1):
    temp = int(i)
    sum = 0
    while temp > 0 :
        sum += a[int(temp%10)]
        temp//=10

    sum = int(sum)
    if sum % i == 0 :
        ans += i

print (int(ans))

Project Euler #11: Largest product in a grid

HACKERRANK SOLUTION: c++

#include<iostream>
using namespace std;
int main(){
    long long a[20][20],max=0,p;
    for(int i=0;i<20;i++){
        for(int j=0;j<20;j++){
          cin>>a[i][j]; 
        }
    }   
    for(int i=0;i<20;i++){
        for(int j=0;j<=16;j++){
            p = a[i][j]*a[i][j+1]*a[i][j+2]*a[i][j+3];
            if(p> max) max=p;
       
            p=a[j][i]*a[j+1][i]*a[j+2][i]*a[j+3][i];
            if(p> max) max=p;
        }
    }   


    for(int i=0;i<17;i++){
        for(int j=0;j<17;j++){
            p= a[i][j]*a[i+1][j+1]*a[i+2][j+2]*a[i+3][j+3];
            if(p> max) max=p;
        }
    }
    for(int i=3;i<20;i++){
        for(int j=0;j<17;j++){

        p=a[i][j]*a[i-1][j+1]*a[i-2][j+2]*a[i-3][j+3];
        if(p> max) max=p;
        }
    }   

    cout<<max;
    return 0;

}

Project Euler #38: Pandigital multiples; Hackerrank

Take the number  and multiply it by each of , and 
By concatenating each product we get the  to  pandigital, . We will call  the concatenated product of  and 
The same can be achieved by starting with  and multiplying by , and , giving the pandigital, , which is the concatenated product of  and . Let's call 9 as the Multiplier 
The similar process can be shown for  to  pandigital also.  when multiplied by  gives  which is  pandigital.
You are given  and  where  =  or , find the multipliers for that given  below  and print them in ascending order.
Input Format
Input contains two integer  and .
Constraints
 
 
Output Format
Print the answer corresponding to the test case.
Sample Input
100 8
Sample Output
18
78
solution: c++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f(a,b,c)                for(int a=b;a<c;a++)
#define s(x)                    scanf("%d",&x);
#define sl(x)                   scanf("%lld",&x);
#define p(x)                    printf("%d\n",x);
#define p2(x,y)                 printf("%d %d\n",x,y);
#define pl(x)                   printf("%lld\n",x);
#define pl2(x,y)                 printf("%lld %lld\n",x,y);
#define p1d(a,n)                for(int ix=0;ix<n;ix++) printf("%d ",a[ix]); printf("\n");
#define p2d(a,n,m)              for(int ix=0;ix<n;ix++){ for(int jx=0;jx<m;jx++) printf("%d ",a[ix][jx]); printf("\n");}
void input(){
    #ifdef Megamind
            #define DEBUG
            #define TRACE
            #define gc getchar()
            freopen("inp.txt","r",stdin);
            //freopen("out1.txt","w",stdout);
    #else
            #define gc getchar_unlocked()
    #endif
}
#ifdef TRACE
    #define trace(x)                       cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
    #define trace2(x,y)                    cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
    #define trace3(x,y,z)                  cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
#else
    #define trace(x)
    #define trace2(x,y)
    #define trace3(x,y,z) 
#endif
template <class T>
inline void read(T &n1){
 n1=0;
    char c=gc;
    while(c<'0' || c>'9') c=gc;
    while(c>='0' && c<='9'){
       n1=(n1<<3)+(n1<<1)+c-'0';
       c=gc;
    }
}
inline ll power(ll a, ll b, ll m) {
    ll r = 1;
    while(b) {
        if(b & 1) r = r * a % m;
        a = (a * a)% m;
        b >>= 1;
    }
    return r;
}
inline ll power(ll a, ll b) {
     ll r = 1;
    while(b) {
        if(b & 1) r = r * a ;
        a = a * a;
        b >>= 1;
    }
    return r;
}
 
/*........................................................END OF TEMPLATES.......................................................................*/
int digit[10];

bool check(int num , int k){
 
 bool ok = 1;
 int temp ,mod,counter=0;
 memset(digit,0,sizeof(digit));
 
 f(i,1,10){
        if(counter >= k) break;
  temp = num*i;
  
  while(temp){
   
   mod = temp%10;
   
   if(!mod || mod>k || digit[mod])  return !ok;
   digit[mod]=1;
   
            counter++;
   temp/=10;
  }
 }
    
 return (counter == k);
   
}

int main(){
 input();
 int n,k;
 
 s(n);
 s(k);
 f(i,2,n)
  if(check(i,k))
     p(i);
 
 #ifdef Megamind
  cout << "Time elapsed: "<< 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
        #endif
}