Skip to content

Instantly share code, notes, and snippets.

View anirudhjayaraman's full-sized avatar
🏠
Working from home

Anirudh Jayaraman anirudhjayaraman

🏠
Working from home
View GitHub Profile
@anirudhjayaraman
anirudhjayaraman / karatsuba.py
Last active September 11, 2023 10:18
Karatsuba Multiplication
def karatsuba(x,y):
"""Function to multiply 2 numbers in a more efficient manner than the grade school algorithm"""
if len(str(x)) == 1 or len(str(y)) == 1:
return x*y
else:
n = max(len(str(x)),len(str(y)))
nby2 = n / 2
a = x / 10**(nby2)
b = x % 10**(nby2)
@anirudhjayaraman
anirudhjayaraman / HW02.R
Last active June 14, 2021 09:25
14.74x Foundation of Development Policy (Homework Assignment 02)
# Set working directory to local directory where the data is kept
setwd("~/IGIDR/Development Economics - MIT/Homework Assignment 02")
# read data
migueldata = read.csv("ted_miguel_worms.csv", header = TRUE)
attach(migueldata)
# Question 6
# How many observations are there per pupil? (Enter a whole number of 0 or higher)?
length(migueldata$pupid)
length(unique(migueldata$pupid))
# Question 7
@anirudhjayaraman
anirudhjayaraman / HW01.R
Created October 12, 2015 16:26
14.74x Foundation of Development Policy (Homework Assignment 01)
# set working directory to local directory where the data is kept
setwd("~/IGIDR/Development Economics - MIT/Homework Assignment 01")
# read the data
wb_dev_ind = read.csv("wb_dev_ind.csv")
# summarize data
summary(wb_dev_ind)
# Question 1
# What is the Mean of GDP per capita? What is the standard deviation of GDP per capita?
@anirudhjayaraman
anirudhjayaraman / solution_baoloc.R
Created October 12, 2015 10:40
Solution to Baoloc
[1] "The number of solutions are:"
[1] 128
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
solution 9 1 2 5 6 7 3 4 8
solution 7 9 6 1 5 2 3 4 8
solution 1 9 6 7 5 2 3 4 8
solution 1 5 2 3 4 8 7 9 6
solution 1 5 2 3 4 8 9 7 6
solution 5 1 2 9 6 7 3 4 8
solution 5 1 2 9 6 7 4 3 8
@anirudhjayaraman
anirudhjayaraman / baoloc.R
Created October 12, 2015 10:34
Solution to [Viral] Math Puzzle for Vietnamese Eight-Year-Olds (Using R)
## The problem could be written as u + 13v/w + x + 12y - z - 11 + pq/r -10 = 66
## which reduces to u + 13v/w + x + 12y - z + pq/r = 87
## This problem was solved on [1] "Wed May 27 17:46:52 2015"
baoloc <- function()
{
packages <- rownames(installed.packages())
if("combinat" %in% packages) library("combinat") else install.packages("combinat")
numbers <- 1:9
permutations <- permn(numbers) ## list of all permutations of vector input
@anirudhjayaraman
anirudhjayaraman / choroplethr.R
Created October 2, 2015 02:51
Choropleths in R
## load the requisite libraries into R
library("xlsx")
library("choroplethr")
library("choroplethrAdmin1")
library("ggplot2")
indianregions <- get_admin1_regions("india")
## gets dataframe of 2 columns with name of country ("india") throughout column 1
## and name of regions in 2nd column
@anirudhjayaraman
anirudhjayaraman / sloc.py
Created September 27, 2015 09:32
Count Source Lines of Code in Python
# prints recursive count of lines of python source code from current directory
# includes an ignore_list. also prints total sloc
import os
cur_path = os.getcwd()
ignore_set = set(["__init__.py", "count_sourcelines.py"])
loclist = []
for pydir, _, pyfiles in os.walk(cur_path):
@anirudhjayaraman
anirudhjayaraman / euler68.py
Created September 13, 2015 12:32
Project Euler Problem 68
from itertools import permutations
from itertools import combinations
# array of candidate solutions empty at the beginning
record = []
# choose 5 numbers for inner cells between 1 and 9; there are 9C5 combinations
# the problem ask for a 16-digit number, so 10 is not to be included in inner cells
cells = range(1,10)
inner_cells = [map(int,comb) for comb in combinations(cells,5)]
@anirudhjayaraman
anirudhjayaraman / euler67.py
Created September 5, 2015 08:11
Project Euler Problem 67
# Read the problem matrix into a triangle array in python
filename = 'euler67.txt'
with open(filename, "r") as ins:
array = []
for line in ins:
array.append(line)
# Convert the triangle arry entries into integers
newArray = []
for i in array:
j = i.split(' ')
@anirudhjayaraman
anirudhjayaraman / euler67.txt
Created September 5, 2015 07:43
Project Euler Problem 67 Triangle Array
59
73 41
52 40 09
26 53 06 34
10 51 87 86 81
61 95 66 57 25 68
90 81 80 38 92 67 73
30 28 51 76 81 18 75 44
84 14 95 87 62 81 17 78 58
21 46 71 58 02 79 62 39 31 09