xxxxxxxxxx
256
function setup() {
}
//Multiples of 3 or 5
function p1(n){
let sum = 0;
for(let i = 1; i < n; i++){
if(i % 3 == 0 || i % 5 == 0){
sum += i;
}
}
return sum;
}
//Even Fibonacci numbers
function p2(limit){
let n1 = 1;
let n2 = 2;
let temp = 0;
while (n2 < limit){
temp = n2;
n2 = n1 + n2;
if(temp % 2 == 0){result += temp;}
}
return result;
}
//smallest multiple
function p5(n){
let result = 1;
while(true){
let check = 0;
for(let i = 0; i <= n; i++){
if(result % i == 0){
check++;
}
}
if(check == n){
break
}else{
result ++;
}
}
console.log(result);
}
//Sum square difference
function p6(n){
let squaredSum = 0;
let sum = 0;
for(let i = 1; i <= n; i++){
squaredSum += i*i;
sum += i;
}
console.log(sum*sum - squaredSum);
}
//Longest Collatz Sequence
function p14(){
let record = 0;
let largestSeqGen = 1;
for(let i = 1; i <= 1000000; i++){
let seqLen = 0;
let t = i;
while(t != 1){
if(t % 2 == 0){
t = t/2;
}else{
t = 3 * t + 1
}
seqLen++;
}
if(seqLen > record){
record = seqLen;
largestSeqGen = i;
}
}
console.log(`The longest sequnece is ${record} generated by ${largestSeqGen}`);
}
//Lychel Numbers
function p55(){
let lychrelCount = 0;
let number;
for(let i = 1; i <= 10000; i++){
number = i;
let count = 0;
for(let j = 1; j <= 50; j++){
//get flipped number
const numberReversed = flip(number);
const result = number + numberReversed;
//check if palindrome
if(result != flip(result)){
number = result;
count++;
}else{
break
}
}
//see if count reached 50 i.e. a lychrel number
if(count == 50){
lychrelCount++;
}
}
console.log(lychrelCount);
}
function flip(x){
let tempArray = [];
for(let i = str(x).length; i >= 0; i--) {tempArray.push(str(x)[i]);}
return int(tempArray.join(''));
}
//Path Sum
function p81(){
loadStrings("p081_matrix.txt",p81_read);
}
function p81_read(result){
//turn txt file into 2d array
for(let i = 0; i < result.length; i++){
result[i] = int(result[i].split(","));
}
const len = result.length - 1;
for(let i = len; i >= 0; i--){
for(let j = len; j >= 0; j--){
if(i < len && j < len){
result[i][j] += min(result[i][j+1],result[i+1][j]);
}else if(i < len){
result[i][j] += result[i+1][j];
}else if(j < len){
result[i][j] += result[i][j+1];
}
}
}
console.log(result)
console.log(`Min path: ${result[0][0]}`);
}
//Path Sum
function p82(){
loadStrings("p082_matrix.txt",p82_read);
}
function p82_read(result){
const len = result.length;
//turn txt file into 2d array
for(let i = 0; i < len; i++){
result[i] = int(result[i].split(","));
}
//transpose that bad boi using fancy stuff
let trans = result[0].map((_, colIndex) => result.map(row => row[colIndex]));
//loop through cols
for(let i = 1; i < len; i++){
let sumUp = [];
let sumDown = [];
//loop through column entries top to bot
for(let j = 0; j < len; j++){
//edge case bot - only compute left + self
if(j == 0){
sumUp[j] = trans[i][j] + trans[i-1][j];
}else{ //otherwise compute min of left + self and down + left
sumUp[j] = trans[i][j] + min(trans[i-1][j],sumUp[j-1]);
}
}
//loop through entries bot to top
for(let j = len - 1; j >= 0; j--){
//edge case up - only compute left + self
if(j == len - 1){
sumDown[j] = trans[i][j] + trans[i-1][j];
}else{ //otherwise compute min of left + self and up + left
sumDown[j] = trans[i][j] + min(trans[i-1][j],sumDown[j+1]);
}
}
//update matrix with smallest value
for(let j = 0; j < len; j++){
trans[i][j] = min(sumUp[j],sumDown[j]);
}
}
//find min value of last col
console.log(`Min path: ${min(trans[len-1])}`);
}
//Path Sum -- Incomplete!
function p83(){
loadStrings("p083_matrix.txt",p83_read);
}
function p83_read(result){
const len = result.length;
//turn txt file into 2d array
for(let i = 0; i < len; i++){
result[i] = int(result[i].split(","));
}
//transpose that bad boi using fancy stuff
let trans = result[0].map((_, colIndex) => result.map(row => row[colIndex]));
console.log(trans)
//loop through cols
for(let i = 1; i < len; i++){
let sumUp = [];
let sumDown = [];
//loop through column entries top to bot
for(let j = 0; j < len; j++){
//edge case bot - only compute left + self
if(j == 0){
sumUp[j] = trans[i][j] + trans[i-1][j];
}else{ //otherwise compute min of left + self and down + left
sumUp[j] = trans[i][j] + min(trans[i-1][j],sumUp[j-1]);
}
}
//loop through entries bot to top
for(let j = len - 1; j >= 0; j--){
//edge case up - only compute left + self
if(j == len - 1){
sumDown[j] = trans[i][j] + trans[i-1][j];
}else{ //otherwise compute min of left + self and up + left
sumDown[j] = trans[i][j] + min(trans[i-1][j],sumDown[j+1]);
}
}
//update matrix with smallest value
for(let j = 0; j < len; j++){
trans[i][j] = min(sumUp[j],sumDown[j]);
}
console.log(trans[i]);
}
console.log(`Min path: ${trans[len-1][len-1]}`);
}