-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodfunc_.c
181 lines (153 loc) · 4.28 KB
/
modfunc_.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
int strlen(char *a)
{
int c=0;
for (;*a!='\0'&&*a!='\n';c++,a++);
return c;
}
void *sub(char *a,char *b,char c[]) // c=a-b ; c should be >=0
{
char ints[]="0123456789";
int b_size=strlen(b);
int a_size=strlen(a);
int max_size=b_size>a_size?b_size:a_size; // max_size of c
for (;*a;a++);
for (;*b;b++);
a--;// last digit of a
b--; // last digit of b
int c1=0;
int carryon=0; // carry-on
for (;c1<b_size&&c1<a_size;c1++,a--,b--)
{
c[max_size-c1-1]=
ints[((int)((*a-'0')-(*b-'0')-carryon>=0?(*a-'0')-(*b-'0')-carryon:((*a-'0')-(*b-'0')-carryon+10)))%10];
if (*a-'0'-carryon>=*b-'0')carryon=0;
else carryon=1;
}
for (;c1<a_size;c1++,a--)
{
c[max_size-c1-1]=ints[((*a-'0')-carryon>=0?(*a-'0')-carryon:(*a-'0')-carryon+10)%10];
if (*a-'0'>=carryon)carryon=0;
else carryon=1;
}
c1=0;
for (;c[c1]=='0'&&c1<strlen(c);c1++);
int c2=0;
for (;c1<strlen(c);c2++,c1++)
c[c2]=c[c1];
c[c2]='\0';
}
void *add(char *a,char *b,char c[]) // c=a+b : c can have leading 0
{
char ints[]="0123456789";
int b_size=strlen(b);
int a_size=strlen(a);
int max_size=b_size>a_size?1+b_size:1+a_size; // max_size of c with carry
for (;*a;a++);
for (;*b;b++);
a--;// last digit of a
b--; // last digit of b
int c1=0;
int carryon=0; // carry-on
for (;c1<b_size&&c1<a_size;c1++,a--,b--)
{
c[max_size-c1-1]=ints[(carryon+*a-'0'+*b-'0')%10];
carryon=(*a-'0'+*b-'0'+carryon)/10;
}
for (;c1<b_size;c1++,b--)
{
c[max_size-c1-1]=ints[(carryon+*b-'0')%10];
carryon=(carryon+*b-'0')/10;
}
for (;c1<a_size;c1++,a--)
{
c[max_size-c1-1]=ints[(carryon+*a-'0')%10];
carryon=(carryon+*a-'0')/10;
}
c[0]=ints[carryon];
c[max_size]='\0';
if (c[0]=='0')
{
for (int c1=0;c1<max_size;c1++)
c[c1]=c[c1+1];
}
}
bool comp(char *a,char *b) // returns a>=b
{
for (;*a!='\0'&&*a=='0';a++); // omitting leading zeros
for (;*b!='\0'&&*b=='0';b++); // omitting leading zeros
if (strlen(a)!=strlen(b))return strlen(a)>strlen(b);
for (;*a!='\0'&&*b!='\0'&&*a==*b;a++,b++);
return *a-'0'>=*b-'0';
}
bool is_mod(char *dividend,char *divisior) // dividend%divisior
{
char sum[1002];
int size=strlen(dividend);
sum[0]='\0';
for (int c=0;c<size;c++,dividend++)
{
int sum_counter=0;
for (;sum[sum_counter];sum_counter++);
sum[sum_counter]=*dividend;
sum[sum_counter+1]='\0';
if (comp(sum,divisior)) // sum>=divisor
{
while(comp(sum,divisior))
{
char c[1002];
sub(sum,divisior,c); // c=sum-divisor
int c1=0;
sum_counter=0;
for (;c1<strlen(c);c1++)
{
sum[c1]=c[c1];
}
sum[c1]='\0';
}
}
}
for (int c=0;c<strlen(sum);c++)
{
if (sum[c]!='0')
return 0;
}
return 1;
}
void strcpy(char *a,char *b) // copies b to a
{
for (;*b;b++,a++)
*a=*b;
*a='\0';
return ;
}
void printf_factors(char p[1000])
{
char a[1000];
a[0]='0';
int total_factors=0;
for (;comp(p,a);)
{
char c[strlen(a)+2];
add(a,"1",c);
strcpy(a,c); // copies c to a
if (is_mod(p,a)>0)
{
total_factors++;
printf("%s, ",a);
fflush(stdout);
}
}
printf("%d\n",total_factors);
return ;
}
int main()
{
char a[1000];
scanf("%s",a);
a[strlen(a)]='\0';
printf_factors(a);
printf("%d\n",is_mod("100000","60"));
}