Skip to content

Instantly share code, notes, and snippets.

View Prince781's full-sized avatar
⚙️
Compiling a compiler

Princeton Ferro Prince781

⚙️
Compiling a compiler
View GitHub Profile
@Prince781
Prince781 / nth_prime.c
Last active December 19, 2015 20:59
Basic C code to get the nth prime number in a sequence.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
unsigned long nth_prime(unsigned long n) {
unsigned long p = 2, i=0, c;
while (i < n) {
unsigned long n_f = 0; // number of factors
for (c=(unsigned long)sqrt(p); c>0; c--)
n_f += (p%c==0);
@Prince781
Prince781 / DoubleBuffering.java
Last active December 22, 2015 19:19
Example double-buffering using Canvas2D; excerpt from Project X.
/*******************************
* Example of double buffering
* in Canvas2D
********************************/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
@Prince781
Prince781 / christmas_tree.c
Last active January 1, 2016 16:29
Example solution for the following challenge: Given input N, print a centered tree, of height N, where the width of the tree expands (+1) in both directions until line N. On line N+1, print out a single centered character, which shall serve as the stump of the tree. edit: simplified the code a little edit2: allowed \n on last line (there was rea…
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc < 2) return -1;
int n = atoi(argv[1]), i, l=0, c;
for (i=1; l<=n; l++,i=l%n+1)
for (c=1; c<n+i; c++)
printf("%s%s", c>n-i?"*":" ", c==n+i-1?"\n":"");
return 0;
@Prince781
Prince781 / k_elements_from_n.c
Created January 13, 2014 04:57
One solution to the issue of finding the amount of combinations for k elements in a list of length n. (See http://en.wikipedia.org/wiki/Combination#Enumerating_k-combinations).
// k_elements_from_n
// c99
#include <stdio.h>
#include <stdlib.h>
unsigned long factorial(long n) {
return n==1 ? 1 : n*factorial(n-1);
}
void print_arr(int arr[], int length) {
@Prince781
Prince781 / w_lambert.c
Created February 25, 2014 07:52
Implements the Lambert W-function (inverse of f(W) = we^w) of z, useful in solving for we^w = z.
// w_lambert.c Lambert W-function
// c99
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double W(double z) {
double EPSILON = 0.000000000000001;
double w = 0, ew, wN = z;
@Prince781
Prince781 / base2.c
Last active August 29, 2015 14:00
Prints out binary form of whatever is given.
#include <stdio.h>
void printb(unsigned long v) {
if (v>>1 > 0) printb(v>>1);
putchar((v&1) + '0');
}
int main(int argc, char *argv[]) {
if (argc > 1)
printb(atol(argv[1]));
@Prince781
Prince781 / merge.c
Last active August 29, 2015 14:02
Simple merge sort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define sz 100
void msort(int *out, int *begin, int *end);
static void merge(int *out, int *lbeg, int *lend, int *rend);
void printr(int *arr, int *end);
@Prince781
Prince781 / insertion.c
Created June 2, 2014 16:42
Simple insertion sort
void insertion_sort(int *arr, int len) {
int p;
for (p=1; p<len; p++) {
int n = arr[p], p2 = p;
while (p2 > 0 && n < arr[p2-1])
arr[p2] = arr[--p2];
arr[p2] = n;
}
}
@Prince781
Prince781 / dilemma.c
Last active August 29, 2015 14:02
Semi-genetic "survival of the fittest" algorithm for getting the best campaign strategy.
// tests the President's Dilemma through genetic algorithm
// Princeton Ferro - Jun 9, 2014
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define FUND 100
#define N 1000 // members per generation
#define DEBUG 0
@Prince781
Prince781 / ffact.c
Last active August 29, 2015 14:02
Fast factorial implementation (using 1/2 iterations) in C
typedef unsigned long uli;
uli fact(uli n) {
if (n < 2) return 1;
uli r = (n % 2) ? 1+n/2 : 1; // if odd...
uli p = n, a = n, b = n-2;
while (b>1) {
p *= a + b;
a += b;
b -= 2;