Skip to content

Instantly share code, notes, and snippets.

View gunvirranu's full-sized avatar
🤡
clownin around

Gunvir Singh Ranu gunvirranu

🤡
clownin around
  • University of Toronto
  • Palo Alto, California
  • LinkedIn in/gunvirranu
View GitHub Profile
@gunvirranu
gunvirranu / .clang-format
Last active January 30, 2024 03:15
mon format style
# Gunvir Singh Ranu
# Last Updated: 2024-01-29
# Options as of clang-format 19
# Commented out some currently unsupported by CLion
AccessModifierOffset: -4
AlignAfterOpenBracket: BlockIndent
AlignArrayOfStructures: Right
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: Consecutive
@gunvirranu
gunvirranu / rsa_example.py
Created November 16, 2021 09:07
A small Python snippet demonstrating how RSA works.
# Generating some parameters and the public-private key-pair
p, q = 997, 991 # Two "big" secret prime numbers
N = p * q # This is public
phi = (p - 1) * (q - 1) # This *must* be private!
public = 65537 # Choose some prime that's not a factor of `phi`
assert phi % public != 0
# Inverse of `e` mod `phi` found using Euclidean algo
# The important part is that you *need* `phi` to calculate this
private = 919313
assert (private * public) % phi == 1 # Make sure it's the inverse
@gunvirranu
gunvirranu / float_truncation.py
Last active March 8, 2021 19:44
Most floats don't end! They're just truncated for display.
import struct
from decimal import *
getcontext().prec = 40
x = 1.987654321987654321
h = int.from_bytes(struct.pack(">d", x), "big")
f = h & ((1 << 52) - 1)
e = (h >> 52) & ((1 << 11) - 1)
s = h >> 63
@gunvirranu
gunvirranu / sqrt_digits.py
Last active May 7, 2021 17:56
Showcasing float rounding with a very roundabout way of calculating square roots
import math
from decimal import *
# Max precision `prec` you'd want
getcontext().prec = 80
def integral_root(num):
for i in range(int(num) + 1):
if i ** 2 == num:
return i
@gunvirranu
gunvirranu / vec3d.hpp
Last active August 22, 2023 20:59
A simple, sensible, and general 3D vector type for C++
// Created by: Gunvir Ranu
// License: WTFPL
//
// If you want to use another type or don't like the `real` alias, a simple
// find and replace should suffice. If you really wanna get generic, throw this
// bad boy in a `template <typename real>` and you're set.
#include <ostream>
#include <cmath>
@gunvirranu
gunvirranu / logistic_bifurcation.py
Last active March 19, 2017 18:49
A bifurcation diagram using the logistic difference equation
import matplotlib.pyplot as plt
import numpy as np
"""
Bifurcation Diagram
"""
# Number of `r` steps in closed interval [0.5, 4]
rsteps = 1000
# Values of r between [0.5, 4], but can be changed to zoom
@gunvirranu
gunvirranu / Vector2D.java
Created March 17, 2017 07:03
Complete 2D Vector Class for Java
public class Vector2D {
public double x;
public double y;
public Vector2D() { }
public Vector2D(double x, double y) {
this.x = x;