August 14th, 2019, 09:33 AM  #1 
Newbie Joined: May 2016 From: Sweden Posts: 7 Thanks: 0  Need help solving a problem
(@Mods: I'm not sure what section this question belongs to, so feel free to move it to another forum as you see fit) Hi, Assume you can only represent any value (V) with a combination of the four integers 1, 2, 4, 8 (J). Based on a value V, and the number of integers (P) of type J it is made up of, it possible to calculate the exact Js the value V consists of? Specifically, for any V and P, I would like to check if it comprised of the integer J. Examples: Code: V=8, P=2 > J=4, 4 V=8, P=1 > J=8 V=11, P=5 > J=4, 2, 2, 2, 1 V=11, P=3 > J=8, 2, 1 V=4, P=4 > J=1, 1, 1, 1 Is this possible at all with a simple "one liner" algorithm/function? 
August 14th, 2019, 11:24 PM  #2 
Senior Member Joined: Apr 2014 From: UK Posts: 965 Thanks: 342 
The 3rd example could also be 4, 4, 1, 1, 1 I'm not sure a oneliner is going to be easy to find, especially given multiple possible answers. A short algorithm is possible, I have one in my head which I used to find the alternative answer to the 3rd example. 
August 15th, 2019, 12:03 AM  #3 
Newbie Joined: May 2016 From: Sweden Posts: 7 Thanks: 0 
You are right about the 3rd example, of course. Good catch! However, since I am really only interested in the smallest value of J, it might still be a valid approach, though I suspect that the higher the value V, the more chances there are of "collisions". There would also likely be alternative answers that don't include the smallest J. Still, I'd be curios to see your algorithm. 
August 15th, 2019, 06:04 AM  #4 
Senior Member Joined: Jun 2019 From: USA Posts: 309 Thanks: 160 
Smallest possible value of P is easy to determine. Express V in binary, and min(P) is the sum of the last three digits plus the remaining digits rightshifted three places. n1 = V mod 2; n2 = floor(V/2) mod 2; n4 = floor(V/4) mod 4; n8 = floor(V/8); Pmin = n1 + n2 + n4 + n8 where the n1 is the number of 1s, etc.. Then by splitting the larger numbers, you can build ANY value of P from Pmin up to V. Not exactly one line, but you could write a code that fits on a computer screen to form valid J values. 
August 15th, 2019, 11:27 PM  #5 
Senior Member Joined: Apr 2014 From: UK Posts: 965 Thanks: 342 
My algorithym which I did in my head goes something like this: (This doesn't check that if V<P, which can't yield a result) Code: V = 11 P = 5 Dim R[P] ;R[] holds the result for i = 1 to P ;loop P times if V > 8 + P  i then ;if V is big enough to have an 8 and the remaining amount can still fit as all 1's R[i] = 8 ;store the number V = V  8 ;the remaining V is reduced else if V > 4 + P  i then ;if V is big enough to have a 4 and the remaining amount can still fit as all 1's R[i] = 4 ;store the number V = V  4 ;the remaining V is reduced else if V > 2 + P  i then ;if V is big enough to have a 2 and the remaining amount can still fit as all 1's R[i] = 2 ;store the number V=V  2 ;the remaining V is reduced else R[i] = 1 ;store the number V=V  1 ;the remaining V is reduced end if next i ;R[] now contains the result! 
August 16th, 2019, 09:54 AM  #6 
Newbie Joined: May 2016 From: Sweden Posts: 7 Thanks: 0 
Thanks for the ideas, I have enough info input now to solve it!


Tags 
problem, solving 
Thread Tools  
Display Modes  

Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Help solving a problem, is an example problem to understand  redsaints2010  Probability and Statistics  6  April 2nd, 2017 03:41 PM 
please help me while solving this problem  tayyab_zarif  Elementary Math  0  December 4th, 2012 02:18 AM 
Problem solving  haftakhan  Algebra  1  September 7th, 2011 08:23 AM 
problem solving  jc.xd  Algebra  1  September 11th, 2009 03:53 PM 
Problem Solving  Voltman  Algebra  2  May 8th, 2009 03:21 PM 