At the bottom ("Accessing the backend") let's look into the file "finance3.js".
Do you see there this line: usdToForeignRates = newUsdToForeignRates; ?
If I understood the things correctly, this is a good memory-leak, huh? Or not?
@import JavaScriptCore;
...
// get UIWebView's JavaScript context
JSContext *ctx = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
// 'listen' to console.log()
ctx[@"console"][@"log"] = ^(NSString *message) {
NSLog(@"Javascript's console.log() :\n%@\n\n", message);
};
After writing this angry post I just have to mention really good tools and their eminent authors.
1. VIM
3. Firebug
5. ReSharper
6. XCode
7. Sublime Text
8. Bitbucket (and thanks for Git, Linus!)
9. WireShark
10. Parse
To be continued.
window.onscroll = function() {
var scrolled = window.pageYOffset || document.documentElement.scrollTop;
// do something
}
because variable 'scrolled' will be updated only once - after the scroll is completely finished.
Suppose we have the main page (local context) of our Metro application written in JavaScript that just holds an IFRAME (web context). We load into this iframe some web page remotely. Yes, we can control this page, it is our web site and we can edit it if we want. Well, this web page contains primitive navigation to some other pages within the same domain. OK, after clicking every navigation link the iframe's content changes, but... iframe's 'SRC' attribute - NOT (!)
No difference on RTM or on RC: the issue still exists. Cheers to Microsoft.
And if you still think that the Google Chrome's JavaScript Engine (V8, WebKit) is faster than the old good "Gecko" from Mozilla Firefox, just check the link below:
HERE IS THE TEST (nothing dangerous)
I hate frameworks and wrappers of any kind, because of... You must understand that any framework causes performance leaks.
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 (!)
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
Object.prototype.foo = 10; console.log(foo); // 10
var a = {};
console.log(a.b === undefined); // true because property b is not set
undefined = 42;
console.log(a.b === undefined); // false
"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!!!
console.log(NaN === NaN); // false console.log(Math.min() < Math.max()); // false
Etc. etc. etc...
Do you like it? Look here for more shit.
Stupid brute force, not masterpiece, which is generally not suitable on really large numbers. But it is easy.
var num = 1000, sum = 0, res = 0;
while(num--){
if(!(num%3) || !(num%5))
res += num;
}
print(res);
// time: 0.02s memory: 4984 kB
Let's use the power of math coprocessor ;)
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.
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."
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.
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
Well...
I'm sure there is a lot of other cheats in Internet.
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)
OK, just the code without any clarifications because no one reads my blog.
var n = 100; var sqsum = (n * (n + 1) * (2 * n + 1)) / 6; var sumsq = (1 + n) * n / 2; print(sumsq*sumsq - sqsum);
('n' is here just for clarity of formula)
(time: 0.02s memory: 4984 kB on usual PC)
Congratulations, the answer you gave to problem 6 is correct.
You are the 132085th person to have solved this problem.
UPDATED:
krewllobster has also offered an interesting and fast option:
var a = 0, b = 0, x = 1;
while (x < 101) {
a += Math.pow(x,2);
b += x;
x += 1;
}
print(Math.pow(b,2) - a);
(time: 0.01s memory: 4984 kB)
I like Free Software Foundation, Inc.
I like to use the right tool for the right job too.
So to get the number I typed 'bc' in the Linux command line and then '2^1000'.
Well, now I am ready to calculate the sum via JavaScript (SpiderMonkey):
var a="copypasted value from my command line as STRING"; var sum = 0; for(var i = a.length - 1; i > -1; --i) sum += parseInt(a.charAt(i)); print(sum);
(time: 0.02s memory: 4984 kB on usual PC)
Congratulations, the answer you gave to problem 16 is correct.
You are the 70718th person to have solved this problem.
This problem is about permutations and so called central binomial coefficient (the binomial theorem you must remember from the school). And do you remember the Pascal's triangle? If not, check it here. (If you still don't understand what I am talking about you can see the complete solution here).
All you need is to observe that for a NxN grid there are (2n)!/(n!)2 possible ways of getting from one corner to the other one and in our case it will be 40!/(20!)2. If you are still unable to calculate it you can use A000984.
But if you still want to get the answer by yourself, do not rush to crack factorials with your lovely brute force with 350 lines of C++ code, let's start from... little cheat.
We have some ways for cheat.
Of course we could use the J language to get the central binomial coefficient:
(! +:) 20x
or even more shorter:
20!40x
but it isn't real cheat. There is a better way: http://www.google.com/search?q=40+choose+20
Bingo? Btw. tell me truth, did you know that the Google calculator has the operator 'choose'?
Brilliant, isn't? You just command "40 choose 20" and Google gives you the answer: please, master! Try the same way to ask Google for money ;)
Well, now let's start thinking.
Rudy Penteado from Brazil codes in Assembler language. He discovered that:
"This is what I find 2 months ago when I solved it:
Each movement in the horizontal is a zero.
Each movement in the vertical is a one.
1st binary# in this series:
0000000000000000000011111111111111111111
last:
1111111111111111111100000000000000000000
For the numbers in between, the amount of zeros should be the same as ones.
In other words, the ones and zeros have to be rearranged."
Easy, isn't? Try to code this in Assembler.
I won't. I did my solution with some magic too:
var ans = 1; for(var c = 40, d = 1; c > 20; --c, ++d) ans = (ans * c)/d; print(ans);
// time: 0.02s memory: 4984 kB
Congratulations, the answer you gave to problem 15 is correct.
You are the 53180th person to have solved this problem.
"Problem 8
Find the greatest product of five consecutive digits in the 1000-digit number."
Well, this problem can be solved even without computer.
Just use the best tool that you never had: your brain ;)
Use the "Find" command in your favorite text editor to highlight all 9 in the given 1000-digit number.
Is it not easy to find a combination of 99879?
But if you wanna code:
var initial = [the given 1000-digit number as array of integers];
var answer = 0;
var sum = 0;
var bestsum = 0;
var tarr = [5];
for(var c = initial.length - 1; c --> 3;) {
sum = 0;
for(var s = c; s > c-5; --s)
sum += initial[s];
if(sum > bestsum) {
bestsum = sum;
for(var j = c, i = 4; j > c-5; --j, --i)
tarr[i] = initial[j];
}
}
answer = tarr[0] * tarr[1] * tarr[2] * tarr[3] * tarr[4];
print(answer);
#include <stdio.h>
// gcc factorial.c -std=c99 -time -o factorial_c99
int zeta(int n) {
int ret = 0;
for(int p = 5; p <= n; p*=5)
ret += n/p;
return ret;
}
int main() {
int t, n;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
printf("%d\n", zeta(n));
}
return 0;
}
ACHTUNG: this code perfectly runs at http://www.ideone.com/, but not by SPOJ lamers.
importPackage(java.io);
importPackage(java.lang);
var reader = new BufferedReader( new InputStreamReader(System['in']) );
var t = reader.readLine();
var num = null;
function zeta(n) {
var ret = 0;
for(var p = 5; p <= n; p *= 5)
ret += parseInt(n/p);
return ret.toString();
}
while(t--) {
num = reader.readLine();
if(!num)
break;
System.out.println( zeta(num) );
}