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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:54:04+00:00 2026-05-31T03:54:04+00:00

Update: My trouble is in conceptually understanding how to implement it. For example(not as

  • 0

Update: My trouble is in conceptually understanding how to implement it. For example(not as exact as my solution below), I figure out the max range deviation of each value in my list, maybe for 5,3,2 it would be 4-6,2-4,1-3..I can’t figure out what to do with that range so my recursion only proceses that. I can do this with nested for loops but recursions are being a little tricky.

I have two parts of my code working. One that generates values(recursively) and one that returns a score(which I only care about solutions that exceed a certain threshold). Both work(see code below). The problem is in integrating them as I realize I can’t simply refer to the method that controls it because the recursion still generates many results. I think I need to change my recursion code to somehow incorporate the logic of my similar method(the one that returns a score).

The recursion method takes a list and a integer and tries to figure out all the unique ways the values in the sum can be multiplied to equal the sum(if you send a list of values, 5,3,2 and a target sum of 100. The formula is calculating is 5x + 3y + 2z = 100, solve for all the possible values of x, y and z. This is the case in the example code below, if you run it you’ll get the full result set). My problem is I don’t need most of the results only ones that match certain characteristics. I created a method(and plan to create more) to limit the results but I’m not sure how to design the recursive method that will allow me to save time and not calculate results I don’t need.

Here’s an example output based on a small subset of the results. Currently, I get all the results THEN I filter and remove specific results but that means I first have to create a very large dataset(most of which I do not need). Here’s a possible output(not the complete output as there are too many results and I’m doing this manually):

Initial list of values [5, 3, 2]
initial list of quantities: [6.0, 8.0, 23.0]. 

// (5*6)+(3*8)+(2*23)=100 as do all examples below but I only want to include 
//the ones with scores above 90(see similar method in my code to see how I get this score)  

[0.0, 0.0, 50.0] // score = 0.7120763990222406 < -- should not be included
[0.0, 2.0, 47.0] // score = 0.7454415587728428 < -- should not be included
[1.0, 11.0, 31.0] // score = 0.9010050506338834 < -- should be included
[1.0, 13.0, 28.0] // score = 0.9133974596215562 < -- should be included
[1.0, 29.0, 4.0] // score = 0.7124239231090319 < -- should not be included

I want to figure out a way to avoid generating the ones that should not be included, in the first place.

Hope that makes sense. Here’s the code(the first method, findVariables, generates the results using a list/sum and the second, similar, is an example of a control function which I have no clue how to integrate). If i’m not explaining it correctly I think reviewing the two methods will make what I’m doing clear.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class findVariables {

public static void findVariables(double[] constants, double sum, ArrayList<ArrayList<Integer>> ranges) {
    findVariables0(constants, sum, new double[constants.length], 0, ranges);
}

private static void findVariables0(double[] constants, double remaining, double[] variables, int n, ArrayList<ArrayList<Integer>> ranges) {
    //System.out.println();
    if(n == constants.length - 1) {
        // solution if the remaining is divisible by the last constant.
        if (remaining % constants[n] == 0) {
            variables[n] = remaining/constants[n];
            System.out.println(Arrays.toString(variables));
        }
    } else {
        for (int i = ranges.get(n).get(0), limit = (int) (remaining/constants[n]); i <= ranges.get(n).get(1); i++) {
            variables[n] = i;
            findVariables0(constants, remaining - i * constants[n], variables, n+1, ranges);
        }
    }
}

private static void similar(HashMap<String, Integer> list1, HashMap<String, Integer> list2) {

    //TODO: This is currently Euclidean Distance, change it to pearson score as it protects from grade inflation, same logic
    //TODO: assess the logic here.  My logic is all the sums are the same, then I can get an accurate difference by simply studying the differences in values they in common
    System.out.println("hello from simlair method. Hopefully I don't crash or worst..turn evil :-)");
    double runsum = 0.0;
    List<String> keys_in_common = new ArrayList<String>();
    for (Entry<String, Integer> entry : list1.entrySet())
    {
        String key = entry.getKey();
        if (list2.containsKey(key)) {
            keys_in_common.add(key);
        }
    }

    Iterator it=keys_in_common.iterator();

    while(it.hasNext())
    {
      String value=(String)it.next();

      //System.out.println("Value :"+value);
      runsum += Math.pow((list1.get(value) - list2.get(value)),2);
      //System.out.println(runsum);
    }
    double score = Math.pow(runsum, .5);
    double score_percent = (100-score)*.01;
    System.out.println(score_percent);


}

public static void main(String... args) {
    HashMap<String, Integer> list1 = new HashMap<String, Integer>();
    HashMap<String, Integer> list2 = new HashMap<String, Integer>();
    list1.put("a", 5);
    list1.put("b", 3);
    list1.put("c", 2);

    list2.put("a", 3);
    list2.put("b", 3);
    list2.put("c", 2);

    //Trying to capture the range around [6.0, 8.0, 23.0] so creating a list of list of values to keep within the range
    ArrayList<ArrayList<Integer>> listOlists = new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> singleList1 = new ArrayList<Integer>();
    singleList1.add(4);
    singleList1.add(8);
    listOlists.add(singleList1);
    ArrayList<Integer> singleList2 = new ArrayList<Integer>();
    singleList2.add(6);
    singleList2.add(10);
    listOlists.add(singleList2);
    ArrayList<Integer> singleList3 = new ArrayList<Integer>();
    singleList3.add(20);
    singleList3.add(25);
    listOlists.add(singleList3);

    System.out.println(listOlists);

    similar(list1, list2);
    findVariables(new double[]{5, 3, 2}, 100, listOlists);

}

}

My ultimate goal is to have a few thousand variables with large sums and using various type of methods like this to control the results from going too large.

Thanks! Also, as mentioned, I’m very very new to java and I’m sure I’m making mistakes. I welcome tips and suggestions on how to improve. You can see my desired output and my current code but I’m totally happy to change my entire approach if better so don’t feel that your suggestions need to be within the context of my existing code..

  • 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-31T03:54:04+00:00Added an answer on May 31, 2026 at 3:54 am

    EDIT

    Going off of our comments, here’s an idea:

    add a list of pairs to your findVariables0 function as an argument that has the ranges as the two values – e.g.

    (4,6)
    

    to represent a range of 4-6 – let’s call the list “ranges”. The, in the loop

            for (int i = 0, limit = (int) (remaining/constants[n]); i <= limit; i++) {
                variables[n] = i;
                findVariables0(constants, remaining - i * constants[n], variables, n+1);
            }
    

    set

    i = ranges[n].getFirst();" // should be (4)
    

    and add a condition value

    && i < ranges[n].getSecond(); i++) // should be 6
    

    This should limit your recursion to the ranges you want.

    End edit

    From what I can tell,what you’re saying in your code right now is:

    • Get all the possible solutions to a polynomial with n variables;
    • From that set of solutions, select ones that are geometrically close to a point I offer.

    And you want to incorporate that second statement in the first, saying:

    • Look at a point I offer, and get solutions to a polynomial that are close to that point.

    If not, then I’ve misunderstood.

    If I’m on to you, then: since – at least, in your current code – you’ll be running through all of the solutions anyway, you might want to look at instead generating the list of HashMaps close to your suggested HashMap, then just checking all of them. Depending on your definition of “close”, this might cut out a lot of extra possibilities, seeing as (if I remember my math correctly) a 3-polynomial solution that sums at 100 has 100^3 different possibilities, and 90% of 100^3 is a lot – though you do a good job cutting out a lot of them with the “remaining/constants[n]” line in your current code.

    Sadly, I’m not good enough with the math side to help you with an exact algorithm, but I hope I’ve put you on the path to one. Keep in mind that you may not be able to get all of your optimizations to play well with other optimizations, so you may have to end up choosing between some as opposed to others.

    On a good note, I have a few other notes that I hope can help:

    Except for where you “score” things, I don’t really see the need to make everything a double – the one time you do division, you cast it as an Int anyway, so it’d be best to make them all Integers, I’d think – save memory and whatnot.

    Why are the arrays passed to “similar” HashMaps? You essentially iterate through them like arrays, and you’ll always be comparing lists of the same size (right? I could be misunderstanding), so there’s no need to go through the extra hassle of storing the data with keys, then pulling it out again by running through all the keys in order.

    It’s easier/ clearer to see

    runsum += Math.pow((list1[n] - list2.get[n]),2);
    

    than all of the HashMapstuff – and it’s still O1 access time. I’m not sure what storing them in HashMaps does for you.

    Good luck! Hope I’ve helped.

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

Sidebar

Related Questions

I'm having trouble figuring out the proper way to update nested data using Google
I'm having trouble with Zend Db's update() method. It's not safe against sql injection
just having some trouble with an SQL update in PHP. Listed below is an
UPDATE I am still debugging this issue and still have not found a solution.
I am having trouble figuring out how i can update the background of my
I've trouble getting my components to update when the params has changed: package mycompany
I'm having trouble dynamically adding controls inside an update panel with partial postbacks. I've
Update: Solved, with code I got it working, see my answer below for the
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update: giving a much more thorough example. The first two solutions offered were right

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.