Skip to content

Instantly share code, notes, and snippets.

View fortune-c's full-sized avatar
🎯
Focusing

Fortunate Adesina fortune-c

🎯
Focusing
View GitHub Profile
@fortune-c
fortune-c / grains.c
Last active April 15, 2026 21:10
Calculating the umber of grains of wheat on a chessboard.
#include <stdio.h>
uint64_t square(uint8_t index) {
// the number of grains on a given square
if (index < 1 || index > 64) {
return 0;
}
return (uint64_t)1 << (index - 1);
}
@fortune-c
fortune-c / sum_square_diff.c
Last active April 14, 2026 19:29
Compute the difference between the square of the sum and the sum of the squares of the first N natural numbers using efficient mathematical formulas. A classic problem highlighting optimization and algorithmic thinking.
// Calculates (1 + 2 + ... + n)^2
unsigned int square_of_sum(unsigned int n) {
unsigned int sum = (n * (n + 1)) / 2;
return sum * sum;
}
// Calculates (1^2 + 2^2 + ... + n^2)
unsigned int sum_of_squares(unsigned int n) {
return (1LL * n * (n + 1) * (2 * n + 1)) / 6;
}
@fortune-c
fortune-c / leap.c
Last active April 14, 2026 16:23
A leap year (in the Gregorian calendar) occurs: In every year that is evenly divisible by 4. Unless the year is evenly divisible by 100, in which case it's only a leap year if the year is also evenly divisible by 400. Some examples: 1997 was not a leap year as it's not divisible by 4. 1900 was not a leap year as it's not divisible by 400. 2000 w…
bool leap_year(int year){
if(year % 400==0 || (year % 4 == 0 && year % 100! =0)) {
printf("%d is a leap year\n", year);
return true;
}
return 0;
}