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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:59:50+00:00 2026-05-26T09:59:50+00:00

Given n ( say 3 people ) and s ( say 100$ ), we’d

  • 0

Given n ( say 3 people ) and s ( say 100$ ), we’d like to partition s among n people.

So we need all possible n-tuples that sum to s

My Scala code below:

def weights(n:Int,s:Int):List[List[Int]] = {
     List.concat( (0 to s).toList.map(List.fill(n)(_)).flatten, (0 to s).toList).
     combinations(n).filter(_.sum==s).map(_.permutations.toList).toList.flatten
}

println(weights(3,100))

This works for small values of n. ( n=1, 2, 3 or 4).

Beyond n=4, it takes a very long time, practically unusable.

I’m looking for ways to rework my code using lazy evaluation/ Stream.

My requirements : Must work for n upto 10.

Warning : The problem gets really big really fast. My results from Matlab –

---For s =100, n = 1 thru 5 results are ---
n=1 :1 combinations
n=2 :101 combinations
n=3 :5151 combinations
n=4 :176851 combinations
n=5: 4598126 combinations
---
  • 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-26T09:59:51+00:00Added an answer on May 26, 2026 at 9:59 am

    You need dynamic programming, or memoization. Same concept, anyway.

    Let’s say you have to divide s among n. Recursively, that’s defined like this:

    def permutations(s: Int, n: Int): List[List[Int]] = n match {
      case 0 => Nil
      case 1 => List(List(s))
      case _ => (0 to s).toList flatMap (x => permutations(s - x, n - 1) map (x :: _))
    }
    

    Now, this will STILL be slow as hell, but there’s a catch here… you don’t need to recompute permutations(s, n) for numbers you have already computed. So you can do this instead:

    val memoP = collection.mutable.Map.empty[(Int, Int), List[List[Int]]]
    def permutations(s: Int, n: Int): List[List[Int]] = {
      def permutationsWithHead(x: Int) = permutations(s - x, n - 1) map (x :: _)
    
      n match {
        case 0 => Nil
        case 1 => List(List(s))
        case _ => 
          memoP getOrElseUpdate ((s, n), 
                                 (0 to s).toList flatMap permutationsWithHead)
      }
    }
    

    And this can be even further improved, because it will compute every permutation. You only need to compute every combination, and then permute that without recomputing.

    To compute every combination, we can change the code like this:

    val memoC = collection.mutable.Map.empty[(Int, Int, Int), List[List[Int]]]
    def combinations(s: Int, n: Int, min: Int = 0): List[List[Int]] = {
      def combinationsWithHead(x: Int) = combinations(s - x, n - 1, x) map (x :: _)
    
      n match {
        case 0 => Nil
        case 1 => List(List(s))
        case _ => 
          memoC getOrElseUpdate ((s, n, min), 
                                 (min to s / 2).toList flatMap combinationsWithHead)
      }
    }
    

    Running combinations(100, 10) is still slow, given the sheer numbers of combinations alone. The permutations for each combination can be obtained simply calling .permutation on the combination.

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

Sidebar

Related Questions

I have a requirement. Given say Test1,Test2, I have to perform a like operation.
Given a parent URL (say http://dir.yahoo.com/News_and_Media/ ), I want to scrape all URLs which
Given a specific word pattern (say, balloon), I would like to find the number
Say that I have some SELECT statement: SELECT id, name FROM people ORDER BY
Say I have 10 prizes to give to 100 people. Each person gets a
Here on SO people sometimes say something like you cannot parse X with regular
I came across this question: say given two weights 1 and 3, u can
given a string say a 19 b c d 20, how do I test
Given a Range of numbers say 1 to 10,000, Input is in random order.
Given a regular expression: /say (hullo|goodbye) to my lovely (.*)/ and a string: my

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.