tags:

views:

264

answers:

8
+1  A: 

The language doesn't matter, so long as you have a bignum library. A rough pseudo-code solution would be:

str = ""
sum = 0
while input
    get character from input
    if character is not ','
        append character to back of str
    else
        convert str to number
        add number to sum
        str = ""
output sum
Pesto
Since the input isn't guaranteed to end in a comma, you'd also want to make sure that you convert and add str when the `while` finishes.
Daniel LeCheminant
@Daniel: Good point. Have a cookie.
Pesto
+1  A: 

If all of the numbers are smaller than (2**64)/600000 (which still has 14 digits), an 8 byte datatype like "long long" in C will be enough. The program is pretty straight-forward, use the language of your choice.

schnaader
A: 

python can handle the big integers.

daustin777
A: 

Since it's expensive to treat that large input as a whole I suggest you take a look at this post. It explains how to write a generator for string splitting. It's in C# but it well suited for crunching through that kind of input.

If you are worried about the total sum to not fit in a integer (say 32-bit) you can just as easily implement a bignum your self, especially if you just use integer and addition. Just carry the bit-31 to next dword and keep adding.

If precision isn't important, just accumulate the result in a double. That should give you plenty of range.

John Leidegren
+5  A: 

Penguin Sed, "Awk"

sed -e 's/,/\n/g' tmp.txt | awk 'BEGIN {total=0} {total += $1} END {print total}'

Assumptions

  • Your file is tmp.txt (you can edit this obviously)
  • Awk can handle numbers that large
Trampas Kirk
A: 
tr "," "\n" < file | any old script for summing

Ruby is convenient, since it automatically handles big numbers. I can't remember of Awk does arbitrary precision arithmentic, but if so, you could use

awk 'BEGIN {RS="," ; sum = 0 }
     {sum += $1 }
     END { print sum }' < file
Charlie Martin
this splits the line into fields, adds the first field to 0, ignores the other n-1 fields, spits out the first number and exits
Trampas Kirk
typo, thanks, corrected it.
Charlie Martin
A: 

http://www.koders.com/csharp/fid881E3E70CC37E480545A0C37C98BC8C208B06723.aspx?s=datatable#L12

A fast C# CSV parser. I've seen it crunch though a few thousand 1MB files rather quickly, I have it running as part of a service that consumes about 6000 files a month.

No need to reinvent a fast wheel.

Eric H
+4  A: 

Python

sum(map(int,open('file.dat').readline().split(',')))
One line, impressive.
sblundy