generated from cpmachado/c-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Replicate computation of egyptian fractions * rename egc.c -> main.c * Rename lib * Integrate lib * Adapt linter checks * Check cppcheck version * Check help for cppcheck version 2.7 * Try to snap install cppcheck =2.14 * Remove -y from snap install * Remove redundant cppcheck commands in workflow
- Loading branch information
Showing
6 changed files
with
118 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
set noparent | ||
filter=-build/include_subdir,-build/header_guard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* Copyright 2024 cpmachado */ | ||
|
||
#define MAX_FRACTIONS_EGYPTIAN 100 | ||
|
||
typedef struct Fraction { | ||
int64_t num, den; | ||
} Fraction; | ||
|
||
typedef struct EgyptianFraction { | ||
int64_t dens[MAX_FRACTIONS_EGYPTIAN], n; | ||
} EgyptianFraction; | ||
|
||
int64_t computeEgyptianFraction(Fraction *fraction, EgyptianFraction *egyptian); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* Copyright 2024 cpmachado */ | ||
|
||
#include <stdint.h> | ||
#include <stdio.h> | ||
|
||
#include "egc.h" | ||
|
||
int64_t gcd(int64_t a, int64_t b) { | ||
int64_t c; | ||
while (b > 0) { | ||
if (a > b) { | ||
c = a; | ||
a = b; | ||
b = c % a; | ||
} else { | ||
b = b % a; | ||
} | ||
} | ||
return a; | ||
} | ||
|
||
int64_t computeEgyptianFraction(Fraction *fraction, | ||
EgyptianFraction *egyptian) { | ||
int64_t num = fraction->num, den = fraction->den; | ||
int64_t i, gcd_val, n; | ||
|
||
for (i = 0; num > 1; i++) { | ||
gcd_val = gcd(num, den); | ||
num /= gcd_val; | ||
den /= gcd_val; | ||
n = den / num + (den % num > 0); | ||
egyptian->dens[i] = n; | ||
if (INT64_MAX / n < num) { | ||
fprintf(stderr, "Overflow detected: (num, n) = (%ld, %ld)\n", num, n); | ||
return -1; | ||
} | ||
num = num * n - den; | ||
if (INT64_MAX / n < den) { | ||
fprintf(stderr, "Overflow detected: (den, n) = (%ld, %ld)\n", den, n); | ||
return -1; | ||
} | ||
den = den * n; | ||
} | ||
egyptian->dens[i] = den; | ||
i++; | ||
egyptian->n = i; | ||
return i; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters