-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial set of 10. And don't bitch about the directory structure again.
- Loading branch information
0 parents
commit 1275b38
Showing
11 changed files
with
382 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env python | ||
|
||
from sys import argv | ||
|
||
def approximate(n, m): | ||
if n < 0.0: | ||
num, den, val, dif, err = approximate(-n, m) | ||
return (-num, den, -val, dif, err) | ||
elif n == 0.0: | ||
return (0, 1, 0.0, 0.0, 0.0) | ||
else: | ||
best_num = 1 | ||
best_den = 1 | ||
best_val = 1.0 | ||
best_dif = abs(1.0 - n) | ||
best_err = best_dif / n | ||
for den in xrange(1, m + 1): | ||
num = int(round(n * float(den))) | ||
val = float(num) / float(den) | ||
dif = abs(val - n) | ||
err = dif / n | ||
if (dif < best_dif): | ||
best_num = num | ||
best_den = den | ||
best_val = val | ||
best_dif = dif | ||
best_err = err | ||
return (best_num, best_den, best_val, best_dif, best_err) | ||
|
||
def run_approximate(n, m): | ||
num, den, val, dif, err = approximate(n, m) | ||
print str(n) + u' \u2248 ' + str(num) + " / " + str(den) + " = " + str(val) | ||
print "Delta: " + str(dif) | ||
print "Error: " + str(err * 100.0) + "%" | ||
|
||
def main(): | ||
if len(argv) <= 2: | ||
print "usage: approximate <highest-denominator> <value> [<value> [...]]" | ||
else: | ||
try: | ||
mm = max(1, abs(int(argv[1]))) | ||
for i in xrange(2, len(argv)): | ||
if i > 2: print | ||
try: | ||
n = float(argv[i]) | ||
run_approximate(n, mm) | ||
except: | ||
print argv[i] + " is not a number" | ||
except: | ||
print argv[1] + " is not an integer" | ||
|
||
if __name__ == "__main__": main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python | ||
|
||
from sys import argv | ||
|
||
def strbase(n, b): | ||
if n < 0: | ||
return "-" + strbase(-n, b) | ||
else: | ||
(d, m) = divmod(n, b) | ||
ms = chr(48 + m) if m < 10 else chr(55 + m) | ||
return strbase(d, b) + ms if d > 0 else ms | ||
|
||
def main(): | ||
if len(argv) <= 3: | ||
print "usage: bci <input-radix> <output-radix> <value> [<value> [...]]" | ||
return | ||
try: | ||
sb = int(argv[1]) | ||
if sb < 2 or sb > 36: | ||
print "input radix out of range: " + str(sb) | ||
return | ||
except: | ||
print "input radix not an integer: " + argv[1] | ||
return | ||
try: | ||
db = int(argv[2]) | ||
if db < 2 or db > 36: | ||
print "output radix out of range: " + str(db) | ||
return | ||
except: | ||
print "output radix not an integer: " + argv[2] | ||
return | ||
for i in xrange(3, len(argv)): | ||
try: | ||
n = int(argv[i], sb) | ||
dn = strbase(n, db) | ||
print dn | ||
except: | ||
print "invalid value: " + argv[i] | ||
|
||
if __name__ == "__main__": main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env python | ||
|
||
from math import floor | ||
from sys import argv | ||
|
||
def floatbase(s, b): | ||
sawsign = False | ||
sawdp = False | ||
sign = +1 | ||
man = 0 | ||
exp = 0 | ||
for ch in s: | ||
if ch == '+' and not sawsign: | ||
sawsign = True | ||
sign = +1 | ||
elif ch == '-' and not sawsign: | ||
sawsign = True | ||
sign = -1 | ||
elif ch == '.' and not sawdp: | ||
sawsign = True | ||
sawdp = True | ||
else: | ||
sawsign = True | ||
digit = int(ch, b) | ||
man = man * b + digit | ||
if sawdp: exp += 1 | ||
return float(sign * man) / (float(b) ** float(exp)) | ||
|
||
def strbase(n, b): | ||
if n < 0: | ||
return "-" + strbase(-n, b) | ||
else: | ||
def strdigit(d): | ||
return chr(48 + d) if d < 10 else chr(55 + d) | ||
ip = floor(n) | ||
fp = n - ip | ||
ips = "" | ||
fps = "" | ||
while ip > 0: | ||
(ip, digit) = divmod(ip, b) | ||
ips = strdigit(int(digit)) + ips | ||
while fp > 0: | ||
fp *= b | ||
digit = floor(fp) | ||
fp -= digit | ||
fps = fps + strdigit(int(digit)) | ||
if ips == "": ips = "0" | ||
return ips if fps == "" else ips + "." + fps | ||
|
||
def main(): | ||
if len(argv) <= 3: | ||
print "usage: bcr <input-radix> <output-radix> <value> [<value> [...]]" | ||
return | ||
try: | ||
sb = int(argv[1]) | ||
if sb < 2 or sb > 36: | ||
print "input radix out of range: " + str(sb) | ||
return | ||
except: | ||
print "input radix not an integer: " + argv[1] | ||
return | ||
try: | ||
db = int(argv[2]) | ||
if db < 2 or db > 36: | ||
print "output radix out of range: " + str(db) | ||
return | ||
except: | ||
print "output radix not an integer: " + argv[2] | ||
return | ||
for i in xrange(3, len(argv)): | ||
try: | ||
n = floatbase(argv[i], sb) | ||
dn = strbase(n, db) | ||
print dn | ||
except: | ||
print "invalid value: " + argv[i] | ||
|
||
if __name__ == "__main__": main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env bash | ||
function canonicalize { | ||
cd -P -- "$(dirname -- "$1")" 2> /dev/null && | ||
printf '%s\n' "$(pwd -P)/$(basename -- "$1")" | sed 's/^\/\/*/\//' || | ||
printf '%s\n' "$1" | ||
} | ||
if [ "$#" -lt 1 ] | ||
then echo "usage: canonicalize <path> [<path> [...]]" | ||
else | ||
for i in "$@" | ||
do canonicalize "$i" | ||
done | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env bash | ||
bci 10 16 $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
#!/usr/bin/env python | ||
|
||
from math import sqrt | ||
from sys import argv | ||
from time import clock | ||
|
||
def factorize(n): | ||
if n < -1: return [(-1, 1)] + factorize(-n) | ||
elif n == -1: return [(-1, 1)] | ||
elif n == 0: return [(0, 1)] | ||
elif n == 1: return [(1, 1)] | ||
else: | ||
def potential_primes(): | ||
base_primes = (2, 3, 5) | ||
for base_prime in base_primes: | ||
yield base_prime | ||
base_primes = (7, 11, 13, 17, 19, 23, 29, 31) | ||
prime_group = 0 | ||
while True: | ||
for base_prime in base_primes: | ||
yield prime_group + base_prime | ||
prime_group += 30 | ||
factors = [] | ||
sqrtn = sqrt(n) | ||
for divisor in potential_primes(): | ||
if divisor > sqrtn: | ||
break | ||
power = 0 | ||
while (n % divisor) == 0: | ||
n //= divisor | ||
power += 1 | ||
if power > 0: | ||
factors.append((divisor, power)) | ||
sqrtn = sqrt(n) | ||
if n > 1: | ||
factors.append((n, 1)) | ||
return factors | ||
|
||
def divisors_from_factors(factors): | ||
def unsorted_divisors_from_factors(factors): | ||
if not factors: return [1] | ||
else: | ||
base, max_power = factors[0] | ||
if base == -1: return unsorted_divisors_from_factors(factors[1:]) | ||
elif base == 0: return [] | ||
elif base == 1: return unsorted_divisors_from_factors(factors[1:]) | ||
else: | ||
divisors = unsorted_divisors_from_factors(factors[1:]) | ||
all_divisors = [] | ||
for power in xrange(0, max_power+1): | ||
all_divisors += map(lambda x: x * base ** power, divisors) | ||
return all_divisors | ||
all_divisors = unsorted_divisors_from_factors(factors) | ||
all_divisors.sort() | ||
return all_divisors | ||
|
||
|
||
def test_factorize(): | ||
start = clock() | ||
n = 0 | ||
while True: | ||
f = factorize(n) | ||
fa = map(lambda x: x[0] ** x[1], f) | ||
fb = reduce(lambda x,y: x * y, fa, 1) | ||
if fb != n: | ||
print "FACTORIZE FAILED AT " + str(n) | ||
d = divisors_from_factors(f) | ||
da = map(lambda x: d[x] * d[len(d)-x-1], xrange(0, len(d))) | ||
for db in da: | ||
if db != n: | ||
print "DIVISORS FAILED AT " + str(n) | ||
f = factorize(-n) | ||
fa = map(lambda x: x[0] ** x[1], f) | ||
fb = reduce(lambda x,y: x * y, fa, 1) | ||
if fb != -n: | ||
print "FACTORIZE FAILED AT " + str(-n) | ||
d = divisors_from_factors(f) | ||
da = map(lambda x: d[x] * d[len(d)-x-1], xrange(0, len(d))) | ||
for db in da: | ||
if db != n: | ||
print "DIVISORS FAILED AT " + str(-n) | ||
if (n % 10000) == 0: | ||
print "up to " + str(n) + " at " + str(clock() - start) + "s" | ||
n += 1 | ||
|
||
def run_factorize(n): | ||
def str_from_factors_exp(factors): | ||
def str_from_factor(factor): | ||
if factor[1] == 1: return str(factor[0]) | ||
else: return "^".join(map(str, factor)) | ||
return " * ".join(map(str_from_factor, factors)) | ||
def str_from_factors_mul(factors): | ||
factors = map(lambda x: [x[0]] * x[1], factors) | ||
factors = reduce(lambda x,y: x + y, factors, []) | ||
return " * ".join(map(str, factors)) | ||
def pairs_from_divisors(d): | ||
return map(lambda x: (d[x], d[len(d)-x-1]), xrange(0, (1 + len(d)) // 2)) | ||
def str_from_pairs(pairs): | ||
pairs = map(lambda x: "*".join(map(str, x)), pairs) | ||
return "0*0" if not pairs else "\t".join(pairs) | ||
f = factorize(n) | ||
print str(n) + " = " + str_from_factors_exp(f) | ||
print str(n) + " = " + str_from_factors_mul(f) | ||
d = divisors_from_factors(f) | ||
print "Divisors: " + ("N/A" if not d else ", ".join(map(str, d))) | ||
s = reduce(lambda x,y: x + y, d, 0) | ||
sn = s - abs(n) | ||
s2n = sn - abs(n) | ||
ss = "zero" if s == 0 else "unit" if sn == 0 else "prime" if sn == 1 else "deficient" if s2n < 0 else "perfect" if s2n == 0 else "abundant" | ||
print "sigma_0(n) = " + str(len(d)) | ||
print "sigma_1(n) = " + str(s) | ||
print "sigma_1(n)-n = " + str(sn) | ||
print "sigma_1(n)-2n = " + str(s2n) | ||
print str(abs(n)) + " is " + ss + "." | ||
p = pairs_from_divisors(d) | ||
print "Pairs:" | ||
print str_from_pairs(p) | ||
|
||
|
||
def main(): | ||
if len(argv) <= 1: | ||
print "usage: factor <value> [<value> [...]]" | ||
else: | ||
for i in xrange(1, len(argv)): | ||
if i > 1: print | ||
if argv[i] == "test": test_factorize() | ||
else: | ||
try: | ||
n = int(argv[i]) | ||
run_factorize(n) | ||
except: | ||
print argv[i] + " is not an integer" | ||
|
||
if __name__ == "__main__": main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
chmod -R a-w "$@" | ||
chflags -R uchg "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env bash | ||
bci 16 10 $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env bash | ||
function install { | ||
cp "$1" "/usr/local/bin/${1%.*}" 2> /dev/null | ||
chmod a+rx "/usr/local/bin/${1%.*}" 2> /dev/null | ||
} | ||
function die { | ||
echo "This script must be run as root." | ||
exit | ||
} | ||
|
||
install approximate.py || die | ||
install bci.py || die | ||
install bcr.py || die | ||
install canonicalize.sh || die | ||
install dechex.sh || die | ||
install factor.py || die | ||
install harden.sh || die | ||
install hexdec.sh || die | ||
install simplify.py || die | ||
install soften.sh || die |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env python | ||
|
||
from fractions import gcd | ||
from sys import argv | ||
|
||
def mgcd(ns): | ||
ns = map(abs, filter(None, ns)) | ||
if ns: | ||
return reduce(gcd, ns) | ||
else: | ||
return 1 | ||
|
||
def simplify(ns): | ||
divisor = mgcd(ns) | ||
ns = map(lambda n: n // divisor, ns) | ||
return (ns, divisor) | ||
|
||
def run_simplify(ns): | ||
ns, divisor = simplify(ns) | ||
print "GCD: " + str(divisor) | ||
print "Sim: " + " ".join(map(str, ns)) | ||
|
||
def main(): | ||
if len(argv) <= 1: | ||
print "usage: simplify <value> [<value> [...]]" | ||
else: | ||
try: | ||
ns = map(int, argv[1:]) | ||
run_simplify(ns) | ||
except: | ||
print "usage: simplify <value> [<value> [...]]" | ||
|
||
if __name__ == "__main__": main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
chflags -R nouchg "$@" |