This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
NewerOlder