Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6005783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:26:47+00:00 2026-05-23T01:26:47+00:00

Below is python code that attempts to find the sum of all values divided

  • 0

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, after 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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:26:47+00:00Added an answer on May 23, 2026 at 1:26 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have python code below that will loop through a table and print out
I have below a code that will plot a sphere, it's proportions are defined
In the below code ,the mails that are going from the system are going
I have two pieces of code that I'm using to learn about multiprocessing in
i'm using Python 2.5 and WinXP. i am parsing xml file as below: <Test>
I have Python code that pulls info from a sqlite database and then write
The function below takes a python file handle, reads in packed binary data from
Considering the criteria listed below, which of Python, Groovy or Ruby would you use?
Does anyone know of a faster decimal implementation in python? As the example below
Below is my $.ajax call, how do I put a selects (multiple) selected values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.