Skip to content

Commit

Permalink
Add extraLongFactorials implementation in C
Browse files Browse the repository at this point in the history
Implemented a function to calculate and print extremely large factorials using an array-based approach. Includes necessary utility functions for input handling and trimming. This addition supports calculating factorials for very large numbers without overflow.
  • Loading branch information
holman57 committed Dec 22, 2024
1 parent fb8538a commit c336f3e
Showing 1 changed file with 162 additions and 0 deletions.
162 changes: 162 additions & 0 deletions ExtraLongFactorials.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();
char* ltrim(char*);
char* rtrim(char*);

int parse_int(char*);

/*
* Complete the 'extraLongFactorials' function below.
*
* The function accepts INTEGER n as parameter.
*/

void extraLongFactorials(int n) {
// Array to store large numbers as digits
int result[1000];
result[0] = 1; // Initialize with 1
int result_size = 1;

// Multiply numbers from 2 to n
for (int x = 2; x <= n; x++) {
int carry = 0;

// Multiply x with the current result
for (int i = 0; i < result_size; i++) {
int prod = result[i] * x + carry;
result[i] = prod % 10; // Store last digit in the array
carry = prod / 10; // Get the carry
}

// Store remaining carry in result array
while (carry) {
result[result_size] = carry % 10;
carry /= 10;
result_size++;
}
}

// Print the result in reverse (since the most significant digit is at the end)
for (int i = result_size - 1; i >= 0; i--) {
printf("%d", result[i]);
}
printf("\n");
}

int main()
{
int n = parse_int(ltrim(rtrim(readline())));

extraLongFactorials(n);

return 0;
}

char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;

char* data = malloc(alloc_length);

while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);

if (!line) {
break;
}

data_length += strlen(cursor);

if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
break;
}

alloc_length <<= 1;

data = realloc(data, alloc_length);

if (!data) {
data = '\0';

break;
}
}

if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';

data = realloc(data, data_length);

if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);

if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}

return data;
}

char* ltrim(char* str) {
if (!str) {
return '\0';
}

if (!*str) {
return str;
}

while (*str != '\0' && isspace(*str)) {
str++;
}

return str;
}

char* rtrim(char* str) {
if (!str) {
return '\0';
}

if (!*str) {
return str;
}

char* end = str + strlen(str) - 1;

while (end >= str && isspace(*end)) {
end--;
}

*(end + 1) = '\0';

return str;
}

int parse_int(char* str) {
char* endptr;
int value = strtol(str, &endptr, 10);

if (endptr == str || *endptr != '\0') {
exit(EXIT_FAILURE);
}

return value;
}

0 comments on commit c336f3e

Please sign in to comment.