Skip to content

Instantly share code, notes, and snippets.

@veeara282
Last active April 9, 2016 13:36
Show Gist options
  • Save veeara282/15576f99dcaf4141a229dba90cd37f86 to your computer and use it in GitHub Desktop.
Save veeara282/15576f99dcaf4141a229dba90cd37f86 to your computer and use it in GitHub Desktop.
Hacking Sessions
*~
*.o
a.out
#include <stdio.h>
int main() {
// First two Fibonacci numbers are 0 and 1
// The (-1)th Fibonacci number is 1
int prev = 1, cur = 0, next = 1;
int total = 0;
while (cur < 4000000) {
if (cur % 2 == 0) {
printf("%d\n", cur);
total += cur;
}
next = cur + prev;
prev = cur;
cur = next;
}
printf("--------\n%d\n", total);
}
#include <stdio.h>
int main() {
// First two Fibonacci numbers are 0 and 1
// The (-1)th Fibonacci number is 1
int prev = 1, cur = 0, next = 1;
while (cur <= 144) {
printf("%d\n", cur);
next = cur + prev;
prev = cur;
cur = next;
}
}

HW #0

  1. Write a C program that prints the Fibonacci sequence from 0 to 144. (Hint: Use three variables, printf, and a while loop.)
  2. For extra credit, write another program that prints the ratios of consecutive Fibonacci numbers to four decimal places, from 1/1 to 144/89. What number do these ratios approximate?

Paste your code (a single C file) into a GitHub Gist and post the link to our Slack channel by Friday at 5pm. I'll post a solution to both the homework and the extra credit here between then and our next meeting.

GitHub Gists are like Pastebin in that they're used to share code snippets and can be embedded in Web pages. However, as this post explains, Gists are full GitHub repositories, so they can be cloned, forked, and pushed like regular repos. In addition, Gists can contain multiple files, which is what I'll do here to post lesson plans and homework assignments over the next couple of months.

If you do the extra credit, put the code in a second file in the same Gist and clearly label which file is the homework and which file is the extra credit using a comment near the top of one or both files.

We're not at school so these homeworks won't be for a grade. They're just so that I can assess how well I'm teaching you guys. Of course you don't have to do them, but I'll make these homeworks fun so you'll want to do them.

Mentors, do them too!

On Saturday, we'll look at pointers, arrays, strings, and memory allocation. We might also talk about header files, the C compiler, and makefiles, and if we're lucky we'll get a head start on file I/O. See ya then!!

-- Aidan

#include <stdio.h>
int main() {
int prev = 1, cur = 1, next = 2;
while (cur <= 987) {
float ratio = (float) cur / prev;
printf("%.4f = %d / %d\n", ratio, cur, prev);
// printf("%d\n", cur);
next = cur + prev;
prev = cur;
cur = next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment