From 02ca66cf91d5ca7fd0b52555822f25c83107482e Mon Sep 17 00:00:00 2001 From: - Date: Thu, 15 Oct 2020 23:32:38 +0200 Subject: [PATCH 1/4] Prime Factorisation --- mathgenerator/mathgen.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/mathgenerator/mathgen.py b/mathgenerator/mathgen.py index e9987ad..03be158 100644 --- a/mathgenerator/mathgen.py +++ b/mathgenerator/mathgen.py @@ -216,6 +216,21 @@ def multiplyIntToMatrix22(maxMatrixVal = 10, maxRes = 100): solution = f"[[{a*constant},{b*constant}],[{c*constant},{d*constant}]]" return problem, solution +def primeFactors(minVal=10, maxVal=999): + a = random.randint(minVal, maxVal) + i = 2 + factors = [] + while i*i <= a: + if a % i: + i += 1 + else: + a //= i + factors.append(i) + if a > 1: + factors.append(n) + problem = f"Find prime factors of {a}" + solution = f"{factors}" + return problem, solution # || Class Instances @@ -238,4 +253,5 @@ intDivision = Generator("Easy Division", 13,"a/b=","c",divisionToIntFunc) decimalToBinary = Generator("Decimal to Binary",14,"Binary of a=","b",DecimalToBinaryFunc) binaryToDecimal = Generator("Binary to Decimal",15,"Decimal of a=","b",BinaryToDecimalFunc) fractionDivision = Generator("Fraction Division", 16, "(a/b)/(c/d)=", "x/y", divideFractionsFunc) -intMatrix22Multiplication = Generator("Integer Multiplication with 2x2 Matrix", 17, "k * [[a,b],[c,d]]=", "[[k*a,k*b],[k*c,k*d]]", multiplyIntToMatrix22) \ No newline at end of file +intMatrix22Multiplication = Generator("Integer Multiplication with 2x2 Matrix", 17, "k * [[a,b],[c,d]]=", "[[k*a,k*b],[k*c,k*d]]", multiplyIntToMatrix22) +primeFactors = Generator("Prime Factorisation", 18, "Prime Factors of a =", "[b, c, d, ...]", primeFactors) \ No newline at end of file From f38892bec78c33218b77508bb4d825dad9b1c88a Mon Sep 17 00:00:00 2001 From: - Date: Fri, 16 Oct 2020 10:14:21 +0200 Subject: [PATCH 2/4] Fixed undeclared variable --- mathgenerator/mathgen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mathgenerator/mathgen.py b/mathgenerator/mathgen.py index 03be158..5926ba3 100644 --- a/mathgenerator/mathgen.py +++ b/mathgenerator/mathgen.py @@ -227,7 +227,7 @@ def primeFactors(minVal=10, maxVal=999): a //= i factors.append(i) if a > 1: - factors.append(n) + factors.append(a) problem = f"Find prime factors of {a}" solution = f"{factors}" return problem, solution From 08b7c701d699ed80dda64eae92e2bd2a938d15ab Mon Sep 17 00:00:00 2001 From: - Date: Fri, 16 Oct 2020 16:11:45 +0200 Subject: [PATCH 3/4] Fixed error where the problem is adjusted in the algorithm --- mathgenerator/mathgen.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/mathgenerator/mathgen.py b/mathgenerator/mathgen.py index 5926ba3..0a92142 100644 --- a/mathgenerator/mathgen.py +++ b/mathgenerator/mathgen.py @@ -218,16 +218,17 @@ def multiplyIntToMatrix22(maxMatrixVal = 10, maxRes = 100): def primeFactors(minVal=10, maxVal=999): a = random.randint(minVal, maxVal) + n = a i = 2 factors = [] - while i*i <= a: - if a % i: + while i * i <= n: + if n % i: i += 1 else: - a //= i + n //= i factors.append(i) - if a > 1: - factors.append(a) + if n > 1: + factors.append(n) problem = f"Find prime factors of {a}" solution = f"{factors}" return problem, solution @@ -254,4 +255,7 @@ decimalToBinary = Generator("Decimal to Binary",14,"Binary of a=","b",DecimalToB binaryToDecimal = Generator("Binary to Decimal",15,"Decimal of a=","b",BinaryToDecimalFunc) fractionDivision = Generator("Fraction Division", 16, "(a/b)/(c/d)=", "x/y", divideFractionsFunc) intMatrix22Multiplication = Generator("Integer Multiplication with 2x2 Matrix", 17, "k * [[a,b],[c,d]]=", "[[k*a,k*b],[k*c,k*d]]", multiplyIntToMatrix22) -primeFactors = Generator("Prime Factorisation", 18, "Prime Factors of a =", "[b, c, d, ...]", primeFactors) \ No newline at end of file +primeFactors = Generator("Prime Factorisation", 18, "Prime Factors of a =", "[b, c, d, ...]", primeFactors) + +for i in range(0, 10): + print(primeFactors()) \ No newline at end of file From 7e555f42fe522249de353bb24a67cd9bfe1d94c3 Mon Sep 17 00:00:00 2001 From: - Date: Fri, 16 Oct 2020 16:11:59 +0200 Subject: [PATCH 4/4] Adjusted difficulty --- mathgenerator/mathgen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mathgenerator/mathgen.py b/mathgenerator/mathgen.py index 0a92142..0eadbc9 100644 --- a/mathgenerator/mathgen.py +++ b/mathgenerator/mathgen.py @@ -216,7 +216,7 @@ def multiplyIntToMatrix22(maxMatrixVal = 10, maxRes = 100): solution = f"[[{a*constant},{b*constant}],[{c*constant},{d*constant}]]" return problem, solution -def primeFactors(minVal=10, maxVal=999): +def primeFactors(minVal=1, maxVal=200): a = random.randint(minVal, maxVal) n = a i = 2