1. Lars Yencken
  2. euler

Commits

Lars Yencken  committed 190289e

C++ 02.

  • Participants
  • Parent commits a6dcf2b
  • Branches master

Comments (0)

Files changed (2)

File cpp/02-sum-fibonacci.cpp

View file
  • Ignore whitespace
+/*
+ *
+ *  02-sum-fibonacci.c
+ *  euler
+ *
+ *  Created by Lars Yencken on 2012-03-01.
+ *  Copyright 2012 Lars Yencken. All rights reserved.
+ *
+ */
+
+#include <iostream>
+
+using namespace std;
+
+int sum_even_fibonacci(int n)
+{
+	int sum = 0;
+	int last = 1;
+	int curr = 2;
+	int tmp;
+
+	while (curr < n)
+	{
+		if (curr % 2 == 0)
+		{
+			sum += curr;
+		}
+
+		tmp = last;
+		last = curr;
+		curr += tmp;
+	}
+
+	return sum;
+}
+
+int main(int argc, const char *argv[])
+{
+	cout << sum_even_fibonacci(4000000) << endl;
+	return 0;
+}

File cpp/Makefile

View file
  • Ignore whitespace
+CXX = clang++
+
+ALL = 01-sum-numbers \
+	  02-sum-fibonacci
+
+all: $(ALL)
+
 01-sum-numbers: 01-sum-numbers.cpp
 	$(CXX) -o $@ $<
 
+02-sum-fibonacci: 02-sum-fibonacci.cpp
+	$(CXX) -o $@ $<
+
 clean:
-	rm -f 01-sum-numbers
+	rm -f $(ALL)
+