2009-02-09から1日間の記事一覧

ProjectEuler 2 Ruby

a,b = 1,1 sum = 0 while b<=4000000 sum += b if b%2==0 a,b = b,a+b end p sum

ProjectEuler 2 C++TMP

#include <iostream> template<int a,int b> struct intlt { static const bool value = a < b; }; template<bool inrange=true,int a=1,int b=1> struct fibsum { static const int value = fibsum<intlt<a+b,4000000>::value,b,a+b>::value + (b%2==0?b:0); }; template<int a,int b> struct fibsum<false,a,b> { static …</false,a,b></int></intlt<a+b,4000000></bool></int></iostream>

ProjectEuler 1 C++TMP

#include <iostream> template<int i=0> struct sum35 { static const int value = sum35<i+1>::value + ((i%3==0 || i%5==0)?i:0); }; template<> struct sum35<1000> { static const int value = 0; }; int main() { std::cout << sum35<0>::value << std::endl; return 0; }</i+1></int></iostream>

ProjectEuler 1 Ruby

#!/usr/bin/ruby #ans 1 p (0...1000).select {|i|i%3==0||i%5==0}.inject(0) {|a,b|a+b} #ans 2 require "enumerator" p ((0...1000).to_enum(:step,3).to_a+(0...1000).to_enum(:step,5).to_a).uniq.inject(0) {|a,b|a+b}