How Many Hanukkah Candles?
The Festival of 1 + n + f(n-1) Lights
As Hanukkah approached and I gathered my holiday supplies, I ran through my list to make sure I had everything I needed. Latkes? Check. Chcoclate Gelt? Double check. Menorah? Check. Candles? Check.... I think? How many am i going to need? I phoned a friend, they weren't sure. Now, I could have just googled like any sane person, but at this point the question meant more than the answer.
To me their was only one way I was going to answer this question: algorithmically.
#include <stdio.h>
#include <stdlib.h>
int candles(int n) {
if (n < 1) {
return 0;
}
return 1 + n + candles(n-1);
}
int main(int argc, char* argv[]) {
printf("%d\n", candles(atoi(argv[1])));
}
The answer is 44. Happy Hanukkah!
Search
Recent Posts
-
How Many Hanukkah Candles?
-
Pratt Parsing: Top-Down Operator Precedence
-
Generating P-Code by AST Traversal
-
Ternary Search Tries: String Specific Ordered Symbol Tables
-
Digital Search Trees
-
Lossless Compression Part III: Huffman Coding
-
Lossless Compression Part II: The LZ77 Algorithm
-
Lossless Compression Part I: Working with Bits in a Byte Oriented World
-
Bottom Up AVL Tree: The OG Self-Balancing Binary Search Tree
-
A Different Take on Merge Sort: Binary Search Trees?
Meta
Leave A Comment