ProjectEuler

ScalaでProjectEuler 1 to 10

#!/usr/bin/scala !# { println((0 to 999 toList).filter(s => (s%3==0 || s%5==0)).foldLeft(0)((a,b)=>a+b)) } { def f(x:Int,y:Int,s:Int):Int = if (x <= 4000000) f(y,x+y,x*(1-x%2) + s) else s println(f(1,2,0)) } { def f(n:Long,x:Long):Long = i…

ProjectEuler 1-10 Ruby

#!/usr/bin/ruby require "rational" def p1 p (0...1000).select {|i|i%3==0||i%5==0}.inject(0) {|a,b|a+b} end def p2 a,b = 1,1 sum = 0 while b<=4000000 sum += b if b%2==0 a,b = b,a+b end p sum end def p3 a=600851475143;(2..a).each{|i|i

ProjectEuler 3 C++TMP

#include <iostream> template<long long int p,long long int i=2> struct p3 { //static const bool per = p%i==0; static const long long int value = p3<p%i==0?p/i:p,p%i==0?i:i+1>::value; }; template<long long int p> struct p3<p,p> { static const long long int value = p; }; int main() { std::cout << p3<6…</p,p></long></p%i==0?p/i:p,p%i==0?i:i+1></long></iostream>

ProjectEuler 3 Ruby

#!/usr/bin/ruby #ans 1 a=600851475143 i=2 while i

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}