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

  • SEARCH
  • Home
  • 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 8946929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:37:05+00:00 2026-06-15T12:37:05+00:00

Basically this algorithm I’m writing takes as input a List L and wants to

  • 0

Basically this algorithm I’m writing takes as input a List L and wants to find a number x such that all items in L, i, minus x squared and summed are minimized. Find minimum x for the sum of abs(L[i]-x)**2. So far my algorithm is doing what it’s supposed to, just not in the cases of floating. I’m not sure how to implement floating. For example [2, 2, 3, 4] ideally would yield the result 2.75, but my algorithm isn’t currently capable of yielding floating integers.

 def minimize_square(L):
     sumsqdiff = 0
     sumsqdiffs = {}
     for j in range(min(L), max(L)):
             for i in range(len(L)-1):
                     sumsqdiff += abs(L[i]-j)**2
             sumsqdiffs[j]=sumsqdiff
             sumsqdiff = 0
     return min(sumsqdiffs, key=sumsqdiffs.get)
  • 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-06-15T12:37:07+00:00Added an answer on June 15, 2026 at 12:37 pm

    It is easy to prove [*] that the number that minimizes the sum of squared differences is the arithmetic mean of L. This gives the following simple solution:

    In [26]: L = [2, 2, 3, 4]
    
    In [27]: sum(L) / float(len(L))
    Out[27]: 2.75
    

    or, using NumPy:

    In [28]: numpy.mean(L)
    Out[28]: 2.75
    

    [*] Here is an outline of the proof:

    We need to find x that minimizes f(x) = sum((x - L[i])**2) where the sum is taken over i=0..n-1.

    Take the derivative of f(x) and set it to zero:

    2*sum(x - L[i]) = 0
    

    Using simple algebra, the above can be transformed into

    x = sum(L[i]) / n
    

    which is none other than the arithmetic mean of L. QED.

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

Sidebar

Related Questions

I'm trying to basically reverse this algorithm so that I get the time for
My understanding is that Ladner's theorem is basically this: P != NP implies that
I'm writing a program based on the multi-chain Metropolis algorithm to find minimum energy
I made a hash algorithm that uses MD5 for some low-security key generation. Basically,
I am writing a program that attempts to duplicate the algorithm discussed at the
Basically, I have a templated class that runs some algorithm, and that algorithm needs
basically i need some algorithm that fits into the following problem: i have 5
I'm looking for an algorithm that takes 2 strings and will give me back
Basically this is a question how to access local scope handler. I trying to
So basically this code: class A { }; class B { B (const B&

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.