onsdag 19 december 2012

Python is_prime using Miller-Rabin


from random import randrange

def is_prime(n, trials=6):
    """ Miller-Rabin probabilistic prime test.
    """
    if n < 2:
        return False
    i = 0
    while i < trials:
        i+=1
        a = randrange(1, n)
        if pow(a, n-1, n) != 1: # (a**(n-1)) % n
            return False
    return True

# or as a tiny-font-one-liner


def is_prime(n, trials=6):
    return n >= 2 and (trials == 0 or (pow(randrange(1, n), n-1, n) == 1 and is_prime(n, trials - 1)))

onsdag 12 december 2012

uboot pogoplug netconsole

I used this tip to set up my pogoplug to send the uBoot prompt
via udp to my laptop
(see: Use netconsole to troubleshoot uBoot without a serial cable
 http://forum.doozan.com/read.php?3,14,14)

Now, to get the boot prompt from pogoplug (192.168.1.74) to my laptop (192.168.1.72)
I do (on my laptop)


ifconfig en0 alias 192.168.1.72 255.255.255.0

to set an ip alias (remove with -alias), and then

nc -lu 192.168.1.72 6666 & nc -u 192.168.1.74 6666

to communicate with the pogoplug.