-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
135 lines (111 loc) · 3.09 KB
/
main.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
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "errors.h"
#include "utils.h"
bool is_pandigital(int number){
int digit_count[10] = {0};
int total_count = 0;
while(number > 0){
int digit = number % 10;
digit_count[digit]++;
total_count++;
number /= 10;
}
if (digit_count[0]>0){
return false;
}
for (int i=1; i<=9; i++){
if(digit_count[i]!=1){
return false;
}
}
return total_count == 9;
}
bool is_array_pandigital(int* numbers_array, int array_size){
int digit_count[10] = {0};
int total_count = 0;
for(int i=0; i<array_size;i++){
int number = numbers_array[i];
while(number > 0){
int digit = number % 10;
digit_count[digit]++;
total_count++;
number /= 10;
}
}
if (digit_count[0]>0){
return false;
}
for (int i=1; i<=9; i++){
if(digit_count[i]!=1){
return false;
}
}
return total_count == 9;
}
int digit_length(int number) {
int length = 0;
if (number < 0) {
number = -number;
}
do {
length++;
number /= 10;
} while (number != 0);
return length;
}
int array_digit_length(int arr[], int size) {
int cumulative_length = 0;
for (int i = 0; i < size; i++) {
cumulative_length += digit_length(arr[i]);
}
return cumulative_length;
}
int concatenate_array(int arr[], int size) {
int concatenated = 0;
for (int i = 0; i < size; i++) {
concatenated *= pow(10, digit_length(arr[i]));
concatenated += arr[i];
}
return concatenated;
}
int main(int argc, char* argv[]){
clock_t start = clock();
int max = 0;
for(int j=3; j<=10;j++){
for (int i=0;i<1000000000;i++){
result range_result = int_range(1, j, 1);
if(range_result.Err != NULL){
fprintf(stderr, "Error: %s\n", range_result.Err);
goto fail;
}
int_range_struct* range_struct = range_result.Ok;
int* products = multiply_by_each(range_struct->values, range_struct->size, i);
bool break_flag = false;
if (array_digit_length(products, range_struct->size) > 9){
break_flag = true;
}
if(is_array_pandigital(products, range_struct->size)){
int concatenated_pandigital = concatenate_array(products, range_struct->size);
if (concatenated_pandigital > max){
max = concatenated_pandigital;
}
}
free(products);
free_int_range_result(range_struct);
free_result(range_result);
if (break_flag == true){
break;
}
}
}
clock_t stop = clock();
double elapsed = (double)(stop - start)/CLOCKS_PER_SEC;
printf("maximum pandigital=%d (elapsed = %fs)\n", max, elapsed);
return EXIT_SUCCESS;
fail:
return EXIT_FAILURE;
}