Skip to content

Instantly share code, notes, and snippets.

View samueltardieu's full-sized avatar

Samuel Tardieu samueltardieu

View GitHub Profile
@samueltardieu
samueltardieu / primes.py
Created November 26, 2010 22:46
Prime numbers manipulation
"""Prime number generation and number factorization."""
import bisect, itertools, random, sys
_primes = [2]
_miller_rabin_limit = 48611 # 5000th prime
_miller_rabin_security = 7
def modpow (a, b, c):
@samueltardieu
samueltardieu / pell.py
Created November 26, 2010 22:47
Solve the Pell equation
"""Compute solutions to the diophantine Pell equation x^2-D*y^2=1."""
import itertools
def pell (D):
"""Return the smallest integer set solving Pell equation
x^2-D*y^2=1 where x, D and y are positive integers. If there are no
solution (D is a square), return None.
>>> pell(3)
USING: hash-sets kernel locals math sequences sets ;
IN: acm
:: add ( set n -- newset )
HS{ } clone :> newset
set members [ n [ + ] [ - ] 2bi [ newset adjoin ] bi@ ] each newset ;
:: least ( n -- x )
HS{ 0 } 1 [ [ add n over in? ] keep swap ] [ 1 + ] until nip ;
int netaccept(int fd, char *server, int *port)
{
int cfd = 0;
int one = 0;
union {
struct sockaddr_in ipv4;
struct sockaddr_in6 ipv6;
} sa;
socklen_t len = sizeof sa;
int rc = 0, af;
@samueltardieu
samueltardieu / injected-singleton.scala
Created March 11, 2011 10:18
Example of Scala injection of a singleton
// StopWatch class
abstract class StopWatch {
def giveTime: Time
}
// Class is declared abstract as it is lacking a StopWatch which will be later injected.
// Note that we could have defined a default value here and thus made the class concrete.
abstract class FooBar {
// Use with a default value as in:
// runningScalaVersion getOrElse "2.8.0"
lazy val runningScalaVersion = {
val matcher = """version (\d+\.\d+\.\d+).*""".r
util.Properties.versionString match {
case matcher(vsn) => Some(vsn)
case _ => None
}
}
@samueltardieu
samueltardieu / ips.c
Created July 26, 2011 09:54 — forked from bortzmeyer/gist:1086392
Resolve names, using the standard library (so, going through NSS and things like that, unlike dig)
// Compile with -std=gnu99 to get the proper definitions of struct addrinfo
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
@samueltardieu
samueltardieu / test.scala
Created July 29, 2011 20:17
Type erasure and reification
class Example {
println("Constructing an Example object: " + this)
}
object Test extends App {
// Return a new object of type T when called as f[T]
def f[T : Manifest] = manifest[T].erasure.getConstructor().newInstance()
@samueltardieu
samueltardieu / AndroidUtils.scala
Created October 11, 2011 09:46
Current (working but not polished) implementation of onUI and onBG
import scala.collection.mutable.{HashMap, Map}
import scala.concurrent.Channel
import scala.concurrent.ops.spawn
import android.content.Context
import android.os.{Handler, Message}
object AndroidBG {
private val androidBGs: Map[Context, AndroidBG] = new HashMap()
@samueltardieu
samueltardieu / CachesMapDownloader.java
Created October 25, 2011 07:19
Downloader thread
package cgeo.geocaching.maps;
import cgeo.geocaching.Settings;
import cgeo.geocaching.cgBase;
import cgeo.geocaching.cgSearch;
import cgeo.geocaching.geopoint.Geopoint;
import org.apache.commons.lang3.StringUtils;
import android.os.Handler;