Code Reference

A list of random programs that I have written for general use. They are not difficult programs at all. The purpose of putting them here is so that they could be transformed and placed into other programs to do more complicated things.

Disclaimer: The programs may not be extremely well-written

Non-Recursive Factorial

factorial = 1

n = int(input())

for i in range(n, 1, -1):

  factorial*=i

print(factorial)


https://replit.com/@MatthewLiu23/Factorial 

Recursive Factorial

def factorial(n):

  if n == 0:

    f = 1

  else:

    f = n*factorial(n-1)

  return f

  

n = int(input())


print(factorial(n))


https://replit.com/@MatthewLiu23/factorial-but-recursive 


Sieve of Eratosthenes - Prime Number Generator

N = int(input())

isPrime = [False, False]


for i in range(2, N+1):

  isPrime.append(True)


j = 2

while (j <= int(N**0.5)):

  if isPrime[j] == True:

    for k in range(2*j, N+1, j):

      isPrime[k] = False

  j+=1


for l in range(N):

  if isPrime[l] == True:

    print(l)


Blog Post about the Sieve of Eratosthenes


https://replit.com/@MatthewLiu23/Sieve