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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:44:46+00:00 2026-06-05T03:44:46+00:00

I am trying to generate all the solutions for the following equations for a

  • 0

I am trying to generate all the solutions for the following equations for a given H.

With H=4 :

1) ALL solutions for x_1 + x_2 + x_3 + x_4 =4
2) ALL solutions for x_1 + x_2 + x_3 = 4
3) ALL solutions for x_1 + x_2 = 4
4) ALL solutions for x_1 =4

For my problem, there are always 4 equations to solve (independently from the others). There are a total of 2^(H-1) solutions. For the previous one, here are the solutions :

1) 1 1 1 1
2) 1 1 2 and 1 2 1 and 2 1 1
3) 1 3 and 3 1 and 2 2
4) 4

Here is an R algorithm which solve the problem.

library(gtools)
H<-4
solutions<-NULL

for(i in seq(H))
{
    res<-permutations(H-i+1,i,repeats.allowed=T)
    resum<-apply(res,1,sum)
    id<-which(resum==H)

    print(paste("solutions with ",i," variables",sep=""))
    print(res[id,])
}

However, this algorithm makes more calculations than needed. I am sure it is possible to go faster. By that, I mean not generating the permutations for which the sums is > H

Any idea of a better algorithm for a given H ?

  • 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-05T03:44:47+00:00Added an answer on June 5, 2026 at 3:44 am

    Here’s an implementation in C++

    blah.cpp:

    #include <stdlib.h>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    vector<int> ilist;
    
    void diophantine(int n)
    {
        size_t i;
        if (n==0) 
        {
            for (i=0; i < ilist.size(); i++) cout << " " << ilist[i];
            cout << endl;
        }
        else
        {
            for (i=n; i > 0; i--)
            {
                ilist.push_back(i);
                diophantine(n-i);
                ilist.pop_back();
            }
        }          
    }
    
    
    int main(int argc, char** argv)
    {
        int n;    
    
        if (argc == 2 && (n=strtol(argv[1], NULL, 10)))
        {
            diophantine(n);
        }
        else cout << "usage: " << argv[0] << " <Z+>" << endl;
    
        return 0;
    }
    

    commandline stuff:

    $ g++ -oblah blah.cpp
    $ ./blah 4
     4
     3 1
     2 2
     2 1 1
     1 3
     1 2 1
     1 1 2
     1 1 1 1
    $
    

    Here’s an implementation in bash:

    blah.sh:

    #!/bin/bash
    
    diophantine()
    {
        local i
        local n=$1
        [[ ${n} -eq 0 ]] && echo "${ilist[@]}" ||
        {
            for ((i = n; i > 0; i--))
            do
                ilist[${#ilist[@]}]=${i}
                diophantine $((n-i))
                unset ilist[${#ilist[@]}-1]
            done               
        }    
    }
    
    RE_POS_INTEGER="^[1-9]+$"
    [[ $# -ne 1 || ! $1 =~ $RE_POS_INTEGER ]] && echo "usage: $(basename $0) <Z+>" ||
    {
        declare -a ilist=
        diophantine $1
    }
    exit 0
    

    Here’s an implementation in Python

    blah.py:

    #!/usr/bin/python
    
    import time
    import sys
    
    
    def output(l):
        if isinstance(l,tuple): map(output,l) 
        else: print l,
    
    
    #more boring faster way -----------------------
    def diophantine_f(ilist,n):
        if n == 0:
            output(ilist)
            print
        else: 
            for i in xrange(n,0,-1):
                diophantine_f((ilist,i), n-i)
    
    
    #crazy fully recursive way --------------------
    def diophantine(ilist,n,i):
        if n == 0:
            output(ilist)
            print
        elif i > 0:
            diophantine(ilist, n, diophantine((ilist,i), n-i, n-i))
        return 0 if len(ilist) == 0 else ilist[-1]-1 
    
    
    ##########################
    #main
    ##########################
    try:
    
        if    len(sys.argv) == 1:  x=int(raw_input())
        elif  len(sys.argv) == 2:  x=int(sys.argv[1])
        else: raise ValueError 
    
        if x < 1: raise ValueError
    
        print "\n"
        #diophantine((),x,x)
        diophantine_f((),x)    
        print "\nelapsed: ", time.clock()
    
    except ValueError:
        print "usage: ", sys.argv[0], " <Z+>"
        exit(1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a DNA string for example AGC . I am trying to generate all
I am trying to generate a list of all possible number combinations within a
I am trying to generate a controller with all the RESTful actions stubbed. I
I am trying to generate a query that selects all from a user table
I'm trying to generate the following html code using cl-who: <html> <body> <div id=cnt_1></div>
I am faced with the following programming problem. I need to generate n (a,
We've got a problem when trying to generate and use a really large proxy
I'd like to know if there is any way of solving the following problem
I'm trying to recursively generate all items in a list recursively. I've seen a
Given this link: http://www.argenteam.net/movie/40749/American.Reunion.%282012%29 I'm trying to get all links like this on that

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.