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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:21:36+00:00 2026-06-15T13:21:36+00:00

Conditions: a + b + c = 100 a,b,c positive integers or 0 Desired

  • 0

Conditions:

a + b + c = 100
a,b,c positive integers or 0

Desired output:

[
  [0,0,100],
  [0,1,99 ],
  ... # all other permutations
  [99,1,0 ],
  [100,0,0]
]
  • 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-15T13:21:38+00:00Added an answer on June 15, 2026 at 1:21 pm

    I’d write:

    (0..100).flat_map { |x| (0..100-x).map { |y| [x, y, 100-x-y] } }
    #=> [[0, 0, 100], [0, 1, 99]], ..., [99, 1, 0], [100, 0, 0]]
    

    Site note 1: this is a classical example where list-comprehensions shine (and even more if there were a condition somewhere). Since Ruby has no LC we have to do the typical conversion to OOP: N-1 flat_map‘s + 1 map. It would be awesome to have LCs in Ruby (check this feature request), Scala has proven that even a pure OOP language greatly benefits from this syntactic sugar (though I can understand prevention from the devs because of the implicit iterable protocol/method). On an imaginary Ruby that supported them you’d write:

    [[x, y, 100-x-y] for x in 0..100 for y in 0..100-x] # imaginary Ruby
    

    Side note 2: Imagine that you prefer a less memory-consuming solution (you probably don’t need the whole array). A lazy solution with Ruby 2.0 requires just to add a couple of [lazy][2] proxies:

    (0..100).lazy.flat_map { |x| (0..100-x).lazy.map { |y| [x, y, 100-x-y] } }
    

    Side note 3: Just for completeness, in the line of @akuhn answer, another lazy solution using enumerators:

    Enumerator.new do |e| 
      (0..100).each { |x| (0..100-x).each { |y| e.yield([x, y, 100-x-y]) } }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I set conditions to affect all images within my webpage (via CSS)
rails 2.3.4, sqlite3 I'm trying this Production.find(:all, :conditions => ["time > ?", start_time.utc], :order
In the below Stored procedure, i am returning a row if all the conditions
How to write a regular expression in javascript that must follow the conditions All
Hi all I finally got my validation for my invoices model working 100% but
Possible Duplicate: Display numbers from 1 to 100 without loops or conditions Interview question:
Conditions: Windows 98 SE WMI not available I have code that looks like this,
I am evaluating certain conditions sent by a jsp page and sending the ajax
How can i make conditions on sending emails to users based on date which
what sort of conditions can we use for branching in jinja2? I mean can

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.