-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob32.cpp
101 lines (101 loc) · 1.48 KB
/
prob32.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <stdlib.h>
int isPandigital(long long int n,int size){
int d[size],i,t;
for(i=0;i<size;i++){
d[i]=0;
}
i=0;
while(n!=0){
t=n%10;
if(t==0){
return 0;
}
if(t>size){
//number is not pandigital of size "size"
return 0;
}
if(d[t-1]==1){
return 0;
}else{
d[t-1]=1;
}
n=n/10;
}
return 1;
}
int getSize(long long int n){
int t=0;
while(n!=0){
n=n/10;
t++;
}
return t;
}
unsigned long long int giveNum(int i,int j,int p){
unsigned long long int n=0;
n=i;
int t;
t=getSize(j);
while(t!=0){
t--;
n=n*10;
}
n=n+j;
t=getSize(p);
while(t!=0){
t--;
n=n*10;
}
n=n+p;
return n;
}
int main(){
int i,j,p;
int hash[10000];
for(i=0;i<10000;i++){
hash[i]=0;
}
unsigned long long int x,sum=0;
for(i=1;i<=9;i++){
for(j=1;j<=9999;j++){
p=i*j;
x=giveNum(i,j,p);
if(getSize(x)!=9){
continue;
}
if(isPandigital(x,getSize(x))){
printf("\n%d * %d = %d",i,j,p);
if(p>10000){
printf("\nneed more hashing ,,, upto %d\n",p);
exit(0);
}
if(hash[p-1]==0){
sum=sum+p;
hash[p-1]=1;
}
}
}
}
for(i=10;i<=99;i++){
for(j=1;j<=999;j++){
p=i*j;
x=giveNum(i,j,p);
if(getSize(x)!=9){
continue;
}
if(isPandigital(x,getSize(x))==1){
printf("\n%d * %d = %d",i,j,p);
if(p>10000){
printf("\nneed more hashing ,,, upto %d\n",p);
exit(0);
}
if(hash[p-1]==0){
sum=sum+p;
hash[p-1]=1;
}
}
}
}
printf("\nANS = %llu\n",sum);
}