Skip to content

Instantly share code, notes, and snippets.

View jbagley's full-sized avatar

Jason Bagley jbagley

  • Art & Logic, Inc.
View GitHub Profile
@jbagley
jbagley / FunctionalLeibnizPiApprox
Last active May 7, 2020 22:55
Swift tail-recursion experimentation: Recursive non-mutating implementation of Gregory-Leibnitz pi approximation.
// Recursive implementation
func LeibnizPiApproximationFunc(#afterIterations: Int) -> Double
{
// See http://stackoverflow.com/questions/24270693/nested-recursive-function-in-swift
var ApproxPi: (Double, Double, Double, Int) -> Double = { _ in return 0.0 }
ApproxPi =
{
(denom : Double, factor : Double, piAccum: Double, iteration: Int) -> Double in
@jbagley
jbagley / LeibnizPiApprox
Last active August 29, 2015 14:06
My first attempt at using Swift in an Xcode playground
// Gregory-Leibniz approximation of π
// See https://gist.github.com/jbagley/9558879 for C implementation
var factor = -1.0
var piAccumulation = 4.0
var denom = 3.0
// If using in a playground beware this many iterations, try 2500 instead.
for i in 1…500000
{
@jbagley
jbagley / CalcPi.c
Last active August 29, 2015 13:57
A few methods for calculating Pi written from descriptions on Wikipedia one day when I was bored enough to do it.
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
// So I can change the type more easily.
typedef long double Float;
#define FloatFormat "%.30Lf"
#define SqRoot sqrtl
@jbagley
jbagley / SampleTreeMap.py
Created February 19, 2013 10:10
Sample code for using Google's Visualization: Treemap API
# Start the HTML and Javascript code
print '''
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["treemap"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
'''