Skip to content

Instantly share code, notes, and snippets.

Avatar

Ata Deniz Aydın meagtan

  • Bilkent University
View GitHub Profile
@meagtan
meagtan / hensel.py
Last active May 16, 2019
p-adic numbers implemented in Python
View hensel.py
# Finding roots of polynomials in p-adic integers using Hensel's lemma
from padic import *
from poly import *
def roots(p, poly):
'Yield all roots of polynomial in the given p-adic integers.'
for root in xrange(p):
try:
yield PAdicPoly(p, poly, root)
@meagtan
meagtan / search.c
Last active Apr 4, 2017
Newton's method for sorted array search
View search.c
/*
* Search for integer a in sorted array arr[n] similarly to Newton's method
*/
int newtonsearch(int *arr, int n, int a)
{
int i = 0;
while (i >= 0 && i < n) {
if (arr[i] == a)
return i;
i -= (arr[i] - a) / (arr[i + 1] - arr[i]); // TODO check for boundary conditions, zero denominator, rounding errors
@meagtan
meagtan / collatz.c
Last active Oct 21, 2016
Collatz conjecture
View collatz.c
/*
* Verifies the Collatz conjecture up to N positive integers.
* The program searches backwards starting from 1 to form a tree, where each integer n has children 2n and (n-1)/3 if odd,
* and then searching this tree breadth-first without forming it.
*/
#include <stdio.h>
#include <stdlib.h>
#define N 1000
@meagtan
meagtan / trie.js
Last active Sep 5, 2016
Trie implementation in JavaScript
View trie.js
// Tries are implemented as objects containing values and maps of letters to a value or subtrie.
// Construct trie from given list of strings (optional, defaults to 0)
function Trie(strings) {
this.value = null;
this.nodes = {};
if (strings !== undefined)
for (var str in strings)
this.addString(str);
@meagtan
meagtan / galois.c
Last active Jun 6, 2021
Quick implementation of Galois fields
View galois.c
/*
* The following is an implementation of the finite field GF(2^8) as bit vectors of length 8, where the nth bit represents the
* coefficient of the nth power of the generator in each element, and the generator satisfies the minimal polynomial
* x^8 + x^4 + x ^3 + x^2 + 1 in the prime field Z_2, in which addition is equivalent to XOR and multiplication to AND.
* The elements of GF(2^8) thus represent polynomials of degree < 8 in the generator x. Addition in this field is simply
* bitwise XOR, but multiplication requires the elimination of powers of x <= 8.
*/
#include <stdio.h>
#include <stdint.h>
@meagtan
meagtan / weather.sh
Created Aug 3, 2016
Geektool script to print current weather
View weather.sh
curl --silent "http://wxdata.weather.com/wxdata/weather/local/TUXX0002?cc=*&unit=m&dayf=1" | \
grep -A2 '<tmp>' | tr '\n' ' ' | sed -E 's|.*<tmp>(.*)</tmp>.*<t>(.*)</t>.*|\2, \1°|'
@meagtan
meagtan / fuzzyclock.py
Last active May 15, 2017
Fuzzy clock script for Geektool
View fuzzyclock.py
#!/usr/bin/python
from datetime import datetime
numwrds = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "quarter", "sixteen", "seventeen",
"eighteen", "nineteen", "twenty", "twenty-one", "twenty-two", "twenty-three",
"twenty-four", "twenty-five", "twenty-six", "twenty-seven", "twenty-eight",
"twenty-nine", "half"]
now = datetime.now()