Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Challenge - Binary Digit Sum Even/Odd 1

Status
Not open for further replies.

Tarwn

Programmer
Mar 20, 2001
5,787
US
Challenge:
Given the page:
BinaryChallenge.asp
Code:
<%
Option Explicit
%>
<html>
<body>
<form name=&quot;frmNumber&quot; action=&quot;BinaryChallenge.asp&quot; method=&quot;POST&quot;>
This page will tell you if the sum of the digits in a binary representation of your number is even or odd.<br>
For example:
<table>
	<tr>
		<td>Your Entry</td>
		<td>Binary Representation</td>
		<td>Total Of Digits</td>
		<td>Even/Odd</td>
	</tr>
	<tr>
		<td>5</td>
		<td>101</td>
		<td>2</td>
		<td>Even</td>
	</tr>
	<tr>
		<td>10</td>
		<td>1010</td>
		<td>2</td>
		<td>Even</td>
	</tr>
	<tr>
		<td>14</td>
		<td>1110</td>
		<td>3</td>
		<td>Odd</td>
	</tr>
</table>
Enter a number: <input type=&quot;text&quot; name=&quot;txtUserEntry&quot;><br>
<input type=&quot;submit&quot; value=&quot;Get Answer&quot;>
</form>
<%
'------- This section only displays if they entered a number on the previous page
Dim userNumber, result
If Request.Form(&quot;txtUserEntry&quot;) <> &quot;&quot; Then
	If isNumeric(Request.Form(&quot;txtUserEntry&quot;)) Then
		userNumber = Request.Form(&quot;txtUserEntry&quot;)
		result = ChallengeFunction(userNumber)
		Response.Write &quot;The sum of the digits for the number &quot;&userNumber&&quot; is &quot;&result
	Else
		Response.Write &quot;Your entry must be a number!&quot;
	End If
End If
'------- End Display for previous entry

'--------------------------------- ChallengeFunction Area ---------------------
Function ChallengeFunction(numStr)

End Function
'------------------------------------------------------------------------------
%>
</body>
</html>
You may write any type of code you wish inside the ChallengeFunction Area without changing any of the code outside of it. The challenge here is to not only finish the ChallengeFunction, but to do it in the least number of lines possible.

-Tarwn &quot;Customer Support is an art not a service&quot; - marketing saying
&quot;So are most other forms of torture&quot; - programmers response
(The Wiz Biz - Rick Cook)
 
how's this?

Function ChallengeFunction(numStr)
dim digit, x, y, z, remainder,q
result=0
y=userNumber
z=1
do while y>0
q=y/2
x = split(q,&quot;.&quot;)
remainder = y - (2*x(0))
If (remainder= 0) Then
digit = CStr(0) & digit
Else
result=result+1
digit = CStr(1) & digit
End If
y = x(0)
loop
ChallengeFunction=result

End Function

That was fun, a little frustrating till I worked out the math properly...kept my day interesting
Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
Hmm, close, but it should be returning 'even' or 'odd'.
Still 17 lines is good, anyone else?
-Tarwn &quot;The problem with a kludge is eventually you're going to have to back and do it right.&quot; - Programmers Saying (The Wiz Biz - Rick Cook)
&quot;Your a geek!&quot; - My Girlfriends saying
 
dim value,carry,templen,tempoddeven,dec
carry=usernumber
do
tempint=int(formatnumber(int(carry/2),0))
if carry=(tempint*2) then dec=&quot;0&quot; & dec
if carry=(tempint*2) then dec=&quot;1&quot; & dec
carry=tempint
loop until carry<1
templen=len(dec)
tempoddeven=int(formatnumber(int(templen/2),0))
if templen=(tempoddeven*2) then response.write &quot;Even&quot;
if templen<>(tempoddeven*2) then response.write &quot;Odd&quot;
 
Slight amendment

dim value,carry,templen,tempoddeven,dec
carry=usernumber
do
tempint=int(formatnumber(int(carry/2),0))
if carry=(tempint*2) then dec=&quot;0&quot; & dec
if carry<>(tempint*2) then dec=&quot;1&quot; & dec
carry=tempint
if carry=1 then dec=&quot;1&quot; & dec:carry=0
loop until carry=0
templen=len(dec)
response.write dec
tempoddeven=int(formatnumber(int(templen/2),0))
if templen=(tempoddeven*2) then response.write &quot;Even&quot;
if templen<>(tempoddeven*2) then response.write &quot;Odd&quot;
 
Ok final one, dont think it can be any less :)

dim value,carry,templen,tempoddeven,dec
carry=45
do
tempint=int(formatnumber(int(carry/2),0))
if carry=(tempint*2) then dec=&quot;0&quot; & dec else dec=&quot;1&quot; & dec
carry=tempint
loop until carry=0
templen=len(dec)
tempoddeven=int(formatnumber(int(templen/2),0))
if templen=(tempoddeven*2) then response.write &quot;Even&quot; else response.write &quot;Odd&quot;
 
8 functional lines, very nice :)
-Tarwn &quot;The problem with a kludge is eventually you're going to have to back and do it right.&quot; - Programmers Saying (The Wiz Biz - Rick Cook)
&quot;Your a geek!&quot; - My Girlfriends saying
 
7 anyone?
* for the person that can do it in seven or less :) beats working ;)
-Tarwn &quot;The problem with a kludge is eventually you're going to have to back and do it right.&quot; - Programmers Saying (The Wiz Biz - Rick Cook)
&quot;Your a geek!&quot; - My Girlfriends saying
 
Does this count lol

carry=usernumber
do
tempint=int(formatnumber(int(carry/2),0))
if carry=(tempint*2) then dec=&quot;0&quot; & dec else dec=&quot;1&quot; & dec
carry=tempint
loop until carry=0
tempoddeven=int(formatnumber(int((len(dec))/2),0))
if templen=(tempoddeven*2) then response.write &quot;Even&quot; else response.write &quot;Odd&quot;
 
Here was my solution in 7 lines, I'll try to think up another challenge in the next couple days to keep us all from working to hard :)
Code:
Function ChallengeFunction(numStr)
	If ttl(cInt(numStr),32) mod 2 = 0 Then ChallengeFunction = &quot;even&quot; else ChallengeFunction = &quot;odd&quot;
End Function

Function ttl(num,pow)
	if pow < 0 then	
		ttl = 0
	elseif fix(num/pow2(pow)) > 0 Then 
		ttl = 1 + ttl(num-pow2(pow),pow - 1)
	else ttl = ttl(num,pow - 1)	end if
End Function

Function pow2(num)
	If num > 0 Then pow2 = 2 * pow2(num - 1) else pow2 = 1
End Function
This will handle a number up to 4294967296 (10^32) so I figure it aught to be a decent solution :) * Gary for 8 lines.
-Tarwn &quot;The problem with a kludge is eventually you're going to have to back and do it right.&quot; - Programmers Saying (The Wiz Biz - Rick Cook)
&quot;Your a geek!&quot; - My Girlfriends saying
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top