Skip to content

Instantly share code, notes, and snippets.

@l0gicpath
l0gicpath / problem_2.erl
Last active August 29, 2015 13:55
Project Euler Problem #2
-module(problem_2).
-export([solve/1]).
-define(LIMIT, 4000000).
%% Normalizing the start value, need to start with an odd value and enter tail recursion
solve(Start) when Start rem 2 == 0, Start > 0 -> solve(Start - 1, Start, 0);
solve(Start) -> solve(Start, Start + 1, 0).
%% guard against the limit and drop out of recursion if we hit it
solve(Prev, Next, Sum) when Prev >= ?LIMIT; Next >= ?LIMIT -> Sum;
%% Sum even values
@gr33ndata
gr33ndata / gist:8727727
Created January 31, 2014 07:08
Even Fibonacci numbers
# Solving this problem:
# http://projecteuler.net/problem=2
# Sum: 4,613,732
def fab(a,b, maxlimit):
while b < maxlimit:
yield b
a, b = b, a+b
@EslaMx7
EslaMx7 / ProjectEuler.P2.cs
Last active December 26, 2018 06:16
Project Euler Problem 2 Solution with C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsolesTests
{
class Program
{
@l0gicpath
l0gicpath / problem_2.erl
Created January 31, 2014 13:35
Because my other 5 line Erlang solution was too long.
-module(problem_2).
-export([s/0]).
-define(L, 4000000).
s() -> s(1, 2, 0).
s(P, N, S) when P >= ?L; N >= ?L -> S;
s(P, N, S) -> S1 = case N rem 2 of 0 -> N + S; _ -> S end, s(N, P + N, S1).
@l0gicpath
l0gicpath / one_liner.rb
Last active August 29, 2015 13:55
One liner ruby solution for project euler problem #2 because ruby is hardcore
(l ,ss = 4000000, proc {|p, n, s| p >= l || n >= l ? s : n.even?? ss[n, n + p, s + n] : ss[n, n + p, s] })[1][1, 2, 0]
def sieve(numbers, n):
for i in range(n * 2, len(numbers), n):
numbers[i] = False
def skip_to_next_prime(numbers, n):
n += 1
while n < max and not numbers[n]:
n += 1
return n
@gr33ndata
gr33ndata / gist:8848854
Created February 6, 2014 17:33
Prime numbers generator!
# Using Sieve of Eratosthenes
# https://en.wikipedia.org/wiki/Generating_primes
import sys
def main():
num = int(sys.argv[1])
num_list = [[i,True] for i in range(num)]
num_list[0][1] = False
num_list[1][1] = False
using System;
using System.Collections.Generic;
using System.Text;
namespace Largest_prime_factor
{
class Program
{
static void Main(string[] args)
{
@EslaMx7
EslaMx7 / ProjectEuler.P3.cs
Created February 7, 2014 12:14
Project Euler Problem 3 Solution with C#
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsolesTests
{
class Program
{
static void Main(string[] args)
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LargestPrimeFactor
{
class Program
{