Skip to content

Instantly share code, notes, and snippets.

View abecus's full-sized avatar
🕹️
Curious

Abdul Salam abecus

🕹️
Curious
View GitHub Profile
@abecus
abecus / tower_of_hanoi.py
Last active February 13, 2023 16:57
A code for solving tower of hanoi puzzle
def tower_of_hanoi(r1, r2, r3, n_pegs):
if n_pegs == 1:
yield r1, r3
return
yield from tower_of_hanoi(r1=r1, r2=r3, r3=r2, n_pegs=n_pegs - 1)
yield from tower_of_hanoi(r1=r1, r2=r2, r3=r3, n_pegs=1)
yield from tower_of_hanoi(r1=r2, r2=r1, r3=r3, n_pegs=n_pegs - 1)
@abecus
abecus / dijkstra.py
Last active February 13, 2021 19:07
from collections import defaultdict
import heapq as heap
def dijkstra(G, startingNode):
visited = set()
parentsMap = {}
pq = []
nodeCosts = defaultdict(lambda: float('inf'))
nodeCosts[startingNode] = 0
heap.heappush(pq, (0, startingNode))
import React, { useState, useEffect } from "react";
const words = ["Developer", "Programmer."];
export default function Home() {
const [index, setIndex] = useState(0);
const [subIndex, setSubIndex] = useState(0);
const [blink, setBlink] = useState(true);
const [reverse, setReverse] = useState(false);
// typeWriter
def gammaTrialMethod(n):
gamma = 1
count = 0
while n % 2 == 0:
count += 1
n = n // 2
gamma *= pow(2, ceil(count / 2))
for i in range(3, ceil(math.sqrt(n)) + 1, 2):
count = 0
def getReducedFactorization(N:int, spf:list)-> int:
"""
counts repetition of each prime from prime factorisation of N
using trial method upon spf list, and calculating the ceil of
half of all prime's powers (pow(p, ceil(a / 2))) and multiplying
them together.
"""
gamma = 1
while (N != 1):
# keep a prime in prev variable
from math import ceil, sqrt
def EratosthenesSieve(N:int)-> list:
'''
Calculating SPF (Smallest Prime Factor) for every number till N.
Time Complexity : O(NloglogN)
'''
N+=1
# stores smallest prime factor for every number
spf = [*range(N)]
def pythagoreanTriplets(n):
# calculate spf array
spf = EratosthenesSieve(2 * (n - int(sqrt(2 * n - 1))))
# looping for every values of 2*b
for b2 in range(4, 2 * (n - int(sqrt(2 * n - 1))), 2):
# calculates reduced factor of 2*b
gamma = getReducedFactorization(b2, spf)
from math import ceil, sqrt
def EratosthenesSieve(N:int)-> list:
'''
Calculating SPF (Smallest Prime Factor)
for every number till N.
Time Complexity : O(NloglogN)
'''
N+=1
@abecus
abecus / plotter.py
Last active July 8, 2020 12:25
creates Mandelbrot Set
def plotter(n, thresh, max_steps=25):
mx = 2.48 / (n-1)
my = 2.26 / (n-1)
mapper = lambda x,y: (mx*x - 2, my*y - 1.13)
img=np.full((n,n), 255)
for x in range(n):
for y in range(n):
it = get_iter(complex(*mapper(x,y)), thresh=thresh, max_steps=max_steps)
img[y][x] = 255 - it
return img
@abecus
abecus / iterations.py
Last active March 15, 2020 08:57
use to find the divergence of complex number for mandelbrot set
import matplotlib.pyplot as plt
import numpy as np
def get_iter(c:complex, thresh:int =4, max_steps:int =25) -> int:
# Z_(n) = (Z_(n-1))^2 + c
# Z_(0) = c
z=c
i=1
while i<max_steps and (z*z.conjugate()).real<thresh:
z=z*z +c