algorithm: how to seperate a number into a group of small numbers

1) there are 6 numbers 1,2,4,8,16,32,64. Increase by power 2.
2) given a number N <=64, for example 59, see what numbers in 1) consist of 59.
3) a number can be used only once.
It's better to code in DOS shell.
For example, 59 = 32+16+8+2+1
jl66Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

t0t0Commented:
Good evening.... This was a lovelly little teaser. Please see my solution below:

@echo off
if "%1"=="" exit /b
if %1 leq 0 exit /b

set dec=%1
set result=
set bit=1

:loop1
   set /a bit=bit*2
if %bit% leq %dec% goto loop1

set /a bit=bit/2

:loop2
   if %bit% leq %dec% (
      set /a dec-=bit
      if not defined result (
         set result=%bit%
      ) else (
         set result=%result%+%bit%
      )
   )
   set /a bit=bit/2
if %bit% gtr 0 goto loop2

echo %1 = %result%
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jl66Author Commented:
Excellent. One more question: if the number < 1,200,000,000, it's OK. Do you have any idea when the number is larger than it, say 8,000,000,000?
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
This will work only for 32bit signed integer, meaning 2,17 billion.

The algorithm is standard binary coding, because of "power 2" in point 1. Why to obscure that fact? Sounds like homework ...

0
t0t0Commented:
Thank you Qlemo....

jl66
There is a limit in DOS when performing integer maths. The solution provided uses mathematical evaluations to arrive at the output.And as you've discovered, this noly works upto a certain limit.

This modified loop1 will handle numbers upto 2,147,483,647.

@echo off

if "%1"=="" exit /b
if %1 leq 0 exit /b

set result=

set dec=%1
set bit=1
set tmp=1

:loop1
   set /a tmp=tmp*2
   if %tmp% gtr %dec% goto end_loop1
   set bit=%tmp%
   goto loop1
:end_loop1

:loop2
   if %bit% leq %dec% (
      set /a dec-=bit
      if not defined result (
         set result=%bit%
      ) else (
         set result=%result%+%bit%
      )
   )
   set /a bit=bit/2
if %bit% gtr 0 goto loop2

echo %1 = %result%
exit /b
0
jl66Author Commented:
Not homework but simplied real issue.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Algorithms

From novice to tech pro — start learning today.