Skip to content

Instantly share code, notes, and snippets.

View nafu's full-sized avatar
🧗
Keep exploring

Fumiya Nakamura nafu

🧗
Keep exploring
View GitHub Profile
@nafu
nafu / newton_raphson_sample
Created February 13, 2013 11:33
Newton-Raphson ( for root finding)
epsilon = 0.01
y = 1234567.0
guess = y/2.0
while abs(guess*guess - y) >= epsilon:
guess = guess - (((guess**2) - y)/(2*guess))
print(guess)
print('Square root of ' + str(y) + ' is about ' + str(guess))
@nafu
nafu / gist:4944047
Created February 13, 2013 11:39
Bisection Search
x = 1234567
epsilon = 0.01
numGuesses = 0
low = 0.0
high = x
ans = (high + low)/2.0
while abs(ans**2 - x) >= epsilon:
print('low = ' + str(low) + ' high = ' + str(high) + ' ans = ' + str(ans))
numGuesses += 1
if ans**2 < x:
@nafu
nafu / gist:4944095
Created February 13, 2013 11:51
Decimal to Binary
num = 256
if num < 0:
isNeg = True
num = abs(num)
else:
isNeg = False
result = ''
if num == 0:
result = '0'
@nafu
nafu / gist:4952475
Last active December 13, 2015 17:59
Definitions
def a(x, y, z):
if x:
return y
else:
return z
def b(q, r):
return a(q>r, q, r)
print(a(False, 2, 3))
@nafu
nafu / gist:4959181
Created February 15, 2013 08:42
Git Color
The value for these configuration variables is a list of colors (at most two) and attributes (at most one), separated by spaces. The colors accepted are normal, black, red, green, yellow, blue, magenta, cyan and white; the attributes are bold, dim, ul, blink and reverse. The first color given is the foreground; the second is the background. The position of the attribute, if any, doesn't matter.
http://git-scm.com/docs/git-config
@nafu
nafu / 1-1.cpp
Created February 17, 2013 11:33
#include <cstdio>
const int MAX_N = 50;
int main() {
int n, m, k[MAX_N];
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &k[i]);
}
@nafu
nafu / 27.cpp
Created February 17, 2013 11:34
#include <algorithm>
#include <iostream>
using namespace std;
const int MAX_N = 100;
int n, m, k[MAX_N];
int kk[MAX_N * MAX_N];
def three4doller(num):
return (num%3)*round(1/3.0, 2)+(num/3)*1
print three4doller(1)
print three4doller(2)
print three4doller(3)
print three4doller(4)
print three4doller(5)
print three4doller(6)
# 1 pound == 16 ounces
def ounce2pound(ounce):
return ounce/16.0
def cost(ounces):
return round(1.99*ounce2pound(ounces), 2)
print cost(1)
print cost(2)
print cost(3)
@nafu
nafu / chop.py
Last active December 13, 2015 21:38
def chop(target, search_array):
if target in search_array:
return search_array.index(target)
else:
return -1