Monday, May 21, 2012

JavaScript SUX or "Gimme more shit, please!"

Shit #1

Just try this:

console.log(0.1 + 0.2 == 0.3);

Output: false (!)
Why?
Because JavaScript sux and muzdie:

console.log(0.1 + 0.2);

Output: 0.30000000000000004 (!)

Shit #2

console.log(Number.MIN_VALUE < Number.MAX_VALUE);

Output: false (!)
Why?
Because JavaScript sux and muzdie:

console.log(Number.MIN_VALUE);
console.log(Number.MAX_VALUE);

Output: 5e-324
1.7976931348623157e+308

Shit #3 (secure scope)

Object.prototype.foo = 10; 
console.log(foo); // 10

Shit #4 (happy debugging)

var a = {};
console.log(a.b === undefined); // true because property b is not set
undefined = 42;
console.log(a.b === undefined); // false

Shit #5 (are you duck?)

"string" instanceof String; // false. 
    // 'course it isn't not a string, it may look like a string
    // but actually it's masquerading as a banana.

When is a string, not a string? When it’s a duck!!!

Gimme more shit, please!

console.log(NaN === NaN); // false
console.log(Math.min() < Math.max()); // false

Etc. etc. etc...
Do you like it? Look here for more shit.

Thursday, May 17, 2012

Do not be afraid to seem a fool

"Do not be afraid to seem a fool, be afraid to be a fool." (c)

Wednesday, May 16, 2012

Project Euler, problem 1 solution

Problem 1

Stupid brute force, not masterpiece, which is generally not suitable on really large numbers. But it is easy.

JavaScript (Spider Monkey)

var num = 1000, sum = 0, res = 0;
while(num--){
    if(!(num%3) || !(num%5))
        res += num;
}
print(res);

// time: 0.02s memory: 4984 kB

Tuesday, May 15, 2012

Project Euler, problem 13 solution

Problem 13

Let's use the power of math coprocessor ;)

JavaScript (Spider Monkey)

var numbers = [
    3.7107287533902102798797998220837590246510135740250,
    4.6376937677490009712648124896970078050417018260538,
    ............... etc
    5.3503534226472524250874054075591789781264330331690
];

var sum = 0;
for(var i = numbers.length - 1; i > -1; --i) {
    sum += numbers[i];
}
var result = sum * 10000000000;
print(result.toString().substring(0,10));

// time: 0.01s memory: 4984 kB

Congratulations, the answer you gave to problem 13 is correct.
You are the 67470th person to have solved this problem.

Project Euler, problem 28 solution

Problem 28

Somebody named... Euler wrote:
"First I noted that for an n by n grid, and n being odd, the number in the top right corner is n2.
A little mathematical analysis told me that the other corners are given by: n2-n+1, n2-2n+2, and n2-3n+3.
Adding these together gives the quadratic, 4n2-6n+6.
Then all I had to do was create a loop from 3 to 1001 in steps of 2 and find the running total
(starting from 1) of the quadratic."

JavaScript (Spider Monkey)

var s = 1;
for(var n = 3; n <= 1001; n += 2) {
    s += 4 * Math.pow(n,2) - 6 * n + 6;
}
print(s);
// time: 0.02s    memory: 4984 kB

Congratulations, the answer you gave to problem 28 is correct.
You are the 39019th person to have solved this problem.

Friday, May 11, 2012

Project Euler, Problem 9 solution

Problem 9

JavaScript (SpiderMonkey)

var limit = 500,
    product = 0,
    pow_M = 0, 
    pow_N = 0;

for(var n = 3; n < limit; ++n) {
    for(var m = 4; m < limit; ++m) {
        product = m * (m + n);
        if(product == limit) {
            pow_M = Math.pow(m, 2);
            pow_N = Math.pow(n, 2);
            print((pow_M - pow_N) * (2*(m*n)) * (pow_M + pow_N));
            n = limit;
            break;
        }
    }
}

time: 0.01s memory: 4984 kB

Congratulations, the answer you gave to problem 9 is correct.
You are the 99646th person to have solved this problem.
You have earned 1 new award:
Decathlete: Solve ten consecutive problems

Project Euler, Problem 5 solution

Problem 5

First of all I wanted refresh my school math knowledge here.
Then I've wrote the optimized program.
And then I read this:

"24 Jul 2004 01:43 am
bitRAKE (Assembler)

This does not require programming at all.
Compute the prime factorization of each number from 1 to 20, and multiply the greatest power of each prime together:
20 = 2^2 * 5
19 = 19
18 = 2 * 3^2
17 = 17
16 = 2^4
15 = 3 * 5
14 = 2 * 7
13 = 13
11 = 11
All others are included in the previous numbers."

So you better type into Linux command shell 'bc' and then 2^4 * 3^2 * 5 * 7 * 11 * 13 * 17 * 19


Congratulations, the answer you gave to problem 5 is correct.
You are the 131586th person to have solved this problem.

Sunday, May 6, 2012

How to determine whether a number is degree of given number?

For example, 128 is 27, 65536 is 216 etc.
Question: how to determine whether a number 794889263257962974796277498092801308291525640763748664903194643469338087775424965801409745320266996710649718116931109481559848982586784968419475084821084743272680947722675151641735826243378403750534655587182832000457137589153821622272 is degree of 2 or not?
Answer: f**k off it is easy :)

Well, how?

OK, let's consider the case with 2. We have the number system with base of 2, where:
decimal 0 == binary 0
decimal 1 == binary 1
decimal 2 == binary 10
decimal 4 == binary 100
decimal 8 == binary 1000
decimal 16 == binary 10000
etc.

Don't you see now, how to? ;)

Question: how to determine whether a number 340282366920938463463374607431768211456 is degree of 16 or not?
Answer: ? (But now you know, how to get it!)

Tuesday, May 1, 2012

Project Euler, Problem 3 solution

Problem 3

Well...

Cheat One

Cheat Two

Cheat Three

I'm sure there is a lot of other cheats in Internet.

 

JavaScript (SpiderMonkey):

var num = 600851475143;
var ans = 0;
for(var div = 3; ; div += 2) {
    if(!(num % div)) {
        do {num /= div;} while (!(num % div));
        if(num == 1) {
            ans = div;
            break;
        }
    }
}
print(ans);

(time: 0.02s memory: 4984 kB)

Congratulations, the answer you gave to problem 3 is correct.
You are the 127054th person to have solved this problem.

UPDATED at 16 May 2012: Solution with one loop:

var num = 600851475143;
var div = 2;
while (num > 1) {
    if (0 == (num % div)) {
        num /= div;
        div--;
    }
    div++;
}
print(div);

(time: 0.02s memory: 4984 kB)