CTK Exchange
Front Page
Movie shortcuts
Personal info
Awards
Reciprocal links
Privacy Policy

Interactive Activities

Cut The Knot!
MSET99 Talk
Games & Puzzles
Arithmetic/Algebra
Geometry
Probability
Eye Opener
Analog Gadgets
Inventor's Paradox
Did you know?...
Proofs
Math as Language
Things Impossible
My Logo
Math Poll
Other Math sit's
Guest book
News sit's

Manifesto: what CTK is about |Store| Search CTK Buying a book is a commitment to learning Table of content Things you can find on CTK Chronology of updates Email to Cut The Knot

CTK Exchange

Subject: "recursive function ?"     Previous Topic | Next Topic
Printer-friendly copy     Email this topic to a friend    
Conferences The CTK Exchange College math Topic #280
Reading Topic #280
Joe Ott
guest
Jul-11-02, 09:08 PM (EST)
 
"recursive function ?"
 
   Hi
I'm looking for a solution for the following:
. Write a recursive function that returns the number of 1’s in the binary representation of N. Use the fact that this is equal to the number of 1’s in the representation on N/2, plus 1, if N is odd


  Alert | IP Printer-friendly page | Edit | Reply | Reply With Quote | Top
stapel
Member since Mar-5-02
Jul-12-02, 07:31 AM (EST)
Click to EMail stapel Click to send private message to stapel Click to view user profileClick to add this user to your buddy list  
1. "RE: recursive function ?"
In response to message #0
 
   This might help:

https://www.mathgoodies.com/forums/topic.asp?TOPIC_ID=4260


  Alert | IP Printer-friendly page | Edit | Reply | Reply With Quote | Top
Michael Klipper
guest
Jul-27-02, 02:45 PM (EST)
 
2. "RE: recursive function ?"
In response to message #1
 
   There's a small flaw with the algoritm mentioned in your page. The problem is how is treats negative numbers. If you simply stick a negative sign in front of the number, that's fine. However, if you use two's complement representation to do negative numbers, then the answer is incorrect.

In case you don't know, two's complement representation uses a leading bit to store the sign of a number. If it is 0, the number is positive, and if it is 1, then the number is negative. To negate a number, you flip ALL the bits, AND THEN you add 1 to the number.

i.e. 5 = 0101 (padded to 4 bits so you see the leading bit)
flip: 1010 = -6
add 1: 1011 = -5

Now, you should be able to see why if F(n) is the function you're making, F(n) != F(-n) for a large number of values of n. Instead, if you're computing this for negative numbers, you need to know how many bits store your number, and then you can make a proper function.


  Alert | IP Printer-friendly page | Edit | Reply | Reply With Quote | Top

Conferences | Forums | Topics | Previous Topic | Next Topic

You may be curious to visit the old CTK Exchange archive.

|Front page| |Contents| |Store|

Copyright © 1996-2018 Alexander Bogomolny

[an error occurred while processing this directive]
 Advertise

New Books
Second editions of J. Conway's classic On Numbers And Games and the inimitable Winning Ways for Your Mathematical Plays