├── README.md ├── logicGates.py ├── mathtool.py └── mathtool2.py /README.md: -------------------------------------------------------------------------------- 1 | # python 2 | This repository contains python files 3 | -------------------------------------------------------------------------------- /logicGates.py: -------------------------------------------------------------------------------- 1 | def OR(x,y): 2 | return 1 if (x==1 or y==1) else 0 3 | def AND(x,y): 4 | return 1 if (x==1 and y==1) else 0 5 | def NOT(x): 6 | return 0 if x==1 else 1 7 | 8 | def NAND(x,y): 9 | return NOT(AND(x,y)) 10 | def NOR(x,y): 11 | return NOT(OR(x,y)) 12 | def XOR(x,y): 13 | return OR(AND(x,NOT(y)),AND(NOT(x),y)) 14 | 15 | 16 | def ADDER(x,y,z): 17 | sum = XOR(XOR(x,y),z) 18 | carry = OR(AND(XOR(x,y),z),AND(x,y)) 19 | return [sum,carry] 20 | 21 | def add(arr1,arr2,c): 22 | arr = [0,0,0,0] 23 | arr[0] = ADDER(arr1[0],arr2[0],ADDER(arr1[1],arr2[1],ADDER(arr1[2],arr2[2],ADDER(arr1[3],arr2[3],c)[1])[1])[1])[0] 24 | arr[1] = ADDER(arr1[1],arr2[1],ADDER(arr1[2],arr2[2],ADDER(arr1[3],arr2[3],c)[1])[1])[0] 25 | arr[2] = ADDER(arr1[2],arr2[2],ADDER(arr1[3],arr2[3],c)[1])[0] 26 | arr[3] = ADDER(arr1[3],arr2[3],c)[0] 27 | carry = ADDER(arr1[0],arr2[0],ADDER(arr1[1],arr2[1],ADDER(arr1[2],arr2[2],ADDER(arr1[3],arr2[3],c)[1])[1])[1])[1] 28 | return [arr,carry] 29 | 30 | # addbits is a function to add two binary no.s of n bits using recursion 31 | # this function is not correct and is incomplete as well 32 | # def addbits(arr1,arr2,c,n,count): 33 | # if count==n: 34 | # return ADDER(arr1[n],arr2[n],c) 35 | # return addbits(arr1,arr2,c,n+1,count+1) 36 | 37 | print('enter your 4 bit binary numbers: [note: please note that bits should be space seperated]') 38 | while True: 39 | try: 40 | print('number 1: ',end="") 41 | arr1 = list(map(int,input().split())) 42 | print('number 2: ',end="") 43 | arr2 = list(map(int,input().split())) 44 | except Exception as e: 45 | print('[err : %s]'%e) 46 | carry = 0 47 | try: 48 | print('ans : ',*add(arr1,arr2,carry)) 49 | except Exception as e: 50 | print('[err : %s]'%e) 51 | -------------------------------------------------------------------------------- /mathtool.py: -------------------------------------------------------------------------------- 1 | # gives fibonacci series 2 | series = [] 3 | counter = 0 4 | def getfibonacciSeries(n): 5 | a = 0 6 | b = 1 7 | series.append(a) 8 | series.append(b) 9 | temp(a,b,n) 10 | return series 11 | 12 | def temp(a,b,n): 13 | c = a+b 14 | if n==0: 15 | series.append(c) 16 | return 17 | series.append(c) 18 | temp(b,c,n-1) 19 | 20 | 21 | # gives factors of a number 22 | factors = [] 23 | def getFactors(n): 24 | for i in range(2,n+1): 25 | if n%i == 0: factors.append(i) 26 | else: continue 27 | return factors 28 | 29 | 30 | # gives prime numbers 31 | primes = [] 32 | def getPrimes(n): 33 | for i in range(2,n+1): 34 | if checker(i): primes.append(i) 35 | else: continue 36 | return primes 37 | def checker(n): 38 | for i in range(2,n+1): 39 | if i==n: continue 40 | elif n%i != 0: continue 41 | else: return False 42 | return True 43 | # print(getPrimes(100)) 44 | -------------------------------------------------------------------------------- /mathtool2.py: -------------------------------------------------------------------------------- 1 | from mathtool import getFactors, getfibonacciSeries 2 | 3 | def sumSquare(n): 4 | sum = 0 5 | for i in range(1,n+1): 6 | sum += (i*i) 7 | print(sum) 8 | # sumSquare(3) 9 | 10 | #------------------------------------------------------------------------------------------------ 11 | 12 | def palindromeNum(n): 13 | b = bin(n) 14 | s = str(b[2:]) 15 | for i in range(0,int(len(s)/2)): 16 | temp = None 17 | if s[i] == s[len(s)-i-1]: 18 | temp = True 19 | else: 20 | temp = False 21 | break 22 | if temp: print("yes") 23 | else: print("no") 24 | # palindromeNum(5) 25 | 26 | #------------------------------------------------------------------------------------------------ 27 | 28 | def minSum(n): 29 | sum = 0 30 | for i in range(2,n): 31 | if n%i == 0: 32 | sum += i 33 | if sum > n: 34 | sum -= i 35 | break 36 | print(sum) 37 | # minSum(10) 38 | 39 | #------------------------------------------------------------------------------------------------ 40 | 41 | def nthFibonacci(n): 42 | s = getfibonacciSeries(n) 43 | return s[n-1] 44 | # print(nthFibonacci(5)) 45 | 46 | #------------------------------------------------------------------------------------------------ 47 | 48 | def maxNum(a,b): 49 | if a==b: return "both are equal" 50 | elif a>b: return a 51 | else: return b 52 | # print(maxNum(5,4)) 53 | 54 | #------------------------------------------------------------------------------------------------ 55 | 56 | def primeNum(a,b): 57 | for i in range(a,b+1): 58 | if checker(i): print(i,end=" ") 59 | else: continue 60 | def checker(n): 61 | for i in range(2,n): 62 | if n%i == 0: 63 | return False 64 | else: continue 65 | return True 66 | # primeNum(23,46) 67 | 68 | #------------------------------------------------------------------------------------------------ 69 | 70 | def areaOfCircle(r): 71 | return 3.14*r*r 72 | # print(areaOfCircle(2)) 73 | 74 | #------------------------------------------------------------------------------------------------ 75 | 76 | def checkInFibo(n): 77 | s = getfibonacciSeries(n) 78 | if n in s: return "yes" 79 | else: return "no" 80 | # print(checkInFibo(2)) 81 | 82 | #------------------------------------------------------------------------------------------------ 83 | 84 | def cubeSum(n): 85 | sum = 0 86 | for i in range(0,n+1): 87 | sum += (i*i*i) 88 | return sum 89 | # print(cubeSum(4)) 90 | 91 | #------------------------------------------------------------------------------------------------ 92 | 93 | def sumOfOddFactors(n): 94 | sum = 0 95 | f = getFactors(n) 96 | print(f) 97 | for i in range(0,len(f)): 98 | if f[i]%2 != 0: sum += f[i] 99 | else: continue 100 | return sum 101 | # print(sumOfOddFactors(35)) 102 | 103 | #------------------------------------------------------------------------------------------------ 104 | 105 | def sumOfArray(arr): 106 | sum = 0 107 | for i in range(0,len(arr)): 108 | sum += arr[i] 109 | return sum 110 | # print(sumOfArray([2,3,5,2])) 111 | 112 | #------------------------------------------------------------------------------------------------ 113 | 114 | def checkTriangle(a,b,c): 115 | if a+b+c != 180: return "this triangle does not exists" 116 | else: 117 | # if(a+b>) 118 | return "yes this triangle exists" 119 | # print(checkTriangle(60,60,60)) 120 | 121 | #------------------------------------------------------------------------------------------------ 122 | from mathtool import getPrimes 123 | def maxPrimeFactor(n): 124 | maxfactor = 0 125 | primes = getPrimes(n) 126 | print(primes) 127 | for i in primes: 128 | if n%i == 0: maxfactor = i 129 | return maxfactor 130 | # print(maxPrimeFactor(20)) 131 | --------------------------------------------------------------------------------