Sum of 1 to 1000000000 in different programming languages
31–40 of 83 posts
Re: Sum of 1 to 1000000000 in different programming languages
#32APL: +/⍳1E9 Try it yourself, download NARS2000 (free) here: http://www.nars2000.org/ The "⍳" character is entered by typing ALT+i Explanation: 1E9 is 1,000,000,000 ⍳1E9 generates a vector containing integers from 1 to 1,000,000,000 (inclusive) +/ is the sum of the vector Any good APL interpreter will not actually generate a vector with a billion numbers but rather recognize the above expression and optimize the resul…
+/i.1E9+1
You can get that here: http://www.jsoftware.com/stable.htmRe: Sum of 1 to 1000000000 in different programming languages
#33Re: Sum of 1 to 1000000000 in different programming languages
#34Knowing how to use a language is critical to get expected results. This gives the proper result in PHP by forcing the integer cast. $sum = (int) $sum + $i;
I've found that web developers often undervalue (or even scoff at) web developers with CS degrees, but any first-year CS/EECE student at a decent university (or even someone who has taken a couple of Coursera CS classes) would have instantly recognized this to be a floating point related issue. Being able to whip up some scaffolds in Rails is great and immensely useful, but I'm of the opinion that a great Rails programmer will need a proper understanding of the C & Assembly that's behind all the magic.
Re: Sum of 1 to 1000000000 in different programming languages
#35It would have been interesting to see this problem solved in many different languages. But I guess that would kill the question on Stackoverflow.
Without a clear, quantitative standard as to which Python* implementation is best, you'll find: a one-liner that claims to be 'pythonic', a 150-liner that's slightly more efficient, and a 30-liner that looks like it was written in C. The more popular the language, the wider the variety of responses. The more helpful the language (that is, 'newbies' can contribute solutions to otherwise difficult problems), the more solutions. The more divisive the language, the more bickering you'll have.
The Programming Language Shootout doesn't suffer this fate because it is 0% subjective. Discussion is only interesting with subjectivity, but technical discussion is presumed to be mostly objective. StackOverflow treads the line carefully.
1 writer could do it. 2, if they don't disagree on anything.
* I don't mean to pick on Python. Python is great. Insert language-of-choice
Re: Sum of 1 to 1000000000 in different programming languages
#36Knowing how to use a language is critical to get expected results. This gives the proper result in PHP by forcing the integer cast. $sum = (int) $sum + $i;
This casts only $sum to int. If $i is a float, the result is a float. With 32 bits, $sum will be a float bigger than PHP_INT_MAX at some point, and the cast will truncate it, which is unlikely to give the right answer. With 64 bits, which I assume is the platform you tested on, $sum is never bigger than PHP_INT_MAX, so it's never converted to a float, and the (int) cast does nothing (and the computation is done right…
I posted initially with casting (int) $i but I tested it without and it worked too.
In fact on my 64bit centos machine PHP 5.4 doesn't need the cast at all and the original code gives the correct result the first time as is.
Re: Sum of 1 to 1000000000 in different programming languages
#37Knowing how to use a language is critical to get expected results. This gives the proper result in PHP by forcing the integer cast. $sum = (int) $sum + $i;
Correction, properly learning about data types and binary representation is critical to "get" programming. I've found that web developers often undervalue (or even scoff at) web developers with CS degrees, but any first-year CS/EECE student at a decent university (or even someone who has taken a couple of Coursera CS classes) would have instantly recognized this to be a floating point related issue. Being able to whi…
I find that when it comes to programmers who reach a certain level of proficiency, it doesn't matter whether they learned these things through a CS degree, or through hard knocks.
I started out with VBScript and ASP 1.0. I didn't get a CS degree, but I ended up learning about floating point errors rather quickly. This lead me to learn more about data types. Similar result, different path. I ended up paying for my error, much like someone would pay for an education.
There are certainly bad programmers without CS degrees who make these mistakes, but I know plenty of programmers who hold a CS degree and will consistently misuse float unless reminded to use something more appropriate.
Re: Sum of 1 to 1000000000 in different programming languages
#38Earlier quoted context omitted.
Did you intend to demonstrate a very common off-by-one error in Python?
Probably should've used xrange as well. In the 2.x series, Python's range function returns an actual list. I believe in 3.x range has been replaced with xrange.
Correct.
Re: Sum of 1 to 1000000000 in different programming languages
#39Posted on stackoverflow, what an irony...