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 4232618
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:07:03+00:00 2026-05-21T02:07:03+00:00

In the form f(x,y,z) where x is a given integer sum, y is the

  • 0

In the form f(x,y,z) where x is a given integer sum, y is the minimum length of the sequence, and z is the maximum length of the sequence. But for now let’s pretend we’re dealing with a sequence of a fixed length, because it will take me a long time to write the question otherwise.

So our function is f(x,r) where x is a given integer sum and r is the length of a sequence in the list of possible sequences.

For x = 10, and r = 2, these are the possible combinations:

1 + 9
2 + 8
3 + 7
4 + 6
5 + 5

Let’s store that in Python as a list of pairs:

[(1,9), (2,8), (3,7), (4,6), (5,5)]

So usage looks like:

>>> f(10,2)
[(1,9), (2,8), (3,7), (4,6), (5,5)]

Back to the original question, where a sequence is return for each length in the range (y,x). I the form f(x,y,z), defined earlier, and leaving out sequences of length 1 (where y-z == 0), this would look like:

>>> f(10,1,3)
[{1: [(1,9), (2,8), (3,7), (4,6), (5,5)],
  2: [(1,1,8), (1,2,7), (1,3,6) ... (2,4,4) ...],
  3: [(1,1,1,7) ...]}]

So the output is a list of dictionaries where the value is a list of pairs. Not exactly optimal.

So my questions are:

  1. Is there a library that handles this already?
  2. If not, can someone help me write both of the functions I mentioned? (fixed sequence length first)?
  3. Because of the huge gaps in my knowledge of fairly trivial math, could you ignore my approach to integer storage and use whatever structure the makes the most sense?

Sorry about all of these arithmetic questions today. Thanks!

  • 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-21T02:07:03+00:00Added an answer on May 21, 2026 at 2:07 am

    The itertools module will definately be helpful as we’re dealing with premutations – however, this looks suspiciously like a homework task…

    Edit: Looks like fun though, so I’ll do an attempt.

    Edit 2: This what you want?

    from itertools import combinations_with_replacement
    from pprint import pprint
    
    f = lambda target_sum, length: [sequence for sequence in combinations_with_replacement(range(1, target_sum+1), length) if sum(sequence) == target_sum]
    
    def f2(target_sum, min_length, max_length):
        sequences = {}
        for length in range(min_length, max_length + 1):
            sequence = f(target_sum, length)
            if len(sequence):
                sequences[length] = sequence
        return sequences
    
    if __name__ == "__main__":
        print("f(10,2):")
        print(f(10,2))
        print()
        print("f(10,1,3)")
        pprint(f2(10,1,3))
    

    Output:

    f(10,2):
    [(1, 9), (2, 8), (3, 7), (4, 6), (5, 5)]
    
    f(10,1,3)
    {1: [(10,)],
     2: [(1, 9), (2, 8), (3, 7), (4, 6), (5, 5)],
     3: [(1, 1, 8),
         (1, 2, 7),
         (1, 3, 6),
         (1, 4, 5),
         (2, 2, 6),
         (2, 3, 5),
         (2, 4, 4),
         (3, 3, 4)]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a given form in a database, is there a quick/easy way to find
I've created two UserControls, a ValidationManager and a ValidationOutput. On a given form there
Given some typical search form, I can't construct this form action when submitting a
Given the following HTML form: <form id=myform> Company: <input type=text name=Company value=ACME, INC./> First
Given the code from the Complex Form part III how would you go about
Given a filename in the form someletters_12345_moreleters.ext , I want to extract the 5
Given a IPv4 address in the form of a string (ex. 10.171.24.69) and a
Given an Item that has been appended to a Form , whats the best
Given the 2D contour of a shape in the form of lines and vertices,
At work, we are given a set of constraints of the form (taskname, frequency)

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.