Results 1 to 11 of 11

Thread: squared digit length

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    1

    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.

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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?

  3. #3
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  4. #4
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    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

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  6. #6
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: squared digit length

    Start with what you've learned so far. If you haven't learned anything then go back to the book.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: squared digit length

    Quote Originally Posted by stanav View Post
    Hints: A recursive function fits well for this.
    LINQ would do equally good too
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: squared digit length

    Quote Originally Posted by stanav View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: squared digit length

    Quote Originally Posted by keystone_paul View Post
    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     MsgBox(GetSquaredDigitLength(48))
    3. End Sub
    4.  
    5. Function GetSquaredDigitLength(ByVal num As Double) As Integer
    6.     Dim count As Integer
    7.     Do While num <> 1 AndAlso num <> 4
    8.         count += 1
    9.         num = (From c In num.ToString Select Integer.Parse(c) ^ 2).Sum
    10.     Loop
    11.     Return count
    12. End Function
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width