Twice Linear Java -


this problem trying solve. not looking exact solution if guide me how solve.

description: consider sequence u u defined follows: number u(0) = 1 first 1 in u. each x in u, y = 2 * x + 1 , z = 3 * x + 1 must in u too. there no other numbers in u. ex: u = [1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...] 1 gives 3 , 4, 3 gives 7 , 10, 4 gives 9 , 13, 7 gives 15 , 22 , on… task: given parameter n function dbl_linear (or dbllinear…) returns element u(n) of ordered (with <) sequence u. example: dbl_linear(10) should return 22 note: focus attention on efficiency

i have own solution above problem solution breaks down when have bigger index number input function dbl_linear. if index number 6000 or 10,000.

here own solution works smaller ranges.

   public static int dbllinear (int n) {        // code        system.out.println("the input range " + n);        arraylist<integer> possibleoutputs = new arraylist<integer>();        //linkedhashset<integer> possibleoutputswithoutduplicates = new linkedhashset<integer>();        possibleoutputs.add(1);        for(int i=0; i<n; i++)        {            int y = 2*possibleoutputs.get(i) + 1;            int z = 3*possibleoutputs.get(i) + 1;               if(!possibleoutputs.contains(y))              {                possibleoutputs.add(y);              }              if(!possibleoutputs.contains(z))              {                possibleoutputs.add(z);              }                //collections.sort(possibleoutputs);         }             //system.out.println(possibleoutputs);         collections.sort(possibleoutputs);         //system.out.println(possibleoutputs);         return possibleoutputs.get(n);     } 

as requested, guiding solution instead of giving exact solution:

you'll want create set u() in sorted order, instead of sorting after fact. you'll want use set, rather list, make adding of members without duplicating them trivial.

the efficient sorted set can use bitset.

start initializing u(1) in set, then...

search next unprocessed u(x) value, , add u(2x+1) , u(3x+1) set.

searching next unprocessed value easy nextsetbit.

for efficiency, can stop generating u(x) values @ point. exact limit left exercise student.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -