├── README.md ├── ProjectEuler ├── readme.md ├── Problem 1.py ├── Problem 6.py ├── problem 3.py └── Problem 2.py └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # PythonIfeTraining 2 | Solutions to mathematical programs given during the virtual training 3 | -------------------------------------------------------------------------------- /ProjectEuler/readme.md: -------------------------------------------------------------------------------- 1 | Follow the following instructions: 2 | 3 | 1. Visit https://projecteuler.net 4 | 5 | 2. Create an account with them. 6 | 7 | 3. Go ahead to solve as many questions as you can solve. 8 | 9 | 4. Visit [here](https://www.youtube.com/watch?v=JxYr1x9uMjY&feature=emb_logo) for a sample video on solution to question 1. 10 | -------------------------------------------------------------------------------- /ProjectEuler/Problem 1.py: -------------------------------------------------------------------------------- 1 | 2 | def sumNumbers(n): 3 | i = 0 4 | NumList = [] 5 | while i < n: #while the current number is less than n. 6 | if (i%3)==0 or (i%5) == 0: #Check if i is divisible by 3 or 5 7 | NumList.append(i) #append the number to our list. 8 | else: 9 | pass 10 | i+= 1 #increment by i until it is equal to n. 11 | return sum(NumList) #return the sum of list. 12 | 13 | x = sumNumbers(1000) #Test the function 14 | print(x) 15 | 16 | #Answer: 233168 17 | -------------------------------------------------------------------------------- /ProjectEuler/Problem 6.py: -------------------------------------------------------------------------------- 1 | def sumSquares(n): 2 | i = 0 3 | X = 0 #initialize the variable with the sum of individual values 4 | Y = 0 #initialize the variable with the square of the sum of all values 5 | 6 | while (i <=n): #loop from 0 till the last value n 7 | X += pow(i,2) #sum of individual squares 8 | Y += i # sum of all values 9 | i+= 1 # increment i 10 | return pow(Y,2) - X #return the difference between the squares and sum 11 | 12 | Value = sumSquares(10) 13 | #Answer is 2640 14 | 15 | Value = sumSquares(100) 16 | #Answer is 25164150 17 | -------------------------------------------------------------------------------- /ProjectEuler/problem 3.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python3 2 | """Problem: Largest prime factor of 600851475143 3 | Solution: 6857 4 | Author: AnonymouX47 5 | """ 6 | 7 | # Note: (not n % p) is basically the same as (n % p == 0) but better :) 8 | 9 | def largest_prime_factor(num): 10 | """Returns the largest prime factor of 'num'""" 11 | 12 | while not num % 2: 13 | num //= 2 14 | if num == 1: 15 | return 2 16 | n = 1 17 | while num > 1: 18 | # With 2 aside, Only odd numbers can be prime 19 | n += 2 # next odd number 20 | 21 | # It's just like the normal process to get prime factors in maths. 22 | # Keep dividing num by n until num is no longer evenly divisible by n. 23 | while not num % n: 24 | num //= n 25 | 26 | return n # The number to finish dividing num is the largest 27 | 28 | num = 600851475143 29 | 30 | n = largest_prime_factor(num) 31 | print("%d is the largest prime factor of %d" % (n, num)) 32 | -------------------------------------------------------------------------------- /ProjectEuler/Problem 2.py: -------------------------------------------------------------------------------- 1 | ## Two functions 2 | ## function 1 is fibonacci: This function finds the whole list of numbers with values less than n. 3 | ## sumEvenValue: This is function 2, this finds the sum of all numbers in the list that is divisible by 2. 4 | 5 | def fibonacci(n): 6 | c = 1 7 | SequenceList = [1,2] #initialize your list with 2 starting numbers 1&2 8 | while c < n: # loop from 0 until c is greater than n 9 | c = SequenceList[-1] + SequenceList[-2] #sum the last 2 numbers in the list. 10 | SequenceList.append(c) #append the sum of the last 2 numbers. 11 | 12 | return SequenceList[:-1] #return all in the list except the number greater than n 13 | 14 | def SumEvenValue(n): 15 | Total = 0 16 | sequence = fibonacci(n) #call the function fibonacci and pass in n. 17 | for i in sequence: #loop through the fibonacci sequence 18 | if i%2 == 0: #test if current value is divisible by 2 19 | Total += i #if so add the number to total. 20 | 21 | return Total #return the total number when program is done 22 | 23 | x = SumEvenValue(4000000) #call the program to run 24 | print(x) #print your answer 25 | 26 | #Answer: 4613732 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | --------------------------------------------------------------------------------