mirror of
https://bitbucket.org/Mattrixwv/projecteulerts.git
synced 2025-12-06 17:43:59 -05:00
Added solution to problem 33
This commit is contained in:
@@ -56,12 +56,13 @@ import { Problem29 } from "./Problems/Problem29";
|
||||
import { Problem30 } from "./Problems/Problem30";
|
||||
import { Problem31 } from "./Problems/Problem31";
|
||||
import { Problem32 } from "./Problems/Problem32";
|
||||
import { Problem33 } from "./Problems/Problem33";
|
||||
|
||||
|
||||
export class ProblemSelection{
|
||||
//Holds the valid problem numbers
|
||||
public static PROBLEM_NUMBERS: number[] = [ 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];
|
||||
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33];
|
||||
|
||||
//Returns the problem corresponding to the given problem number
|
||||
public static getProblem(problemNumber: number): Problem{
|
||||
@@ -99,6 +100,7 @@ export class ProblemSelection{
|
||||
case 30: problem = new Problem30(); break;
|
||||
case 31: problem = new Problem31(); break;
|
||||
case 32: problem = new Problem32(); break;
|
||||
case 33: problem = new Problem33(); break;
|
||||
}
|
||||
return problem;
|
||||
}
|
||||
|
||||
161
Problems/Problem33.ts
Normal file
161
Problems/Problem33.ts
Normal file
@@ -0,0 +1,161 @@
|
||||
//ProjectEulerTS/Problems/Problem33.ts
|
||||
//Matthew Ellison
|
||||
// Created: 05-28-21
|
||||
//Modified: 05-28-21
|
||||
/*
|
||||
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s
|
||||
We shall consider fractions like, 30/50 = 3/5, to be trivial examples
|
||||
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator
|
||||
If the product of these four fractions is given in its lowest common terms, find the value of the denominator
|
||||
*/
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses
|
||||
/*
|
||||
Copyright (C) 2021 Matthew Ellison
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
import { gcd, getProd } from "../../../Typescript/typescriptClasses/Algorithms";
|
||||
import { Unsolved } from "../Unsolved";
|
||||
import { Problem } from "./Problem";
|
||||
|
||||
|
||||
export class Problem33 extends Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private static MIN_NUMERATOR: number = 10; //The lowest the numerator can be
|
||||
private static MAX_NUMERATOR: number = 98; //The highest the numerator can be
|
||||
private static MIN_DENOMINATOR: number = 11; //The lowest the denominator can be
|
||||
private static MAX_DENOMINATOR: number = 99; //THe highest the denominator can be
|
||||
//Instance variables
|
||||
private numerators: number[]; //Holds the numerators that were found
|
||||
private denominators: number[]; //Holds the denominators that were found
|
||||
private prodDenominator: number; //Holds the answer to the question
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public constructor(){
|
||||
super("If the product of these four fractions is given in its lowest common terms, find the value of the denominator");
|
||||
this.prodDenominator = 1;
|
||||
this.numerators = [];
|
||||
this.denominators = [];
|
||||
}
|
||||
//Operational functions
|
||||
//Solve the problem
|
||||
public solve(): void{
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(this.solved){
|
||||
return;
|
||||
}
|
||||
|
||||
//Start the timer
|
||||
this.timer.start();
|
||||
|
||||
//Search every possible numerator/denominator pair
|
||||
for(let denominator = Problem33.MIN_DENOMINATOR;denominator <= Problem33.MAX_DENOMINATOR;++denominator){
|
||||
for(let numerator = Problem33.MIN_NUMERATOR;(numerator < denominator) && (numerator <= Problem33.MAX_NUMERATOR);++numerator){
|
||||
let denom = denominator.toString();
|
||||
let num = numerator.toString();
|
||||
let tempNum: number = 0;
|
||||
let tempDenom: number = 1;
|
||||
|
||||
//Check that this isn't a trivial example
|
||||
if((num[1] == "0") && (denom[1] == "0")){
|
||||
continue;
|
||||
}
|
||||
//Remove the offending digits if they exist
|
||||
else if(num[0] == denom[0]){
|
||||
tempNum = parseInt(num[1]);
|
||||
tempDenom = parseInt(denom[1]);
|
||||
}
|
||||
else if(num[0] == denom[1]){
|
||||
tempNum = parseInt(num[1]);
|
||||
tempDenom = parseInt(denom[0]);
|
||||
}
|
||||
else if(num[1] == denom[0]){
|
||||
tempNum = parseInt(num[0]);
|
||||
tempDenom = parseInt(denom[1]);
|
||||
}
|
||||
else if(num[1] == denom[1]){
|
||||
tempNum = parseInt(num[0]);
|
||||
tempDenom = parseInt(denom[0]);
|
||||
}
|
||||
|
||||
//Test if the new fraction is the same as the old one
|
||||
if((tempNum / tempDenom) == (numerator / denominator)){
|
||||
this.numerators.push(numerator);
|
||||
this.denominators.push(denominator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Get the product of the numbers
|
||||
let numProd: number = getProd(this.numerators);
|
||||
let denomProd: number = getProd(this.denominators);
|
||||
//Get the gcd to reduce to lowest terms
|
||||
let curGcd = gcd(numProd, denomProd);
|
||||
//Save the denominator
|
||||
this.prodDenominator = denomProd / curGcd;
|
||||
|
||||
//Stop the timer
|
||||
this.timer.stop();
|
||||
|
||||
//Throw a flag to show the problem is solved
|
||||
this.solved = true;
|
||||
}
|
||||
//Reset the problem so it can be run again
|
||||
public reset(): void{
|
||||
super.reset();
|
||||
this.numerators = [];
|
||||
this.denominators = [];
|
||||
}
|
||||
//Gets
|
||||
//Returns the reult of solving the problem
|
||||
public getResult(): string{
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return `The denominator of the product is ${this.prodDenominator}`;
|
||||
}
|
||||
//Returns the list of numerators
|
||||
public getNumerators(): number[]{
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return this.numerators;
|
||||
}
|
||||
//Returns the list of denominators
|
||||
public getDenominators(): number[]{
|
||||
//If the porblem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return this.denominators;
|
||||
}
|
||||
//Returns the answer to the question
|
||||
public getProdDenominator(){
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!this.solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return this.prodDenominator;
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
The denominator of the product is 100
|
||||
It took an average of 508.730 microseconds to run this problem through 100 iterations
|
||||
*/
|
||||
Reference in New Issue
Block a user