Skip to content

Instantly share code, notes, and snippets.

Avatar

Shai Almog shai-almog

View GitHub Profile
@shai-almog
shai-almog / prime-app.js
Created Oct 19, 2021
Prime number calculator used as part of a debugging tutorial on talktotheduck.dev
View prime-app.js
const MAX_NUMBER = 10 ** 9;
let cnt = 0
function isPrime(num) {
if (num === 2) {
return true;
}
if (num < 2 || num % 2 === 0) {
return false;
}
@shai-almog
shai-almog / prime_main.py
Created Oct 19, 2021
Prime number calculator used as part of a debugging tutorial on talktotheduck.dev
View prime_main.py
import math
import time
import six
if six.PY2:
# In python2, range returns a list instead of an iterator. When running on a large range,
# this can take up all of the available memory and crash the process.
range = xrange
@shai-almog
shai-almog / PrimeMainComplex.kt
Created Oct 19, 2021
Prime number calculator used as part of a debugging tutorial on talktotheduck.dev
View PrimeMainComplex.kt
import kotlin.math.pow
object PrimeMainComplex {
class PrimeChecker(i: Int) {
companion object { var zero = 0 }
var num = 0
var self: PrimeChecker
init {
@shai-almog
shai-almog / PrimeMainMR.java
Created Oct 19, 2021
Prime number calculator used as part of a debugging tutorial on talktotheduck.dev
View PrimeMainMR.java
import java.math.BigInteger;
public class PrimeMainMR {
public static int cnt = 0;
// this is the RabinMiller test, deterministically correct for n < 341,550,071,728,321
// http://rosettacode.org/wiki/Miller-Rabin_primality_test#Python:_Proved_correct_up_to_large_N
public static boolean isPrime(BigInteger n, int precision) {
if (n.compareTo(new BigInteger("341550071728321")) >= 0) {
@shai-almog
shai-almog / StackToHash.java
Created Sep 14, 2021
Convert the current stack to a checksum hex string which we can use in logs to identify call stack differences
View StackToHash.java
public class DebugUtil {
public static String stackHash() {
try {
// code from https://www.baeldung.com/java-stacktrace-to-string
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
new RuntimeException().printStackTrace(pw);
// checksuming for speed
int sum = 0;