|
| 1 | +#!/usr/bin/env python |
| 2 | +#-*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import easyPythonpi as pi |
| 5 | +import regex as re |
| 6 | + |
| 7 | +def add(x:'float', y:'float')->'float': # For addition of 2 numbers |
| 8 | + return x+y |
| 9 | + |
| 10 | +def sub(x:'float', y:'float')->'float': # For substraction of 2 numbers |
| 11 | + return x-y |
| 12 | + |
| 13 | +def multi(x:'float', y:'float')->'float': # For multiplication of 2 numbers |
| 14 | + return x*y |
| 15 | + |
| 16 | +def div(x:'float', y:'float')->'float': # For division of 2 numbers |
| 17 | + return x/y |
| 18 | + |
| 19 | +def mod(x:'float', y:'float')->'float': # For finding the modulus of 2 numbers |
| 20 | + return x%y |
| 21 | + |
| 22 | +def calculate_percentage(part, whole): |
| 23 | + if whole == 0: |
| 24 | + return 0 # Avoid division by zero |
| 25 | + return (part / whole) * 100 |
| 26 | + |
| 27 | + |
| 28 | +def factorial(n:'int')->'int': # To find the factorial of 2 numbers |
| 29 | + # single line to find factorial |
| 30 | + return 1 if (n == 1 or n == 0) else n * factorial(n - 1) |
| 31 | + |
| 32 | +# To compute the factord of the argument passed |
| 33 | +def factors(n:'int')->'int': |
| 34 | + factors = [] |
| 35 | + for i in range(1, n+1): |
| 36 | + if n % i == 0: |
| 37 | + factors.append(i) |
| 38 | + return factors |
| 39 | + |
| 40 | +def Area_circle(r:'float')->'float': # To find the area of a circle using the radius r |
| 41 | + PI = 3.142 |
| 42 | + return PI * (r * r) |
| 43 | + |
| 44 | +def Perimeter_circle(r:'float')->'float': # To find the perimeter of a circle using the radius r |
| 45 | + PI = 3.142 |
| 46 | + return 2 * PI * r |
| 47 | + |
| 48 | +def fibonacci(n:'int')->'int': #To find the nth fibonacci series |
| 49 | + """Finds the fibonacci of the nth sequence. |
| 50 | +
|
| 51 | + This function calculates the fibonacci sequence. This function calculates |
| 52 | + the nth fibonacci of a number by finding the sum of two numbers in the |
| 53 | + fibonacci sequence before n |
| 54 | +
|
| 55 | + Args: |
| 56 | + n ('int') : The number to find the fibonacci sequence of, assumes n >=1 |
| 57 | + as valid numbers to find the fibonacci of n. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + The fibonacci of arg n. |
| 61 | +
|
| 62 | + Raises: |
| 63 | + No errors |
| 64 | + """ |
| 65 | + if n<=0: |
| 66 | + raise pi.InvalidNumberFibException(n) |
| 67 | + # First Fibonacci number is 0 |
| 68 | + elif n==1: |
| 69 | + return 0 |
| 70 | + # Second Fibonacci number is 1 |
| 71 | + elif n==2: |
| 72 | + return 1 |
| 73 | + else: |
| 74 | + fib1 = 0 |
| 75 | + fib2 = 1 |
| 76 | + |
| 77 | + for i in range(2,n-1): |
| 78 | + fibN = fib1 + fib2 |
| 79 | + fib1 = fib2 |
| 80 | + fib2 = fibN |
| 81 | + |
| 82 | + return fib1 +fib2 |
| 83 | + |
| 84 | +#method to print the 1st prime number between the range |
| 85 | +def printprime(start:'int',end:'int')->'list': |
| 86 | + if start<=0: |
| 87 | + start=2 |
| 88 | + p=[] |
| 89 | + for i in range(start,end+1): |
| 90 | + j=0 |
| 91 | + for k in range(2,i): |
| 92 | + if i%k==0: |
| 93 | + j=1 |
| 94 | + break |
| 95 | + if j==0: |
| 96 | + p.append(i) |
| 97 | + |
| 98 | + return p |
| 99 | + |
| 100 | +def even_or_odd(data:'int'): |
| 101 | + try : |
| 102 | + if data%2==0: |
| 103 | + return 'even' |
| 104 | + else: |
| 105 | + return 'odd' |
| 106 | + |
| 107 | + except: |
| 108 | + print("\nError occured, parameter passed should be purely numeric") |
0 commit comments