|
| 1 | +#include <assert.h> |
| 2 | +#include <ctype.h> |
| 3 | +#include <limits.h> |
| 4 | +#include <math.h> |
| 5 | +#include <stdbool.h> |
| 6 | +#include <stddef.h> |
| 7 | +#include <stdint.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +char* readline(); |
| 13 | +char* ltrim(char*); |
| 14 | +char* rtrim(char*); |
| 15 | + |
| 16 | +int parse_int(char*); |
| 17 | + |
| 18 | +void kaprekarNumbers(int p, int q) { |
| 19 | + int found = 0; // To track if we find any Kaprekar number |
| 20 | + |
| 21 | + for (int i = p; i <= q; i++) { |
| 22 | + long square = (long)i * i; // Compute the square of the number |
| 23 | + char square_str[20]; // To hold the square as string |
| 24 | + sprintf(square_str, "%ld", square); // Convert square to string |
| 25 | + int len = strlen(square_str); // Total number of digits in the square |
| 26 | + |
| 27 | + char left_part[20] = {0}, right_part[20] = {0}; |
| 28 | + int split_pos = len - (int)log10(i) - 1; // Calculate split position |
| 29 | + |
| 30 | + // Split the string |
| 31 | + strncpy(left_part, square_str, split_pos); // Left part |
| 32 | + strcpy(right_part, square_str + split_pos); // Right part |
| 33 | + |
| 34 | + // Convert parts to integers |
| 35 | + long left_num = (left_part[0] != '\0') ? atol(left_part) : 0; |
| 36 | + long right_num = atol(right_part); |
| 37 | + |
| 38 | + // Sum the parts and check if it equals the original number |
| 39 | + if ((left_num + right_num) == i) { |
| 40 | + printf("%d ", i); |
| 41 | + found = 1; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (!found) { |
| 46 | + printf("INVALID RANGE"); |
| 47 | + } |
| 48 | + |
| 49 | + printf("\n"); // End the output with a newline |
| 50 | +} |
| 51 | + |
| 52 | +int main() |
| 53 | +{ |
| 54 | + int p = parse_int(ltrim(rtrim(readline()))); |
| 55 | + |
| 56 | + int q = parse_int(ltrim(rtrim(readline()))); |
| 57 | + |
| 58 | + kaprekarNumbers(p, q); |
| 59 | + |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | +char* readline() { |
| 64 | + size_t alloc_length = 1024; |
| 65 | + size_t data_length = 0; |
| 66 | + |
| 67 | + char* data = malloc(alloc_length); |
| 68 | + |
| 69 | + while (true) { |
| 70 | + char* cursor = data + data_length; |
| 71 | + char* line = fgets(cursor, alloc_length - data_length, stdin); |
| 72 | + |
| 73 | + if (!line) { |
| 74 | + break; |
| 75 | + } |
| 76 | + |
| 77 | + data_length += strlen(cursor); |
| 78 | + |
| 79 | + if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { |
| 80 | + break; |
| 81 | + } |
| 82 | + |
| 83 | + alloc_length <<= 1; |
| 84 | + |
| 85 | + data = realloc(data, alloc_length); |
| 86 | + |
| 87 | + if (!data) { |
| 88 | + data = '\0'; |
| 89 | + |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (data[data_length - 1] == '\n') { |
| 95 | + data[data_length - 1] = '\0'; |
| 96 | + |
| 97 | + data = realloc(data, data_length); |
| 98 | + |
| 99 | + if (!data) { |
| 100 | + data = '\0'; |
| 101 | + } |
| 102 | + } else { |
| 103 | + data = realloc(data, data_length + 1); |
| 104 | + |
| 105 | + if (!data) { |
| 106 | + data = '\0'; |
| 107 | + } else { |
| 108 | + data[data_length] = '\0'; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return data; |
| 113 | +} |
| 114 | + |
| 115 | +char* ltrim(char* str) { |
| 116 | + if (!str) { |
| 117 | + return '\0'; |
| 118 | + } |
| 119 | + |
| 120 | + if (!*str) { |
| 121 | + return str; |
| 122 | + } |
| 123 | + |
| 124 | + while (*str != '\0' && isspace(*str)) { |
| 125 | + str++; |
| 126 | + } |
| 127 | + |
| 128 | + return str; |
| 129 | +} |
| 130 | + |
| 131 | +char* rtrim(char* str) { |
| 132 | + if (!str) { |
| 133 | + return '\0'; |
| 134 | + } |
| 135 | + |
| 136 | + if (!*str) { |
| 137 | + return str; |
| 138 | + } |
| 139 | + |
| 140 | + char* end = str + strlen(str) - 1; |
| 141 | + |
| 142 | + while (end >= str && isspace(*end)) { |
| 143 | + end--; |
| 144 | + } |
| 145 | + |
| 146 | + *(end + 1) = '\0'; |
| 147 | + |
| 148 | + return str; |
| 149 | +} |
| 150 | + |
| 151 | +int parse_int(char* str) { |
| 152 | + char* endptr; |
| 153 | + int value = strtol(str, &endptr, 10); |
| 154 | + |
| 155 | + if (endptr == str || *endptr != '\0') { |
| 156 | + exit(EXIT_FAILURE); |
| 157 | + } |
| 158 | + |
| 159 | + return value; |
| 160 | +} |
0 commit comments