This repository was archived by the owner on Apr 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscrete.py
103 lines (82 loc) · 3.12 KB
/
discrete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Experiments in the form of step-by-step tests to demonstrate some attempted self-implemented algorithms to perform discrete-mathematical calculations, by coded computational means.
This is a personal test to see whether I: (a) adequately understand some of the concepts; and (b) can use and demonstrate programming knowledge by putting the concepts into practical-computational use.
@author: Nathaniel Schmidt<schmidty2244@gmail.com>
"""
from math import sqrt, floor# <https://docs.python.org/3/library/math.html>
# Number Theory:
def divides (a, b):
"""
Determines whether positive int $b$ can be divided by whole number $a$ without remainders.
@param a: the first int.
@type a: int
@param b: the second int.
@type b: int
@returns: The resulting truth value of the conditionally-tested input parameters.
@rtype: bool
"""
return b%a == 0
def readDivides ():
a = int (input ("Enter the divisor"))
b = int (input ("Enter the number to be divided"))
return divides (a, b)
def DividesTest ():
if readDivides ():
print ("Yes, this divides")
else:
print ("No, this does not divide without remainders")
# test, do this twice to test alternative results
DividesTest ()
DividesTest ()
def isPrime (num):
"""
Taken and modified from <https://www.programiz.com/python-programming/examples/prime-number>
"""
if num > 1:
for i in range (2, num):
if num%i == 0:
return False
break
elif i == 2:
return True
else:
return True
def readPrime ():
num = int(input("Enter a number"))
while num < 2:
print ("Please enter at least a positive number greater than one")
num = int(input("Enter a prime number"))
return num
def readPrimeTest ():
num = readPrime ()
if isPrime (num):
print ("This is a prime number")
else:
print ("this is not a prime number")
# Test
readPrimeTest ()
readPrimeTest ()
def findPrimes (num):
"""
this is the first practical step in determining the prime factorisations for any given positive int. We want to find (and start factorising from) the prime number closest to but less than the square root of the number we are wanting to prime-factorise, then return the primes to be tested.
NOTE: more robust working algorithm at https://stackoverflow.com/questions/15347174/python-finding-prime-factors
"""
primes = []
maxTest = sqrt(num)
if not maxTest.is_integer ():
maxTest = floor(maxTest)# Built-in floor quotient operator doesn't always convert from float to int, so use function from std. lib.
for i in range(2, maxTest+1):
if isPrime (i):
primes.append (i)
else:
continue
primes.reverse ()
return primes
def readTestedPrimes ():
num = int (input ("Enter the number to find testable primes for"))
primes = findPrimes (num)
return primes
def printPrimes ():
primes = readTestedPrimes ()
print(primes)
printPrimes ()