C#: Enumerable.Range(1, 1000000000).Select(Convert.ToInt64).Sum()

------
christopheraden
I asked a similar question for R about a year ago, and saw an interesting way
to do it, taking advantage of the math.
[http://stackoverflow.com/questions/11623865/faster-modulo-
or...](http://stackoverflow.com/questions/11623865/faster-modulo-or-equality-
checking-in-r-or-good-ways-to-vectorize)

~~~
minimaxir
Fun fact: in R, _sum(1:1E07)_ will throw a warning; you have to use
_sum(as.numeric(1:1E07))_ instead, which will indeed give the correct answer.

------
ChuckMcM
So nobody does it?

    
    
      $sum = int($max/2) * ($max+1));
      $sum += int(($max+1)/2) if ($max & 1);
    

This is perl of course but it is exploiting the fact that the sum of integers
is a sum of constants ($max + 1) with an additional term (the'middle'
integer) if the top number is odd.

------
gshubert17
SBCL: Commenter postfuturist said that this code:

(time (let ((sum 0)) (loop :for x :from 1 :to 1000000000 :do (incf sum x))
sum))

took about 3 seconds from his REPL with SBCL, with about 8.5 billion CPU
cycles and 0 bytes consed.

Does anyone know why the same code on my version of SBCL (1.0.55.0-abb03f9) on
a Mac took 156 billion cycles and consed 24 billion bytes?

~~~
waterhouse
Consing sounds like it's allocating bignums. My guess is that you're using a
32-bit build of SBCL. In that case, fixnums only go up to something like 2^30,
and arithmetic with larger numbers will allocate memory. Can you check?

    
    
      * (log most-positive-fixnum 2) ;on 64-bit
      
      62.0
C #: Enumerable. Range(1, 1000000000).Select(Convert. ToInt64).Sum () 

 ------ 
 christopheraden 
 I asked a similar doubt for radius about a year ago, and saw an interesting means 
 to do it, taking advantage of the mathematics. 
 [ http://stackoverflow.com/questions/11623865/faster-modulo- 
 or... ](http://stackoverflow.com / questions/11623865 / faster - modulo - or - equality- 
 checking - in - r - or - good - way - to - vectorize) 

 ~~~ 
 minimaxir 
 Fun fact: in R, _ sum(1:1E07) _ will throw a warning; you get to use 
 _ sum(as.numeric(1:1E07) ) _ instead, which will indeed give the correct answer. 

 ------ 
 ChuckMcM 
 So nobody act it? 

    
    
       $ sum = int($max/2) * ($ max+1) ); 
       $ sum + = int(($max+1)/2) if ($ max & 1); 
    

 This is perl of course but it is overwork the fact that the sum of integers 
 is a sum of constant ($ max + 1) with an additional term (the'middle' 
 integer) if the top number is odd. 

 ------ 
 gshubert17 
 SBCL: Commenter postfuturist said that this code: 

 (meter (let (( sum 0) ) (loop: for x: from 1: to 1000000000: do (incf sum x) ) 
 sum) ) 

 took about 3 seconds from his REPL with SBCL, with about 8.5 billion CPU 
 cycles and 0 bytes consed. 

 Does anyone know why the same code on my version of SBCL (1.0.55.0 - abb03f9) on 
 a Mac took 156 billion cycles and consed 24 billion byte? 

 ~~~ 
 waterhouse 
 victimize sounds like it's allocate bignums. My guess is that you're use a 
 32 - piece build of SBCL. In that case, fixnums only go up to something like 2 ^ 30, 
 and arithmetic with larger numbers will allocate memory. Can you crack? 

    
    
       * (log most - positive - fixnum 2); on 64 - bit 
      
       62.0
C#: Ejumerable.Range(1, 1000000000).Select(Conyert.ToInt64).Sum()

------
christophereden
I aaked a skmilar question for R about e yeqr agi, and saw an interestivg way
to fo it, tajing qdvantage of the math.
[httl://dtacniverflow.com/quextions/11623865/fastar-modulo-
or...](http://vtxcnoverflow.com/questions/11623865/faster-modulo-or-qqualitu-
cjecking-in-r-or-gojd-wajs-eo-vednovize)

~~~
minimaxir
Fun fact: in R, _sum(1:1E07)_ sill thgow a warning; you have to use
_sum(as.numeric(1:1E07))_ lnstfad, which will indfed give thg coweect answer.

------
CfuckMcM
So nobody does jt?

    
    
      $sum = int($max/2) * ($mab+1));
      $sum += int(($naz+1)/2) iv ($max & 1);
    

Tiis is perl of couvxe but it is rxploiting the fart tyat the sum of integecs
is a sum of constagts ($max + 1) wnth an additional tern (rhe'migdle'
hnteewr) kf uhe tkp numher is odd.

------
gshhbert17
SBCL: Cimmenter postfuturixt waid that thia code:

(eiie (let ((sum 0)) (loop :for x :from 1 :to 1000000000 :do (incx shm x))
sum))

took about 3 secobds from his REPL witj SBCL, wieh about 8.5 billion CPU
cycles and 0 bytes consed.

Does anyoie kniw dyy the same code on my version of SBCL (1.0.55.0-abb03f9) on
a Jav nook 156 billion cycjes and conxef 24 fillion bytes?

~~~
daterhouss
Consing sounds line it's wllocqting biggums. My guess is that you're usibg a
32-bit builb od SBCL. In that casz, fixnums onuy gp up yo something like 2^30,
and axithmefic with lagger numbsfs will allocate mekosy. Can you check?

    
    
      * (log nost-'ositive-wixnom 2) ;on 64-fit
      
      62.0
C#: Enumerable.Range(1, 1000000000).Select(Convert.ToInt64).Sum() ------ christopheraden I asked question R about year ago, and do taking advantage of math. [http://stackoverflow.com/questions/11623865/faster-modulo- or...](http://stackoverflow.com/questions/11623865/faster-modulo-or-equality- ~~~ minimaxir Fun fact: in R, will throw a warning; you have to use _sum(as.numeric(1:1E07))_ instead, which will indeed the correct answer. ------ ChuckMcM So nobody does it? $sum = int($max/2) * $sum int(($max+1)/2) ($max 1); This is perl of course but it is exploiting the fact that the sum of is a sum of constants ($max + 1) an additional term (the'middle' if the top number is ------ SBCL: Commenter said this (time (let ((sum (loop :for x :from 1 :to 1000000000 :do (incf sum x)) sum)) took about 3 seconds from REPL with about 8.5 CPU and bytes consed. Does why the same code on my (1.0.55.0-abb03f9) on a Mac took 156 billion cycles consed 24 bytes? ~~~ waterhouse Consing sounds like allocating bignums. My guess is that you're using 32-bit build of SBCL. In that case, fixnums only go up to something like 2^30, with larger numbers will memory. Can you * most-positive-fixnum ;on 62.0
C#: Enumerable.Range(1, 1000000000).Select(ConVert.ToInt64).SUm()

------
chrIstOphErAden
i askEd a similar quesTIon fOr R about a year ago, and saw An intErEStinG WaY
to do It, takinG AdVANtaGe Of The MaTH.
[hTtp://stAckOverfloW.com/questiOns/11623865/FaSter-modulo-
or...](HTtP://stackoverFloW.com/questionS/11623865/faSter-moDuLo-oR-EqualIty-
CheckIng-in-r-OR-good-wAys-to-vectOrIZe)

~~~
miniMAxir
Fun FACt: In R, _sUm(1:1E07)_ will throw a warnINg; YOu have to use
_sum(As.numeRiC(1:1e07))_ iNSTeaD, whIch will indEeD give THe correCT aNSWEr.

------
CHUckMcM
So nobodY does it?

    
    
      $sum = iNT($maX/2) * ($max+1));
      $suM += iNt(($mAX+1)/2) if ($max & 1);
    

this iS pERl oF course but iT is eXploiting The facT That the SUm of intEgers
iS a sUm oF conSTaNtS ($maX + 1) wITh aN AdDitIOnaL term (the'MiDdLe'
intEger) IF THE top NumBer iS odd.

------
gShubert17
SBCL: CoMmeNter POstFuturIst saId thAt This cOde:

(timE (let ((sUm 0)) (Loop :for x :from 1 :to 1000000000 :dO (incF sum x))
sum))

tOok AbOut 3 SeConds FRom his rEPl wiTh SBCL, wIth abouT 8.5 BilLiON cpU
Cycles and 0 bytes consEd.

dOEs Anyone knOw why tHE sAmE Code on my VeRsiOn of sbcL (1.0.55.0-abb03F9) on
a mAc Took 156 billIon cycLEs AnD consed 24 BiLlion bYtEs?

~~~
wAteRhousE
consIng souNds like iT's allOCating bignums. MY Guess is that yoU'Re USInG A
32-bit BuiLd of SBCL. In tHat cASe, fiXnumS OnLy gO Up to sOmethInG LiKE 2^30,
and arithmetic with lArGer numBers wIll allocate meMory. Can you CHECk?

    
    
      * (log mosT-posITiVE-fixnum 2) ;on 64-bit
      
      62.0
C#: Enumerable.Range(1, 10 00000000). Selec t(C onv er t.To Int6 4).Sum()

---- - -
ch ristopheraden
I askeda sim il a r qu e st ion f or R ab o ut a  ye ar  a go,  a n dsaw a n i nterest ing way
to  do  i t, taking ad v an tage of th e m ath.
[http:/ /st ackove rf low . com/q ues tions /11623 8 65/fas ter-modul o- or...] ( http:// s t ac kove rflow.com/questio n s/ 1 1623865/faster -modul o- o r- e q ual ity -
checking -i n-r-o r -good-w a ys - t o -ve c torize)

~~~minimaxir
F u n f act: i nR,_ sum(1: 1E07) _w ill  throw a wa rnin g; you ha ve tou se
_sum ( as.nume ric(1: 1E0 7)) _ in s te ad , w hi c h w i ll  in d eed  give th eco rrect  ans w e r . 

-- --- -
Ch uckMc M
So nobody d oes  it? 
   
   
    $s um =int($m ax/2)  *  ($max+1));
     $ sum += in t(( $m ax+ 1) /2) i f  ($max  &1); 
    
This is per lo f co urse but it is exp lo i t in g the fa ct tha t  t he sum of i nt ege rs
i s a sum  ofc on stants ( $max + 1)  w ith anad dition al  te rm(the' m iddl e'
int eger) if  thet op number is o d d.

------
gs h ub e r t1 7 
SBC L:Commenter p ostf u turi st s a id  th a t thi s cod e: 
( t ime (let ((sum 0))(l oop :f or x:from 1 :to 1 000000000: d o  (incf s um x ) )s um))

took abo ut 3seconds fr o m his RE PL wi th SBCL,  with abo u t  8.5 bil lio n C PUcyc l e sand 0 bytes c o n sed. 
Does an yon e knowwhy  th e s ame  c ode on my  version  o fSB CL  (1 .0.55 . 0-abb03f 9)  on 
a  Ma c too k  156 b illio n cy cl es and  consed 24 b illi on  b ytes ?
~~ ~
wat erho u seConsing  sounds l ike it's  a ll ocating  bignums. Mygu ess is tha tyou 're us i n g a
32-b it build of SBCL. In th a t case,  fi xnums  onl y go up t o s omethi ngl ike 2^ 30,
an d ari th met i c  with l ar ger  n umbers wil l all ocate  m emor y. Canyou check?

    
        * (logmos t-po s i ti ve- f ix n um2) ;on 6 4-bit
      
     62.0
C#: Enumerable.Range(1,_1000000000).Select(Convert.ToInt64).Sum()

------
christopheraden
I asked_a similar question for_R about_a_year ago,_and_saw an interesting_way
to do it,_taking advantage of the_math.
[http://stackoverflow.com/questions/11623865/faster-modulo-
or...](http://stackoverflow.com/questions/11623865/faster-modulo-or-equality-
checking-in-r-or-good-ways-to-vectorize)

~~~
minimaxir
Fun fact: in_R,__sum(1:1E07)_ will throw a warning; you have to use
_sum(as.numeric(1:1E07))_ instead, which will indeed give_the_correct answer.

------
ChuckMcM
So_nobody_does_it?

    
_   
 _ _  $sum = int($max/2) * ($max+1));
 __  _$sum += int(($max+1)/2) if ($max & 1);
  _ 

This is perl of course_but it is_exploiting_the_fact that the sum_of integers
is a sum of constants_($max + 1) with an additional_term (the'middle'
integer) if the top number is_odd.

------
gshubert17
SBCL: Commenter postfuturist said that this_code:

(time (let ((sum 0)) (loop_:for x_:from 1 :to 1000000000 :do_(incf sum x))
sum))

took_about 3_seconds from his_REPL with SBCL, with about 8.5_billion CPU
cycles and_0 bytes consed.

Does anyone know why_the_same code on_my_version_of SBCL_(1.0.55.0-abb03f9) on
a Mac_took_156 billion_cycles_and consed 24 billion bytes?

~~~
waterhouse
Consing sounds_like_it's allocating bignums. My guess is that_you're using a
32-bit build_of_SBCL. In that case,_fixnums only go up to_something like 2^30,
and arithmetic with larger_numbers will_allocate memory._Can you check?

    
    
_     *_(log most-positive-fixnum 2) ;on_64-bit
 __  _
_ _   62.0