Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #12348
| Message-ID | <1414254.xrXxnZYpxU@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2012-03-02 17:40 +0100 |
| Subject | Re: sample scripts for solving euler problems |
| Newsgroups | comp.lang.javascript |
| References | <496fa250-5efc-44f6-b88a-bca6dda08ec8@k4g2000yqa.googlegroups.com> <13347583.271.1330701787117.JavaMail.geo-discussion-forums@yner4> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
Asen Bozhilov wrote:
> scripts examples написа:
>> Got a web site setup for solving euler problems in python, perl,
>> ruby and javascript.
>>
>> Feel free to give me any feedback, thanks.
>
> I looked at Project Euler Problem 1. Obviously you can solve the problem
> with the formula of arithmetic progression sum:
>
> <URL: http://en.wikipedia.org/wiki/Arithmetic_progression>
>
> Even and without formula, you can solve it more elegant. The solution in
> Python is on a line:
>
> sum(range(3, 10, 3)) + sum(range(5, 10, 5)); #23
> sum(range(3, 1000, 3)) + sum(range(5, 1000, 5)); #266333
>
> JavaScript solution would be:
>
> function multiples(x) {
> var sum = 0, i;
> for (i = 3; i < x; i += 3) sum += i;
> for (i = 5; i < x; i += 5) sum += i;
>
> return sum;
> }
Or in JavaScript 1.7+:
function range (start, end, step)
{
for (var i = start; i < end; i += step)
{
yield i;
}
}
function sum ()
{
return (+x) + (+y);
}
[i for each (i in range(3, 10, 3))].reduce(sum)
+ [i for each (i in range(5, 10, 5))].reduce(sum)
Or, more compatible to other ECMAScript implementations:
function range (start, end, step)
{
var a = [];
for (var i = start; i < end; i += step)
{
a.push(i);
}
return a;
}
range(3, 10, 3).reduce(sum) + range(5, 10, 5).reduce(sum)
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread
sample scripts for solving euler problems scripts examples <example.scripts@gmail.com> - 2012-02-28 20:02 -0800
Re: sample scripts for solving euler problems scripts examples <example.scripts@gmail.com> - 2012-02-29 18:19 -0800
Re: sample scripts for solving euler problems Dr J R Stockton <reply1209@merlyn.demon.co.uk> - 2012-03-01 21:44 +0000
Re: sample scripts for solving euler problems Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-03-02 07:23 -0800
Re: sample scripts for solving euler problems Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-02 17:40 +0100
csiph-web