We have received what looks to me like quite an interesting question in the Visual Basic forum concerning how to check all possible combinations of numbers in a list for those where the sum matches a given number. (That's if I'm explaining it correctly.)
Any mathematicians care to have a look?
If we can come up with an algorithm I expect translating it to VB will be easy enough.
10 10743
Ok I'll bite; just a bit of resursion will do; here's a solution: -
import java.util.ArrayList;
-
import java.util.List;
-
-
public class Sum {
-
private static int[] numbers= { 6, 5, 4, 3, 2, 1 };
-
private static int[] sumsum= new int[numbers.length];
-
-
private static int sum= 10;
-
-
private static void solution(List<Integer> solution) {
-
-
System.out.println(solution);
-
}
-
-
private static void solve(int[] numbers, int i, int sum, List<Integer> solution) {
-
-
if (sum == 0)
-
solution(solution);
-
else
-
for (; i < numbers.length && sumsum[i] >= sum; i++) {
-
if (numbers[i] <= sum) {
-
solution.add(0, numbers[i]);
-
solve(numbers, i+1, sum-numbers[i], solution);
-
solution.remove(0);
-
}
-
}
-
}
-
-
public static void main(String[] args) {
-
-
for (int s= 0, i= numbers.length; --i >= 0; ) {
-
sumsum[i]= numbers[i]+s;
-
s+= numbers[i];
-
}
-
solve(numbers, 0, sum, new ArrayList<Integer>());
-
}
-
}
The numbers that make up the problem need to be in descending order (see the code).
kind regards,
Jos
Cute integer partitioning that.
Cute integer partitioning that.
Yep, but I guess the answer isn't as interesting as the question was; no replies
nor any interest from the Basic droids; oh well ... ;-)
kind regards,
Jos
Yep, but I guess the answer isn't as interesting as the question was; no replies
nor any interest from the Basic droids; oh well ... ;-)
Probably because the solution is written in gibberish.
Oh, sorry. They call that "Java" these days. ;)
Guess we'll have to mount a translation project.
Probably because the solution is written in gibberish.
Oh, sorry. They call that "Java" these days. ;)
Guess we'll have to mount a translation project.
I can translate that to C# if you want ... not that it will look much different but it's the only .NET language I can write code in without feeling ashamed of myself.
Just look at it as pseudo code and pick up the recursion used for the generating function.
I can translate that to C# if you want ... not that it will look much different but it's the only .NET language I can write code in without feeling ashamed of myself.
Just look at it as pseudo code and pick up the recursion used for the generating function.
Yeah, I think the overall structure with the recursion is simple enough to understand. It's the specifics of what certain statements or references mean that stopped me when I glanced over it. Will have a proper look over the long weekend and see if it makes any more sense.
Yeah, I think the overall structure with the recursion is simple enough to understand. It's the specifics of what certain statements or references mean that stopped me when I glanced over it. Will have a proper look over the long weekend and see if it makes any more sense.
I'll make it easier for you and mutilate the previous algorithm implementation: -
import java.io.IOException;
-
-
public class Sum {
-
private static int[] numbers= { 6, 5, 4, 3, 2, 1 };
-
private static int[] solution= new int[numbers.length];
-
private static int ptr= 0;
-
private static int[] sumsum= new int[numbers.length];
-
-
private static int sum= 10;
-
-
private static void solution() {
-
-
for (int i= 0; i < ptr; i++)
-
System.out.print(solution[i]+" ");
-
System.out.println();
-
}
-
-
private static void solve(int[] numbers, int i, int sum) {
-
-
if (sum == 0)
-
solution();
-
else
-
for (; i < numbers.length && sumsum[i] >= sum; i++) {
-
if (numbers[i] <= sum) {
-
solution[ptr++]= numbers[i];
-
solve(numbers, i+1, sum-numbers[i]);
-
ptr--;
-
}
-
}
-
}
-
-
public static void main(String[] args) throws IOException {
-
-
for (int s= 0, i= numbers.length; --i >= 0; ) {
-
sumsum[i]= numbers[i]+s;
-
s+= numbers[i];
-
}
-
solve(numbers, 0, sum);
-
}
-
}
kind regards,
Jos
I'll make it easier for you and mutilate the previous algorithm implementation ...
Thanks. Ended up being too busy over the long weekend. Will try to get into it this week.
I really hate the two-in-one features of Java, though. You know, things like... solution[ptr++]= numbers[x]; - I believe this would translate as something like -
solution(ptr) = numbers(x)
-
ptr = ptr + 1
Correct? And if the "ptr++" had been "++ptr" then the increment would have to be done before the assignment?
Thanks. Ended up being too busy over the long weekend. Will try to get into it this week.
I really hate the two-in-one features of Java, though. You know, things like... solution[ptr++]= numbers[x]; - I believe this would translate as something like -
solution(ptr) = numbers(x)
-
ptr = ptr + 1
Correct? And if the "ptr++" had been "++ptr" then the increment would have to be done before the assignment?
Yep, correct; it isn't a Java invention. In the dark ages CPL already implemented
those pre- and post-increments. BCPL inherited them and later along the path
B, C, C++ and Java implemented those operators as well. The operators are
directly inspired by the 'inc' machine code instructions that were/are available
on many processors and are quite handy when you get used to them.
kind regards,
Jos
...quite handy when you get used to them.
Oh yes, I can see how they'd allow much more compact code. It just seems as though it comes at too high a cost in readability.
Perhaps I simply need more experience.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: suzy |
last post by:
Hi, i'm from belgium, europe, and my english is not perfect, so my excuses.
I ame looking for someone ho can help me with my problem.
I want to compare combinations in an imputfile, and if they...
|
by: MN |
last post by:
Hello all -
I have done ASP for a while but never found a quick way to handle this issue.
I have 8 dropdown boxes. Is there an easy to check that only 1 has selected
'Yes' and the others are...
|
by: pauljwilliams |
last post by:
Hi there,
I'm looking for an algorithm that will generate combinations in the
following way:
Given a lower bound, and an upper bound, generate a set of size n
containing all combinates of...
|
by: Thomas Cameron |
last post by:
Hello all,
I have an array of values which I would like to generate all possible
combinations for. And example would be the values "a,b,c". This would
produce the following:
a
ab
abc
ac
b
|
by: zjut |
last post by:
I need to implement the method : round(String name, int index)
The given string maybe the every type of float type, ( the msdn given the
regax is that :
integral-digits]exponential-digits])
...
|
by: Phil |
last post by:
Thank you for reading my plea for help.
What I'm trying to achieve is to get the subsets from a given set using the
Standard Template Library.
Say, for example, I have created an empty set and...
|
by: arushgadkar |
last post by:
Hello ,
I want a code for generating combinations in C++ /C for example ...
say you have numbers from 1,2,3,4,5.....15 . You want all possible combinations of 5 numbers . like (1,2,3,4,5), ( 3 ,10...
|
by: Bails |
last post by:
Hi all I have a theory for a lotto system and need help on how to code
it.
I want to create 1 massive database with EVERY combination of numbers
possible in a given lotto system, then remove all...
|
by: zgfareed |
last post by:
Can anyone suggest an algorithm or function to generate combinations/
permutations of a group of substrings stored in a vector. The
substrings consists of 3 letters and the resulting string...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |