Advertisement
teleias

Linear Combination Finder

Sep 5th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. //How to find linear combinations.
  2. // Given 'result', 'm' and 'n', find 'a' and 'b' such that the following expression is true.
  3. //  result = m*a + n*b.
  4.  
  5. void foo(int m, int n, int result, int low, int high)
  6. {
  7.     List<Integer> aList = new ArrayList<Integer>();
  8.     List<Integer> bList = new ArrayList<Integer>();
  9.     for(int i = low; i < high; i++)
  10.     {
  11.         aList.add(m*i);
  12.         bList.add(n*i);
  13.     }
  14.     for(Integer ia : aList)
  15.     {
  16.         for(Integer ib : bList)
  17.         {
  18.             if((ia+ib) == result)
  19.             {
  20.                 System.out.println("pair found: (" + ia + ", "+ ib + ")");
  21.             }
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement