Below is python code that attempts to find the sum of all values divided by a particular number using arithmetic progression equations found here
The program will produce incorrect output only a handful of the time, with the same numbers being off by the exact same amount everytime.
Sample Output Format: First its the number that we try to find the sum of all numbers between 0-999 that's divisible by it. Next, is the brute-force answer then my attempt and finally the difference between the two answers
ERROR: 7) correctAnswer = 71071 != testAnswer = 71000 correctAnswer-testAnswer = 71
ERROR: 11) correctAnswer = 45045 != testAnswer = 45000 correctAnswer-testAnswer = 45
ERROR: 13) correctAnswer = 38038 != testAnswer = 38000 correctAnswer-testAnswer = 38
ERROR: 15) correctAnswer = 33165 != testAnswer = 33132 correctAnswer-testAnswer = 33
ERROR: 17) correctAnswer = 29087 != testAnswer = 29058 correctAnswer-testAnswer = 29
ERROR: 19) correctAnswer = 26182 != testAnswer = 26156 correctAnswer-testAnswer = 26
ERROR: 29) correctAnswer = 17255 != testAnswer = 17238 correctAnswer-testAnswer = 17
ERROR: 31) correctAnswer = 16368 != testAnswer = 16352 correctAnswer-testAnswer = 16
ERROR: 33) correctAnswer = 15345 != testAnswer = 15330 correctAnswer-testAnswer = 15
ERROR: 35) correctAnswer = 14210 != testAnswer = 14196 correctAnswer-testAnswer = 14
ERROR: 41) correctAnswer = 12300 != testAnswer = 12288 correctAnswer-testAnswer = 12
ERROR: 45) correctAnswer = 11385 != testAnswer = 11374 correctAnswer-testAnswer = 11
ERROR: 49) correctAnswer = 10290 != testAnswer = 10280 correctAnswer-testAnswer = 10
ERROR: 53) correctAnswer = 9063 != testAnswer = 9054 correctAnswer-testAnswer = 9
ERROR: 55) correctAnswer = 9405 != testAnswer = 9396 correctAnswer-testAnswer = 9
...
The list goes on, but notice how the difference between the two answers is decreasing. The error eventually drops off at 499, in other words, a开发者_Go百科fter 499 the program works perfectly
The code is after this paragraph, it's fully documented and should be ready to just copy paste into an IDE and ran. At this point, either these problems are caused by a herp with a side of derp (trivial error) or some misunderstanding about the language. Thanks in advance for any assistence
""" SumDivisibleby returns the sum of a series of number which are divisible by the value of the parameter first
Parameters: first - An integer which specifies the first value of the arithmetic series whose constant difference is equal to first's value
last - An integer which specifies the last value of the arithmetic series whose constant difference is equal to first's value
nTerms - An integer which specifies the number of terms in the arithmetic series whose constant difference is equal to first's value """
def SumDivisibleby(first,last,nTerms): return nTerms * ((first + last)/2);
""" nthTerm finds a single term in an arithmetic series whose constant difference is equal to first's value
Parameters: first - An integer which specifies the first value of the arithmetic series whose constant difference is equal to first's value
nTerms - An integer which specifies the number of terms in the arithmetic series whose constant difference is equal to first's value
cDiff - An integer which represents the constant difference of an arithmetic series, here in case it does differ from first's value """
def nthTerm(first,nTerms,cDiff):
return first + ((nTerms - 1)*cDiff)
""" determineN finds the number of terms a particular arithmetic series would have
first - An integer which specifies the first value of the arithmetic series whose constant difference is equal to first's value
max - An integer which specifies the highest possible value allowed in the arithmetic series
cDiff - An integer which represents the constant difference of an arithmetic series, here in case it does differ from first's value """
def determineN (first, max, cDiff):
return ((max - first)/cDiff)+1
""" testSumDivisibleBy is a test driver for the three above functions """
#This value is the chosen upper bound
#for the arithmetic series
chosenMax = 999
def testSumDivisibleBy ():
for i in range(1,2000000):
#Attempting to find the sum of all values divisible by i
numberOfTerms = determineN(i, chosenMax , i)
lastTerm = nthTerm(i, numberOfTerms, i)
testAnswer = SumDivisibleby(i,lastTerm,numberOfTerms)
#This is a brute force solution to the same problem
#that SumDivisibleBy() tries to solve
correctAnswer = 0
for j in range(0,chosenMax +1,i):
correctAnswer+=j
#This prompts the user when a discrepancy has come
#up and displays debug text
if(testAnswer != correctAnswer):
print "ERROR: " + repr(i) + ") correctAnswer = " + \
repr(correctAnswer) + " != testAnswer = " + \
repr(testAnswer) + " correctAnswer-testAnswer = " + \
repr(correctAnswer-testAnswer)
return
#Function call
testSumDivisibleBy()
You're running into an issue with integer division and order of operations. Remove the parentheses to change your function from this:
def SumDivisibleby(first,last,nTerms): return nTerms * ((first + last)/2);
to this:
def SumDivisibleby(first,last,nTerms): return nTerms * (first + last)/2;
EDIT: As an example of where this is a problem, consider computing the sum of 5+10+15+20 (a 4-term sequence increasing by 5s). This is equal to 50, and should be given by SumDivisibleBy(5, 20, 4). But your version gives 48.
精彩评论