Skip to content

Instantly share code, notes, and snippets.

// this tail recursive implementation can't be optimized by gcc 4.8.1.
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int N = 1e5;
@qddddr
qddddr / 156.cpp
Last active December 26, 2015 15:59
/*
* Project Euler - Problem 156
*
* g++ -std=c++11 -O4 156.cpp; time ./a.out
* real 0m0.040s
* user 0m0.036s
* sys 0m0.000s
*/
#include <iostream>
/*
* Project Euler - Problem 337 (test, O(N^2))
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <cinttypes>
using namespace std;
int main(void)
{
const int N = 10, n = 2;
enum_coprimes_with_stern_brocot_tree(N, 0, n, n, n);
cout << "--------------------" << endl;
enum_coprimes_with_reverse_euclid(N, n, n);
}
#!r6rs
(import (rnrs))
(define (print . args)
(for-each display args)
(newline))
(define (enum-coprimes/stern-brocot-tree N)
(let recur ([a 0] [b 1] [x 1] [y 1])
#include <iostream>
using namespace std;
void enum_coprimes_with_stern_brocot_tree(int N, int a, int b, int x, int y)
{
int n = a+x, d = b+y;
if (d <= N) {
cout << n << ", " << d << endl;
enum_coprimes_with_stern_brocot_tree(N, a, b, n, d);
/*
* Project Euler - Problem 441
* Brute forcing and parallelized with OpenMP 3.0
*/
#include <iostream>
#include <algorithm>
#include <cinttypes>
#include <omp.h>
#include <boost/multiprecision/cpp_int.hpp>
/*
* Project Euler - Problem 441 (Brute forcing)
*/
#include <iostream>
#include <algorithm>
#include <cinttypes>
#include <omp.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
@qddddr
qddddr / 401.sps
Last active December 25, 2015 13:39
#!r6rs
;; Project Euler - Problem 401
;; Order = O(sqrt(N))
;;
;; $ time racket 401.sps
;; *********
;;
;; real 0m43.008s
;; user 0m41.420s
@qddddr
qddddr / 432.cpp
Last active December 24, 2015 21:09
/*
* Project Euler - Problem 432
* A brute force approach with cache-friendly and parallelized implementation.
*
* $ g++ -std=c++11 -O4 -fopenmp 432.cpp; time ./a.out
* N: real user
* 1e6: 0.015s 0.076s
* 1e7: 0.025s 0.212s
* 1e8: 0.138s 1.488s
* 1e9: 1.206s 13.368s