Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #12348

Re: sample scripts for solving euler problems

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!storethat.news.telefonica.de!telefonica.de!news-2.dfn.de!news.dfn.de!news.uni-stuttgart.de!news.belwue.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <1414254.xrXxnZYpxU@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Fri, 02 Mar 2012 17:40:30 +0100
User-Agent KNode/4.4.11
Content-Transfer-Encoding 8Bit
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
MIME-Version 1.0
Lines 70
NNTP-Posting-Date 02 Mar 2012 17:40:30 CET
NNTP-Posting-Host 4fa9c63f.newsspool1.arcor-online.net
X-Trace DXC=<8WZA\E[mGZV;Ef1`Jk54\ic==]BZ:af^4Fo<]lROoRQ<`=YMgDjhgR`W;:Ca2SK7TDZm8W4\YJN\UFDj8T]K0?SDmniOZG;K0[;K6EeU34Z>[
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:12348

Followups directed to: comp.lang.javascript

Show key headers only | View raw


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


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