Skip to content

Instantly share code, notes, and snippets.

@wdv4758h
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save wdv4758h/9cbf936784cbb472b52d to your computer and use it in GitHub Desktop.

Select an option

Save wdv4758h/9cbf936784cbb472b52d to your computer and use it in GitHub Desktop.
Project Euler #1
print(sum(0 if x % 3 and x % 5 else x for x in range(1000)))
import functools
L = []
def p(c=L, s=0, v=0):
return functools.partial(c[0], L, s+(v%3==0 or v%5==0)*v, v+1)
L.append(p)
for i in range(1000):
p = p()
print(p.args[1])
def compute(t,s,N):
r"""
>>> compute(3,5,1000)
233168
"""
a = "({h}+1)*{h}*{c}//2".format
b = "a(h={m}//{0},c={0}), a(h={m}//{1},c={1}), a(h={m}//{2},c={2})".format
c = "b({t},{s},{t}*{s},m={N}-1)".format
d = "{}+{}-{}".format
return eval(d(*eval(eval(c(**locals())))))
import functools
def f(s=0, v=0):
return functools.partial(f, s+(v%3==0 or v%5==0)*v, v+1)
p = f
for i in range(1000):
p = p()
print(p.args[0])
check = __import__('re').compile('(?=(.{3})*$)|(?=(.{5})*$)').match
sum(i if check('.'*i) else 0 for i in range(1000))
def p(v, s=0):
if v is None:
return s
else:
p.__defaults__= s+(v%3*v%5<1)*v,
for i in range(1000):
p(i)
print(p(None))
def euler1(func):
s, v = func.__defaults__
def wrapper():
nonlocal s, v
s, v = (s+(v%3*v%5<1)*v, v+1)
wrapper.__defaults__ = (s, v)
return wrapper
return wrapper
@euler1
def f(s=0, v=0):
pass
for i in range(1000):
f = f()
print(f.__defaults__[0])
def euler1(func):
def wrapper():
s, v = wrapper.__defaults__
s, v = (s+(v%3*v%5<1)*v, v+1)
wrapper.__defaults__ = (s, v)
return wrapper
wrapper.__defaults__ = func.__defaults__
return wrapper
@euler1
def f(s=0, v=0):
pass
for i in range(1000):
f = f()
print(f.__defaults__[0])
class euler:
s = 0
v = 0
def __getitem__(self, index):
s, v = self.s, self.v
s, v = (s+(v%3*v%5<1)*v, v+1)
self.s, self.v = s, v
e = euler()
for i in range(1000):
e[[[[[[[[]]]]]]], 3.14 and not "apua", i, e, IOError, ...]
print(e.s)
#![feature(iter_arith)] // for .sum()
// unstable library feature 'iter_arith' : bounds recently changed
fn p1_sol1() -> u64 {
// formula solution
// 3, 6, 9, ...
// 5, 10, 15, ...
// 15, 30, 45, ...
// 3 * 1, 3 * 2, 3 * 3, ..., 3 * a => sum: 3 * a * (1 + a) / 2
// 5 * 1, 5 * 2, 5 * 3, ..., 5 * b => sum: 5 * b * (1 + b) / 2
// 15 * 1, 15 * 2, 15 * 3, ..., 15 * c => sum: 15 * c * (1 + c) / 2
fn sum_range(start: u64, end: u64) -> u64 {
end * (start + end) / 2
}
fn sum_multiples_of_two_below(base_num: &[u64], end: u64) -> u64 {
let v1 = base_num[0];
let v2 = base_num[1];
let a = (end - 1) / v1;
let b = (end - 1) / v2;
let c = (end - 1) / (v1 * v2);
let sum_a = v1 * sum_range(1, a);
let sum_b = v2 * sum_range(1, b);
let sum_c = (v1 * v2) * sum_range(1, c);
sum_a - sum_c + sum_b
}
sum_multiples_of_two_below(&[3, 5], 1000)
// [TODO]
// make function more general
// make sure the array's elements is unique (no repeat)
// generate combinations (for more base numbers)
// -C opt-level=3 :
//
// p1_sol1::h7b3c1cda8687c063eaa:
// cmpq %fs:112, %rsp
// ja .LBB0_2
// movabsq $8, %r10
// movabsq $0, %r11
// callq __morestack
// retq
// .LBB0_2:
// pushq %rbp
// movq %rsp, %rbp
// movl $233168, %eax
// popq %rbp
// retq
//
// -C opt-level=3 -C no-stack-check :
//
// p1_sol1::h7b3c1cda8687c063eaa:
// pushq %rbp
// movq %rsp, %rbp
// movl $233168, %eax
// popq %rbp
// retq
}
fn p1_sol2() -> u64 {
// -C opt-level=3 => inline to main (not calculate at compile time)
// simple filter solution
(1..1000)
.filter(|&n| (n % 3 == 0) || (n % 5 == 0))
.sum()
}
fn p1_sol3() -> u64 {
// general filter solution
fn sum_multiples_of_base_below(base_num: &[u64], start: u64, end: u64) -> u64 {
(start..end)
.filter(|&n| base_num.iter().any(|&base| n % base == 0))
.sum()
}
sum_multiples_of_base_below(&[3, 5], 1, 1000)
}
fn p1_sol4() -> u64 {
// macro version for filter
macro_rules! sum_multiples_of_base_below {
( OωO $($base:expr),+ OωO , $start:expr, $end:expr ) => {
($start..$end)
.filter(|&n| $(n % $base == 0)||+)
.sum()
}
}
sum_multiples_of_base_below!(OωO 3, 5 OωO, 1, 1000)
}
fn p1_sol5() -> u64 {
// closure
let mut s = 0;
let mut v = 0;
{
// only borrow in this scope
let mut f = || {
v = v + 1;
s = s + ((v % 3) * (v % 5) < 1) as u64 * v;
};
for _ in 1..1000 {
f();
}
}
s
}
fn p1_sol6() -> u64 {
// static variable
fn f() -> u64 {
static mut s: u64 = 0;
static mut v: u64 = 0;
// static mut is unsafe
unsafe {
v = v + 1;
s = s + ((v % 3) * (v % 5) < 1) as u64 * v;
s
}
}
let mut result = 0;
for _ in 1..1000 {
result = f();
}
result
}
fn p1_sol7() -> u64 {
struct Euler {
s: u64,
v: u64,
}
impl Iterator for Euler {
type Item = u64;
fn next(&mut self) -> Option<u64> {
let s = self.s;
let v = self.v;
let s = s + ((v % 3) * (v % 5) < 1) as u64 * v;
let v = v + 1;
self.s = s;
self.v = v;
Some(self.s)
}
}
let euler = Euler { s: 0, v: 0 };
euler.skip(1000-1)
.next().unwrap_or(0)
}
fn main() {
// sum of all the multiples of 3 or 5 below 1000
// ans : 233168
println!("p1_sol1 : {}", p1_sol1());
println!("p1_sol2 : {}", p1_sol2());
println!("p1_sol3 : {}", p1_sol3());
println!("p1_sol4 : {}", p1_sol4());
println!("p1_sol5 : {}", p1_sol5());
println!("p1_sol6 : {}", p1_sol6());
println!("p1_sol7 : {}", p1_sol7());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment