In Awk, all numbers are represented internally as double precision floating-point numbers. Thus the result for the extra credit is unprecise. Since version 4.1, GNU Awk supports high precision arithmetic (using GNU MPFR and GMP) which is turned on with the -M / --bignum option. The variable PREC sets the working precision for arithmetic operations (here 80 bits):

The ZX81 doesn't offer enough numeric precision to try for the extra credit. This program is pretty unsophisticated; the only optimization is that we skip testing whether i{\displaystyle i} is divisible by 5 if we already know it's divisible by 3. (ZX81 BASIC doesn't do this automatically: both sides of an OR are evaluated, even if we don't need the second one.) Even so, with n{\displaystyle n} = 1000 the performance is pretty acceptable.

The following C# 5 / .Net 4 code is an efficient solution in that it does not iterate through the numbers 1 ... n - 1 in order to calculate the answer. On the other hand, the System.Numerics.BigInteger class (.Net 4 and upwards) is not itself efficient because calculations take place in software instead of hardware. Consequently, it may be faster to conduct the calculation for smaller values with native ("primitive") types using a 'brute force' iteration approach.

A brute-method using only comparisons and adds. Compiles and runs as is in GnuCOBOL 2.0 and Micro Focus Visual COBOL 2.3. Takes about 7.3 seconds to calculate 1,000,000,000 iterations (AMD A6 quadcore 64bit)

: main ( n -- ) 0 swap 3 do i 3 mod 0= if i + else i 5 mod 0= if i + then then loop . ;

1000 main \ 233168 ok

Another FORTH version using the Inclusion/Exclusion Principle. The result is a double precision integer (128 bits on a 64 bit computer) which lets us calculate up to 10^18 (the max precision of a single precision 64 bit integer) Since this is Project Euler problem 1, the name of the main function is named euler1tower.

The method here recalls the story of the young Gauss being set the problem of adding up all the integers from one to a hundred by a master who wanted some peace and quiet from his class. The trick here is to apply the formula for multiples of three and for five, then remember that multiples of fifteen will have been counted twice.

Early Fortrans did not offer such monsters as INTEGER*8 but the F95 compiler I have does so. Even so, the source is in the style of F77 which means that in the absence of the MODULE protocol, the types of the functions must be specified if they are not default types. F77 also does not accept the END FUNCTION name protocol that F90 does, but such documentation enables compiler checks and not using it makes me wince.

The result for a thousand million took about a minute for the brute-force-and-ignorance calculation. For much larger values of N, it should be discarded! Integer overflow even for 64-bit integers impends. The calculations could be conducted in double precision (or better, quadruple precision), a trivial modification to the source. Precise results would require the introduction of multi-precision arithmetic.

The sum of multiples of 3 or 5 between 1 and 0 is: 0
The sum of multiples of 3 or 5 between 1 and 9 is: 23
The sum of multiples of 3 or 5 between 1 and 99 is: 2318
The sum of multiples of 3 or 5 between 1 and 999 is: 233168
The sum of multiples of 3 or 5 between 1 and 9999 is: 23331668
The sum of multiples of 3 or 5 between 1 and 99999 is: 2333316668

The number of multiples of 3 or 5 below 1000 is 233168
The number of multiples of 3 or 5 below 10000 is 23331668
The number of multiples of 3 or 5 below 100000 is 2333316668
The number of multiples of 3 or 5 below 1000000 is 233333166668
The number of multiples of 3 or 5 below 10000000 is 23333331666668
The number of multiples of 3 or 5 below 100000000 is 2333333316666668

IfOpenConsole()PrintN("Sum of numbers below 1000 which are multiples of 3 or 5 is : "+ SumMultiples(1000))PrintN("")PrintN("Press any key to close the console")Repeat: Delay(10) : UntilInkey()<>""CloseConsole()EndIf

However, that's pretty dumb. We can do much better by observing that the sum of the multiples of k{\displaystyle k} below some n+1{\displaystyle n+1} is kTn/k{\displaystyle kT_{n/k}}, where Ti{\displaystyle T_{i}} is the i{\displaystyle i}'th triangular number, for which there exists a trivial formula. Then we simply use an overall formula of 3Tn/3+5Tn/5−15Tn/15{\displaystyle 3T_{n/3}+5T_{n/5}-15T_{n/15}} (that is, summing the multiples of three and the multiples of five, and then subtracting the multiples of 15 which were double-counted).