diff --git a/local/path/home/ContractSolverRamses.js b/local/path/home/ContractSolverRamses.js new file mode 100644 index 0000000..75087d4 --- /dev/null +++ b/local/path/home/ContractSolverRamses.js @@ -0,0 +1,161 @@ +/** @param {NS} ns */ +export async function main(ns) { + ns.tprint(ns.codingcontract.getContractTypes()) + let testcontract = ns.codingcontract.createDummyContract("Total Ways to Sum") + let contractType = ns.codingcontract.getContractType(testcontract); + ns.tprint(ns.codingcontract.getDescription(testcontract)) + let n = ns.codingcontract.getData(testcontract); + ns.tprint("Data: " + n); + let answer = ""; + if (contractType === "Find Largest Prime Factor") { + answer = largestPrimeFactor(n); + } + if (contractType === "Subarray with Maximum Sum") { + answer = SubarrayWithMaximumSum(ns, n) + } + if (contractType === "Total Ways to Sum") { + answer = TotalWaysToSum(ns, n) + } + + + ns.tprint(answer); + + ns.tprint(ns.codingcontract.attempt(answer, testcontract)); +} +/* +5: +4 1 +3 2 +3 1 1 +2 2 1 +2 1 1 1 +1 1 1 1 1 + +6: +5 1 +4 2 +4 1 1 +3 3 +3 2 1 +3 1 1 1 +2 2 2 +2 2 1 1 +2 1 1 1 1 +1 1 1 1 1 1 + +# Start with one position m filling it with the integers between 1 and target +# For each m, fill the next position n with integers between 1 and m +# Repeat as long as the sum is smaller than target. +# append all iterations to the Array and count +*/ + +function TotalWaysToSum(ns, target) { + let sumArray = []; + let inputArray = []; + let unfinishedArray = []; + let rollingSum = 0; + for (let i = 1; i < target; i++) { + inputArray.push([i]); + } + let z = 1 + while (inputArray.length > 0) { + z++ + inputArray.forEach((element) => { + rollingSum = element.reduce((a, b) => a + b, 0); + if (rollingSum === target) { + sumArray.push(element) + } else { + + for (let k = 1; k <= element[element.length-1] && k <= target - rollingSum; k++) { + + unfinishedArray.push(element.concat([k])) + } + } + } + ) + inputArray = unfinishedArray; + } + ns.tprint("Target: " +target) + ns.tprint("Length: " + sumArray.length) + return sumArray.length +} + + + + + + + + +function SubarrayWithMaximumSum(ns, givenArray) { + let arrayLength = givenArray.length; + let maxSum = -10000; + let runningSum = 0; + for (let i = 1; i <= arrayLength; i++) { + for (let j = 0; j <= arrayLength - i; j++) { + runningSum = eval(givenArray.slice(j, i + j).join('+')); + //ns.tprint("i: "+i+ " j: "+ j + " Array: "+givenArray.slice(j,i+j)+ " eval: "+ givenArray.slice(j,i+j).join('+')+"runningSum: "+runningSum); + if (maxSum < runningSum) { maxSum = runningSum }; + } + } + return maxSum +} + + +function FindLargestPrimeFactor(number) { + + let factor = 2; + while (factor * factor <= number) { + if (number % factor === 0) { + number /= factor; + } else { + factor++ + } + } + return number; +} + +/* +function FindLargestPrimeFactor(n) { + let x = Math.ceil(Math.random()*10); + let y = x; + let d = 1; + + while (d === 1) { + x = g(x, n); + y = g(g(y, n), n) + d = gcd(n, Math.abs(x - y)) + //ns.tprint("x:" + x + " y: " + y + " d: " + d) + } + if (d === n) { + return ("failure") + } + else { + return (d) + } +} + +function g(x, n) { + return (x * x) % n +} + +function gcd(a,b) { + a = Math.abs(a); + b = Math.abs(b); + if (b > a) {var temp = a; a = b; b = temp;} + while (true) { + if (b == 0) return a; + a %= b; + if (a == 0) return b; + b %= a; + } +} + + +function gcd(a, b) { + if (!b) { + return a; + } + return gcd(b, a % b); +} +*/ \ No newline at end of file