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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:42:19+00:00 2026-05-16T08:42:19+00:00

Like Select[Tuples[Range[0, n], d], Total[#] == n &] , but faster? Update Here are

  • 0

Like Select[Tuples[Range[0, n], d], Total[#] == n &], but faster?

Update

Here are the 3 solutions and plot of their times, IntegerPartitions followed by Permutations seems to be fastest. Timing at 1, 7, 0.03 for recursive, FrobeniusSolve and IntegerPartition solutions respectively

partition[n_, 1] := {{n}};
partition[n_, d_] := 
  Flatten[Table[
    Map[Join[{k}, #] &, partition[n - k, d - 1]], {k, 0, n}], 1];
f[n_, d_, 1] := partition[n, d];
f[n_, d_, 2] := FrobeniusSolve[Array[1 &, d], n];
f[n_, d_, 3] := 
  Flatten[Permutations /@ IntegerPartitions[n, {d}, Range[0, n]], 1];
times = Table[First[Log[Timing[f[n, 8, i]]]], {i, 1, 3}, {n, 3, 8}];
Needs["PlotLegends`"];
ListLinePlot[times, PlotRange -> All, 
 PlotLegend -> {"Recursive", "Frobenius", "IntegerPartitions"}]
Exp /@ times[[All, 6]]
  • 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-16T08:42:20+00:00Added an answer on May 16, 2026 at 8:42 am

    Your function:

    In[21]:= g[n_, d_] := Select[Tuples[Range[0, n], d], Total[#] == n &]
    
    In[22]:= Timing[g[15, 4];]
    
    Out[22]= {0.219, Null}
    

    Try FrobeniusSolve:

    In[23]:= f[n_, d_] := FrobeniusSolve[ConstantArray[1, d], n]
    
    In[24]:= Timing[f[15, 4];]
    
    Out[24]= {0.031, Null}
    

    The results are the same:

    In[25]:= f[15, 4] == g[15, 4]
    
    Out[25]= True
    

    You can make it faster with IntegerPartitions, though you don’t get the results in the same order:

    In[43]:= h[n_, d_] := 
     Flatten[Permutations /@ IntegerPartitions[n, {d}, Range[0, n]], 1]
    

    The sorted results are the same:

    In[46]:= Sort[h[15, 4]] == Sort[f[15, 4]]
    
    Out[46]= True
    

    It is much faster:

    In[59]:= {Timing[h[15, 4];], Timing[h[23, 5];]}
    
    Out[59]= {{0., Null}, {0., Null}}
    

    Thanks to phadej’s fast answer for making me look again.

    Note you only need the call to Permutations (and Flatten) if you actually want all the differently ordered permutations, i.e. if you want

    In[60]:= h[3, 2]
    
    Out[60]= {{3, 0}, {0, 3}, {2, 1}, {1, 2}}
    

    instead of

    In[60]:= etc[3, 2]
    
    Out[60]= {{3, 0}, {2, 1}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Something like: SELECT MONTH(*date?*) FROM //Not sure what goes here LIMIT 0,6 I know
I would like query like SELECT 'FOO1', 'FOO2' But returns the resultset into rows
i need something like SELECT (Name & ', ' & Firstname) AS Test FROM
I have query like: SELECT * FROM uni_customer WHERE mobile REGEXP '^(1[3,4,5,8]){1}\\d{9}$' But there
I like select the two inputbox after the automplete box. The problem is that
I have a query like: SELECT ... FROM ... WHERE .. AND SomeID =
Given a query like: SELECT table1.field1 FirstField, table2.field2 SecondField FROM table1 INNER JOIN table2
My query string is like: SELECT ... FROM maintable LEFT JOIN table1 on (maintable.id
i'm joining tables like: select * from tableA a full join tableB b on
I have an query like: SELECT id as OfferId FROM offers WHERE concat(partycode, connectioncode)

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.