Skip to content

Instantly share code, notes, and snippets.

@Radcliffe
Radcliffe / a308479.py
Last active June 10, 2019 01:40
Least k such that k*n and (k+1)*n fail to have a common nonzero digit
# This script calculates Sequence A0308479, the least k such that k*n and (k+1)*n
# fail to have a common nonzero digit, or 0 if this property never occurs.
#
# See: https://oeis.org/A308479
#
# Author: David Radcliffe
# 9 June 2019
def share_digit(m, n):
return set(str(m)) & set(str(n)) - set('0')
@Radcliffe
Radcliffe / binom.py
Created June 1, 2019 21:16
Binomial coefficient in Python
# Python script to calculate the binomial coefficient C(n, k)
# where n is an integer or floating point number and k is an integer.
#
# We use the following general definition:
# C(n, k) = n(n-1)(n-2)...(n-k+1) / k! if k >= 0,
# 0 if k < 0.
#
# For n < 0, we use the negative binomial identity
# C(n, k) = (-1)^k * C(k - n - 1, k)
#
@Radcliffe
Radcliffe / tower_mod.py
Created March 26, 2019 01:53
Calculating the last 100 digits of 9^9^9^9 with Python
def phi_2_5(m):
"""Computes the totient of a number m whose only prime factors are 2 or 5."""
if m % 2 == 0:
m //= 2
if m % 5 == 0:
m = (m * 4) // 5
return m
def tower_mod(a, n, m):
"""Evaluates the power tower a^a^a^..^a, with height n, modulo m.
@Radcliffe
Radcliffe / 314prime.txt
Created March 3, 2019 18:15
A 314 prime for Pi Day: A 3141 digit probable prime, with random digits chosen from 3, 1, and 4, starting with 314.
31414143134141341341111344411133114141331444144114343444333433431441441134343314
34441433411413111343334144331331311444341314314131443334334444313114114134413113
41341113314141433141133343113113413111133334143441131333134441311433131143331343
43314443313433441134444414341114313333433313331413431341341144411331143314314334
13431443414444333141341144133431334411413411433443331131331114344414131433444441
31334341331431414133433144414144113344433413333434133143444343333131143343134113
13434434313311343111114434344143343443111333131433344314331133434441333131433311
43331133443133313141413441111331311413443311111114413144334111313343333434333443
11111441441444113444111444133144114333341431141141143414111331344134143144413143
34441441143343113331141443344433111314114411444313331413131413144444331413331433
@Radcliffe
Radcliffe / pwned.py
Created January 21, 2019 02:16
Python 3 script to search for exposed passwords
#!/usr/bin/env python3
#
# This Python script searches a database of over 500 million passwords
# to determine whether a given password has been exposed in a data breach.
#
# Thanks to Troy Hunt for creating the API that is used by this script.
# See https://haveibeenpwned.com/API/v2#PwnedPasswords for more information.
#
# Author: David Radcliffe ([email protected])
# Date: 20 January 2019
@Radcliffe
Radcliffe / powermod.erl
Created August 23, 2018 01:56
Fast modular exponentiation in Erlang
-module(powermod).
-export([powmod/2]).
powmod(A, B, M) -> powmod(A, B, M, 1).
powmod(_, 0, _, R) -> R;
powmod(A, B, M, R) when B rem 2 == 1 -> powmod(A, B-1, M, A*R rem M);
powmod(A, B, M, R) -> powmod(A*A rem M, B div 2, M, R).
@Radcliffe
Radcliffe / power.erl
Created August 23, 2018 01:55
Fast exponentiation in Erlang
-module(power).
-export([pow/2]).
pow(A, B) -> pow(A, B, 1).
pow(_, 0, C) -> C;
pow(A, B, C) when B rem 2 == 1 -> pow(A, B-1, A*C);
pow(A, B, C) -> pow(A*A, B div 2, C).
@Radcliffe
Radcliffe / multiply.erl
Created August 23, 2018 01:44
Peasant multiplication in Erlang
-module(multiply).
-export([mul/2]).
mul(A, B) -> mul(A, B, 0).
mul(_, 0, C) -> C;
mul(A, B, C) when B rem 2 == 1 -> mul(A, B-1, A+C);
mul(A, B, C) -> mul(A + A, B div 2, C).
@Radcliffe
Radcliffe / multiply.erl
Created August 23, 2018 01:41
Naive multiplication with tail recursion
-module(multiply).
-export([mul/2]).
mul(A, B) -> mul(A, B, 0).
mul(_, 0, C) -> C;
mul(A, B, C) -> mul(A, B-1, A+C).
@Radcliffe
Radcliffe / multiply.erl
Last active August 23, 2018 01:42
Naive integer multiplication in Erlang
-module(multiply).
-export([mul/2]).
mul(_, 0) -> 0;
mul(A, B) -> mul(A, B-1) + A.