Skip to content

Instantly share code, notes, and snippets.

View Leowbattle's full-sized avatar

Leo Battle Leowbattle

  • England
View GitHub Profile
@Leowbattle
Leowbattle / MainProgram.java
Created March 13, 2023 16:26
Implementing natural numbers with Peano arithmetic in Java 😎
public class MainProgram {
public static void main(String[] args) {
Nat n1 = Nat.int2Nat(10);
Nat n2 = Nat.int2Nat(20);
Nat n3 = n1.add(n2);
int x = Nat.nat2Int(n3);
System.out.println(x);
#include <iostream>
#include <vector>
#include <optional>
#include <functional>
#include <cctype>
using namespace std;
struct unit {
@Leowbattle
Leowbattle / bilinear.c
Created January 22, 2023 16:13
bilinear.c
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
@Leowbattle
Leowbattle / cholesky.c
Created January 19, 2023 22:39
cholesky.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void cholesky(float* m, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
float sum = 0;
for (int k = 0; k < j; k++) {
sum += m[i * n + k] * m[j * n + k];
@Leowbattle
Leowbattle / my_printf.c
Created January 10, 2023 23:13
Basic printf implementation supporting strings and ints.
#include <stdio.h>
#include <stdarg.h>
void fmt_int(char* buf, int n) {
int i = 0;
if (n < 0) {
*buf++ = '-';
n = -n;
}
@Leowbattle
Leowbattle / subdiv.c
Created January 6, 2023 17:09
subdiv.c
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <execinfo.h>
void panic(const char* format, ...) {
@Leowbattle
Leowbattle / isosphere.c
Created January 5, 2023 22:59
Code to generate an icosphere in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void* xmalloc(size_t size) {
void* ptr = malloc(size);
if (ptr == NULL) {
fprintf(stderr, "Failed to allocate %ld bytes\n", size);
exit(EXIT_FAILURE);
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
void* xmalloc(size_t size) {
void* ptr = malloc(size);
if (ptr == NULL) {
@Leowbattle
Leowbattle / Main.java
Created June 17, 2022 09:36
Some code for adding rational points on elliptic curves.
public class Main {
public static void main(String[] args) throws Exception {
var p = new RationalEllipticCurve.Point(new Rational(1), new Rational(2));
var a = new Rational(-7);
var b = new Rational(10);
var curve = new RationalEllipticCurve(a, b);
var r = curve.multiply(p, -10);
System.out.println(r);
# Does the decimal expansion of 1/q terminate?
def terminates(q):
while q % 2 == 0:
q //= 2
while q % 5 == 0:
q //= 5
return q == 1
# If the decimal expansion of 1/q terminates return 0, else return the period of the repeating digits.
def period(q):