Skip to content

Instantly share code, notes, and snippets.

@wangkuiyi
wangkuiyi / jsonrpc_example.go
Last active August 29, 2015 14:12
JSON RPC Go Example
package main
import (
"errors"
"fmt"
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
)
#include <math.h>
#include <stdio.h>
#include <set>
typedef std::set<int> Set;
bool is_prime(int a, Set* primes) {
for (Set::const_iterator i = primes->begin();
i != primes->end() && *i <= sqrt(a);
++i) {
@wangkuiyi
wangkuiyi / largest-prime-factor.cc
Created September 6, 2012 10:03
Find the largest prime factor of a number. http://projecteuler.net/problem=3
#include <math.h>
#include <stdio.h>
#include <vector>
typedef std::vector<long> Queue;
long find_smallest_divisor(long number) {
for (long i = 2; i < sqrt(number); ++i) {
if (number % i == 0) {
return i;
@wangkuiyi
wangkuiyi / palindrome-as-product.cc
Created September 6, 2012 13:35
Find the largest palindrome made from the product of two 3-digit numbers. http://projecteuler.net/problem=4
#include <stdio.h>
#include <string>
std::string positive_to_string(int num) {
std::string s;
while (num > 0) {
s.push_back((char)('0' + (num % 10)));
num /= 10;
}
return s;
@wangkuiyi
wangkuiyi / number-divisible-by-numbers.cc
Created September 6, 2012 14:00
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? http://projecteuler.net/problem=5
// Question: What is the smallest number divisible by each of the
// numbers 1 to 20?
//
// I factorize each of the numbers 1 to 20. For example, 12=2*2*3.
// This factorization can be represented by a histogram (2*2 3*1). If
// a number is evenly divisible by 20, it must be a product of at
// least two 2's and a 3. So I accumulate each factorization into a
// histogram by the following rule: if the factorization contains two
// 2's, the accumulation must contain at least two 2's. Then the
// final result is the product of numbers in the accumulation.
@wangkuiyi
wangkuiyi / binary-tree-traversal.cc
Created December 14, 2012 14:05
O(n)-time non-recursive traversal of binary trees without extract space cost.
#include <iostream>
struct Node {
int key;
Node* left;
Node* right;
Node* parent;
Node(int k, Node* l, Node* r) {
key = k;
// This simple program shows how to use the write-support branch of
// https://github.com/colinmarc/hdfs/, which accesses HDFS of Hadoop
// 2.2.x throught the native protobuf-based RPC protocol
@wangkuiyi
wangkuiyi / learn-gob.go
Created January 14, 2016 10:28
This sample program shows how to read Gob-encoded records out from a file.
package main
import (
"encoding/gob"
"io"
"log"
"os"
)
func main() {
@wangkuiyi
wangkuiyi / goaws-s3.go
Created February 3, 2016 19:19
Shows how to use https://github.com/AdRoll/goamz/tree/master/s3 to access an AWS bucket created on AWS console.
package main
import (
"bytes"
"image"
"image/jpeg"
"io"
"github.com/AdRoll/goamz/aws"
"github.com/AdRoll/goamz/s3"
@wangkuiyi
wangkuiyi / goaws-sqs.go
Created February 3, 2016 19:20
How to use https://github.com/AdRoll/goamz/tree/master/s3 to create SQS queues as well as read/write with it.
package main
import (
"fmt"
"time"
"github.com/AdRoll/goamz/aws"
"github.com/AdRoll/goamz/sqs"
"github.com/davecgh/go-spew/spew"