Skip to content

Instantly share code, notes, and snippets.

View Rudxain's full-sized avatar

Ricardo Fernández Serrata Rudxain

View GitHub Profile
@Rudxain
Rudxain / fast decimal div2.md
Last active December 19, 2022 06:41
Special-cased algorithm to divide by 2 in base 10. With a hint to an extended version that works on arbitrary radix

divide each digit by 2 starting from the MSD using int div, if you have remainder it means you have to add 5 to the next digit AFTER dividing it by 2.

Examples:

  1. 1687
  2. 1/2 = 0, carry 5 because odd
  3. 6/2 = 3, 3+5 = 8
  4. 8/2 = 4
  5. 7/2 = 3, carry 5
@Rudxain
Rudxain / Euclid-Euler theorem example.md
Last active December 18, 2022 04:27
Attempt to use binary to explain the Euclid-Euler theorem

Use binary 111110000 as example...

Add up the powers of 2, then use bit shift/multiplication/addition identities to simplify the sum of all divisors. You'll get the original Perfect number

Divisors: 1, 10, 100, 1000, 10000, 11111, 111110, 1111100, 11111000

(WIP) I'm planning on improving this, then I'll make a YT video or a GHP-blog/post about it

@Rudxain
Rudxain / keylogger.js
Created December 18, 2022 04:29
Use DOM to log keyboard-presses to the browser console
addEventListener('keydown', k => console.table(k.keyCode, k.code, k.key, String.fromCharCode(k.keyCode)))
@Rudxain
Rudxain / slice by indexOf.py
Last active December 27, 2022 20:40
Slice list by searched values, instead of indices
from typing import Final, TypeVar
_T = TypeVar('_T')
# is there some way to avoid union type?
def slice_by_search(inp: (list | tuple)[_T], start: _T, end: _T):
'''LICENSE: Unlicense'''
i: Final = 1 + inp.index(start)
return inp[i: inp.index(end, i + 1)]
@Rudxain
Rudxain / video size calculator.py
Last active February 25, 2023 02:59
Calculates the byte-size of a video as if it wasn't compressed by a codec
from typing import Final
BYTES_PER_PIXEL: Final = 3 # 24bit color, 8 per channel
def decoded_video_size(*, w: Final[int], h: Final[int], s: Final[float], fps: Final[float]):
pixels_per_frame: Final = w * h # px/px^2 = 1/px = px^-1
total_frames: Final = fps * s # px^2/s * s = px^2
total_pixels: Final = pixels_per_frame * total_frames # px^-1 * px^2 = px^2 / px^1 = px
total_bytes: Final = total_pixels * BYTES_PER_PIXEL # px * B/px = B
return total_bytes

This website seems to have a SQL-injection vulnerability, at the password reset and login sections.

I noticed this when using a password with random symbols.

I posted this gist to remind myself to tell them to fix it, and to notify everyone else about this

@Rudxain
Rudxain / variadic GCD simple CLI.rs
Last active March 10, 2023 05:54
calculates the greatest-common-divisor of many argv `i128`s using Euclidean algorithm
/// # Greatest Common Divisor / Highest Common Factor
/// between `a` & `b`,
/// using https://en.wikipedia.org/wiki/Euclidean_algorithm.
///
/// It only supports unsigneds,
/// because it's easier to avoid bugs,
/// and allows wider input ranges.
const fn gcd(mut a: u128, mut b: u128) -> u128 {
// rustc doesn't have TCO, so recursion is not an option
while b != 0 {
@Rudxain
Rudxain / remove_dupes_first_last.py
Created March 14, 2023 23:34
fn to remove duplicate ocurrences from a list, in either order (preserves original order), not in-place
from typing import Final, TypeVar
T = TypeVar('T')
def remove_dupes(inp: list[T], keep_last: bool = False) -> list[T]:
'''
creates a list from input,
such that all ocurrences (but 1) of each value are removed
(order is preserved).
@Rudxain
Rudxain / mul-table of b-1 in radix b.py
Created March 29, 2023 02:31
Showcase of math phenomenon where multiples of b-1 having a predictable pattern in base b
#!/usr/bin/env python3
from typing import Final
from string import digits, ascii_letters
from sys import argv
FULL_CHARSET: Final = digits + ascii_letters
def to_radix(n: int, charset='0123456789', lil_endian=False):
@Rudxain
Rudxain / alt-bits.js
Created May 25, 2023 02:17
Generate a sequence of bigints with alternating bits
'use strict'
const alt_bit = function*() {
let n = 0n
let even = true
while (true) {
yield n
n <<= 1n
if (even) n |= 1n
even = !even