The Guy Who Keeps Coding While True Yavuz Ege Özcan's blog

Answering The Fourth Problem On Project Euler 8 years ago

Yet another Project Euler problem, solved with rather brute force. I simply became too business process oriented to develop clever solutions to mathematical problems, maybe? =)

The Problem

Finding the largest palindrome made from the product of two 3-digit numbers.

The Solution

console.time("p");
console.log(function () {
var x, y, z, i = 999, latestPalindrome = 0, palindromeDivider = 0;
for (x = 9; x > 0; x--) {
for (y = 9; y >= 0; y--) {
for (z = 9; z >= 0; z--) {
latestPalindrome = 100001 * x + 10010 * y + 1100 * z;
for (i = 999; i >= 100; i--) {
if (latestPalindrome % i === 0) {
palindromeDivider = latestPalindrome / i
if(palindromeDivider > 999)break;
if (palindromeDivider >= 100) {
return [
"our palindrome is ",
palindromeDivider, " x ", i,
" = ", latestPalindrome ].join("");
}
}
}
}
}
}
}());
console.timeEnd("p");
view raw solution.js hosted with ❤ by GitHub

Sorry for the lack of elegance. The most interesting thing to learn while coding this was finding out that parseInt([x,y,z,z,y,x].join(“”)) was much faster than (x100001) + (y10010) + (z*1100) in JavaScript. Hmm.

Edit: It probaby was a weird issue that I had. parseInt trick doesn’t work anymore.

Wasting Time On Project Euler 8 years ago

I wanted to share my boring solution to Problem 3 on Project Euler.

The Problem

What is the largest prime factor of the number 600851475143?

Solution

var originalTarget = 600851475143;
var target = originalTarget;
var i = 2;
while(i<target) {
while(target%i === 0) {
(function(newtarget) {
console.log(target + " can be divided by " + i + " which gives us " + newtarget);
target = newtarget;
})(target / i)
}
i++;
}
console.log("it seems like " + target + " is the biggest prime factor for " + originalTarget);
view raw solution.js hosted with ❤ by GitHub

That should do it. There could be some room to improve, however.

There Is No Spoon 8 years ago

So, here we go

I’d like to gather my thoughts about JavaScript and other technologies around web development, here. Hope it all goes well and I can wrap my head around this Jekyll stuff