|
-
May 1st, 2009, 05:06 AM
#1
Thread Starter
New Member
squared digit length
am a vb.net beginner anyone please help me solve the following assignment so that i am able to know where to start from
The following process determines the squared digit length of an integer. Take
any integer and add up the squares of its digits. This will give you another
integer. Repeat this procedure until the number you end up with is 1 or 4.
The number of times this process has to be repeated before it gets to one or
4 is the squared digit length. For example, if we start with 48, we get:
4^2 + 8^2 = 80
8^2 + 0^2 = 64
6^2 + 4^2 = 52
5^2 + 2^2 = 29
2^2 + 9^2 = 85
8^2 + 5^2 = 89
8^2 + 9^2 = 145
1^2 + 4^2 + 5^2 = 42
4^2 + 2^2 = 20
2^2 + 0^2 = 4
1
This process shows that the squared digit length of 48 is 10.
Last edited by shalini22; May 1st, 2009 at 10:03 PM.
-
May 1st, 2009, 05:19 AM
#2
Re: squared digit length
Im confused by the very first line in your example - if you add up the squares of the digits in 48, I do get 80 like you do, but surely 4 squared is 16 and 8 squared is 64. How do you get 42 and 82?
And last I checked 42+82 = 124 not 80.
I think I understand the principle of what you are saying but your example doesn't make much sense - can you elaborate?
-
May 1st, 2009, 05:25 AM
#3
Re: squared digit length
OK - I understand now - you mean 4^2 and 8^2 not 42 and 82
So how far have you got - it sounds like an assignment for a course is it? I have written a routine to do it but I'd rather help you write your own routine than just give you an answer.
Last edited by keystone_paul; May 1st, 2009 at 05:37 AM.
-
May 1st, 2009, 08:02 AM
#4
Hyperactive Member
Re: squared digit length
yes keystone_paul is right we are not here to solve your assignment but we will be happy if you do something and ask about the errors
* If my post helped you, please Rate it
* If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
* Why Rating is useful
-
May 1st, 2009, 09:50 AM
#5
Re: squared digit length
Hints: A recursive function fits well for this.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
May 1st, 2009, 11:14 PM
#6
Re: squared digit length
Start with what you've learned so far. If you haven't learned anything then go back to the book.
-
May 4th, 2009, 01:52 AM
#7
Re: squared digit length
 Originally Posted by stanav
Hints: A recursive function fits well for this.
LINQ would do equally good too
-
May 4th, 2009, 02:01 AM
#8
Re: squared digit length
 Originally Posted by stanav
Hints: A recursive function fits well for this.
I'd be inclined to do this iteratively. I don't really see the advantage in a method calling itself here. I'd go with a Do Until loop.
-
May 4th, 2009, 02:21 AM
#9
Re: squared digit length
I'd be inclined to do this iteratively
Me too - it took 10 lines of code without recursion, LINQ or anything complicated.
-
May 4th, 2009, 02:34 AM
#10
Re: squared digit length
 Originally Posted by keystone_paul
Me too - it took 10 lines of code without recursion, LINQ or anything complicated.
It took me just 4 lines combined with a LINQ query. But maybe someone would be optimize it better and get a pure LINQ solution.
-
May 5th, 2009, 08:44 AM
#11
Re: squared digit length
Since the OP doesn't seem too much interested, lets see what each one of us have been upto. 
This is what I was doing:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox(GetSquaredDigitLength(48)) End Sub Function GetSquaredDigitLength(ByVal num As Double) As Integer Dim count As Integer Do While num <> 1 AndAlso num <> 4 count += 1 num = (From c In num.ToString Select Integer.Parse(c) ^ 2).Sum Loop Return count End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|