Skip to content

Commit

Permalink
Refactor Fibonacci calculation and fix print_to_98 function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWatcher01 committed Apr 3, 2024
1 parent 0f1b66b commit 5e42712
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
31 changes: 15 additions & 16 deletions functions_nested_loops/104-fibonacci.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,30 @@
*/
int main(void)
{
/* The lower and upper parts of the current and next Fibonacci numbers */
unsigned long int a1 = 0, a2 = 1, b1 = 0, b2 = 2, next1, next2, count;
/* The first two Fibonacci numbers */
unsigned long int a = 1, b = 2;

/* The next Fibonacci number */
unsigned long int next;

/* The counter for the loop */
unsigned long int count;

/* Print the first two Fibonacci numbers */
printf("1, 2");
printf("%lu, %lu", a, b);

/* Calculate and print the next 96 Fibonacci numbers */
for (count = 2; count < 98; count++)
{
/* Calculate the next Fibonacci number */
next1 = a1 + b1;
next2 = a2 + b2 + (next1 / 1000000000);
next1 = next1 % 1000000000;
next = a + b;

/* Update the current two Fibonacci numbers */
a = b;
b = next;

/* Print the next Fibonacci number */
if (next2 > 0)
printf(", %lu%09lu", next2, next1);
else
printf(", %lu", next1);

/* Update the current and next Fibonacci numbers for the next loop iteration */
a1 = b1;
a2 = b2;
b1 = next1;
b2 = next2;
printf(", %lu", next);
}

/* Print a newline at the end */
Expand Down
16 changes: 3 additions & 13 deletions functions_nested_loops/11-print_to_98.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
#include "main.h"

/**
* main - check the code.
* print_to_98 - Prints all natural numbers from n to 98.
*
* Return: Always 0.
* @n: The number to start counting from.
*/
int main(void)
{
print_times_table(3);
_putchar('\n');
print_times_table(5);
_putchar('\n');
print_times_table(98);
_putchar('\n');
print_times_table(12);
return (0);
}

void print_to_98(int n)
{
int i;
Expand Down
Binary file removed functions_nested_loops/a.out
Binary file not shown.

0 comments on commit 5e42712

Please sign in to comment.