Results 1 to 13 of 13

Thread: Reducing numbers to one digit (numerology related)

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2013
    Posts
    5

    Reducing numbers to one digit (numerology related)

    If you don't know what numerology is, or how it works just forget it and see if you can solve this problem.

    I want to reduce a multiple digit number to one digit by adding each digit together, then again, and again etc...until there is only one digit.

    Examples of reduction method:
    9999 (9+9+9+9=36 then 3+6=9 [corrected])
    8765 (8+7+6+5=26 then 2+6=8)
    4444 (4+4+4+4=32 then 3+2=5)
    3210 (3+2+1+0=6)

    Also, I need to convert letters into numbers in a way that the numbers 1-9 correspond with A-I, J-R, and S-Z (z ending on 8 of course). And I'm hoping there's a short way to program in VB other than A=1 B=2 so on....
    Any help is appreciated thanks!
    Last edited by nlw92; Nov 25th, 2013 at 07:01 AM. Reason: 9*4 is 36 not 37.

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,905

    Re: Reducing numbers to one digit (numerology related)

    Which programming language are you using are do you want to use?
    And what is your question?

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2013
    Posts
    5

    Re: Reducing numbers to one digit (numerology related)

    Quote Originally Posted by Arnoutdv View Post
    Which programming language are you using are do you want to use?
    And what is your question?
    I guess since we are on vbforums.com I assumed it was implied that I'm using visual basic language. And I didn't post in question form, but I need to figure out how to reduce numbers in visual basic using the reduction method in original post. That's the only way I can think to describe it.

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,905

    Re: Reducing numbers to one digit (numerology related)

    Sorry, which version of VB?
    VB.Net or VB5/6 (classic)

    Have you tried something yourself yet?

  5. #5
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Reducing numbers to one digit (numerology related)

    First: your own examples are a bit flawed .

    To calulate a sum of digits in a number, you can try something like:
    vb.net Code:
    1. Private Function digitSum(ByVal val As Integer) As Integer
    2.         If val < 0 Then Throw New ArgumentException("Parameter must be positive.", "val")
    3.  
    4.         Dim sval As String = val.ToString()
    5.         Dim sum As Integer
    6.  
    7.         Do
    8.             sum = sval.Sum(Function(c) Integer.Parse(c))
    9.             sval = sum.ToString()
    10.         Loop Until sval.Length = 1
    11.  
    12.         Return sum
    13.     End Function

    and to convert strings of letters into strings of digits by the specified rules, you can try:
    vb.net Code:
    1. Private Function charToDigit(ByVal c As Char) As Char
    2.         'Converts a character into corresponding number by the rules:
    3.         ''A'-'J', 'a'-'j', 'K'-'T' and 'k'-'t' -> '0'-'9'
    4.         ''U'-'Z' and 'u'-'z' -> '0'-'5'.
    5.         '--
    6.         'This is ofc only meaningful for characters 'A' through 'Z' and 'a' thorugh 'z'.
    7.         'Any other characters may produce unexpected results.
    8.         Return Chr((Asc(Char.ToUpper(c)) - 65) Mod 10 + 48)
    9.     End Function
    10.  
    11.     Private Function letterStringToDigitString(ByVal text As String) As String
    12.         Return String.Concat(text.Select(Function(c) charToDigit(c)))
    13.     End Function

    The functions can be tested with:
    vb.net Code:
    1. MessageBox.Show(digitSum(9999).ToString) 'Displays 9
    2.         MessageBox.Show(digitSum(8765).ToString) 'Displays 8
    3.         MessageBox.Show(digitSum(4444).ToString) 'Displays 7
    4.         MessageBox.Show(digitSum(3210).ToString) 'Displays 6
    5.         '---
    6.         MessageBox.Show(letterStringToDigitString("Reducing"))   'Displays 74302836
    7.         MessageBox.Show(letterStringToDigitString("numbers"))    'Displays 3021478
    8.         MessageBox.Show(letterStringToDigitString("ToOneDigit")) 'Displays 9443438689
    9.         '---
    10.         MessageBox.Show(digitSum(Integer.Parse(letterStringToDigitString("Thomas"))).ToString) 'Displays 3

    Regards
    Tom

    #EDIT: Oh just reread the op - to use digits '1' through '9' (as you prefer) instead of '0' through '9' (as in my example), you can just use Mod 9 and add 49. Thus changing the function charToDigit to Return Chr((Asc(Char.ToUpper(c)) - 65) Mod 9 + 49).
    Last edited by ThomasJohnsen; Nov 25th, 2013 at 06:56 AM. Reason: Flaws
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2013
    Posts
    5

    Re: Reducing numbers to one digit (numerology related)

    I just installed visual basic for the first time on a new computer, so the newest free version is the one I'm currently using. I haven't started the code yet but here's a summary of the program: user types first, middle, and last names into textboxes 1, 2, and 3. Then using 3 drop boxes chooses the month, day, and year of birth. User clicks "submit". The program then takes textboxes and converts text into number ("new" becomes 555, "fox" becomes 666 etc) then uses reduction method above (555, becomes 5+5+5 and 666 becomes 6+6+6). The program after reducing to a single digit gives the user 3 "magic number" that correspond with their first middle and last names.

  7. #7
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Reducing numbers to one digit (numerology related)

    Quote Originally Posted by nlw92 View Post
    I just installed visual basic for the first time on a new computer, so the newest free version is the one I'm currently using. I haven't started the code yet but here's a summary of the program: user types first, middle, and last names into textboxes 1, 2, and 3. Then using 3 drop boxes chooses the month, day, and year of birth. User clicks "submit". The program then takes textboxes and converts text into number ("new" becomes 555, "fox" becomes 666 etc) then uses reduction method above (555, becomes 5+5+5 and 666 becomes 6+6+6). The program after reducing to a single digit gives the user 3 "magic number" that correspond with their first middle and last names.
    So you want a 'magic' number for first, middle and last name? Or will there be 3 'semi-magic' number for first, middle and last names that will be combined into a 'super-magic' number? And when is the date used and for what?
    If you list a bit of your code, that shows names of textboxes and comboboxes (text, value here probably unless you use a datetimepicker or similar), I'll modify my example to accomodate your code.

    Regards
    Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2013
    Posts
    5

    Re: Reducing numbers to one digit (numerology related)

    Nice! Exaclty the response (although i didnt know it until now) I was hoping for! If you've got some extra time and wouldn't mind writing a short novel to describe what each line of code means that would greatly increase my learning curve and be much appreciated. I'm new to all the abbreviations and stuff except for the most basic ones such as dim, string, and integer.

    Tom, you can disregard the post I made after your coded post. I'm on mobile and didn't see your codes until after I typed my 'magic number' comment.

  9. #9
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Reducing numbers to one digit (numerology related)

    Quote Originally Posted by nlw92 View Post
    Nice! Exaclty the response (although i didnt know it until now) I was hoping for! If you've got some extra time and wouldn't mind writing a short novel to describe what each line of code means that would greatly increase my learning curve and be much appreciated. I'm new to all the abbreviations and stuff except for the most basic ones such as dim, string, and integer.

    Tom, you can disregard the post I made after your coded post. I'm on mobile and didn't see your codes until after I typed my 'magic number' comment.
    My code isn't all that complicated except maybe for the charToDigit function. If you make a sort of template program (ie draw a form with the controls you need and name them according to your preferences), I will make a short example that displays 'magic' numbers for first, middle, last names, dates or combinations thereof. I will do my best to elaborate on the code. Are you starting out completely from scratch into the world of programming?

    Regards Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  10. #10
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Reducing numbers to one digit (numerology related)

    For what it's worth, this thread seems to have no actual math questions--it's all about coding basic operations on digits and letters. A more appropriate place for this type of post is probably the VB.NET forum. More people frequent that one anyway.

    That said, the operation you described in your original post is the "digital root". There's a lengthy Wikipedia page which lists a short-cut formula, dr(n) = n - 9*floor((n-1)/9). You should be able to just use n - 9*((n-1)\9) in VB.NET; the \ means integer division. (BTW, your 4444 example is still wrong.)
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  11. #11

    Thread Starter
    New Member
    Join Date
    Nov 2013
    Posts
    5

    Re: Reducing numbers to one digit (numerology related)

    Quote Originally Posted by jemidiah View Post
    For what it's worth, this thread seems to have no actual math questions--it's all about coding basic operations on digits and letters. A more appropriate place for this type of post is probably the VB.NET forum. More people frequent that one anyway.

    That said, the operation you described in your original post is the "digital root". There's a lengthy Wikipedia page which lists a short-cut formula, dr(n) = n - 9*floor((n-1)/9). You should be able to just use n - 9*((n-1)\9) in VB.NET; the \ means integer division. (BTW, your 4444 example is still wrong.)
    Haha I'm not even going to edit it. Sorry about posting in the wrong area, it seemed like a math question at the time. And I have no clue why I thought 4 + 4 was 16..
    Now that you mention it I do remember seeing that digital root formula on a YouTube video or something. Thanks!

  12. #12
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Reducing numbers to one digit (numerology related)

    The naive way of doing it would be to cast the full numeric value to a string and parse from it adding each digit, and continuing the process until you're left with a single digit. I would use math instead:

    csharp Code:
    1. static int SumDigits(int i)
    2. {
    3.     int sum = 0, n;
    4.     do
    5.     {
    6.         n = i;
    7.         i /= 10;
    8.         sum += n - i * 10;
    9.     } while (i > 0);
    10.     return sum;
    11. }
    12.  
    13. public static void MyMethod()
    14. {
    15.     int n = 4444;
    16.     while ((int) (Math.Log(n, 10) + 1) > 1) n = SumDigits(n);
    17.     Console.WriteLine("Number: {0}", n);
    18. }

    *Forgot this was the VB.NET section..

    VB.NET version:
    vbnet Code:
    1. Private Function SumDigits(i As Integer) As Integer
    2.     Dim sum As Integer = 0, n As Integer
    3.     While i > 0
    4.         n = i
    5.         i \= 10
    6.         sum += n - (i * 10)
    7.     End While
    8.     Return sum
    9. End Function
    10.  
    11. Sub Main()
    12.     Dim n As Integer = 4444
    13.     Do
    14.         n = SumDigits(n)
    15.     Loop While Math.Log(n, 10) > 1
    16.     Console.WriteLine("Number: {0}", n)
    17.     Console.ReadKey()
    18. End Sub

    I was not aware of the Digital Root expansion formula however.
    Last edited by AceInfinity; Nov 28th, 2013 at 07:25 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  13. #13
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Reducing numbers to one digit (numerology related)

    In that case:
    vbnet Code:
    1. Private Function DigitalRoot(s As String) As Integer
    2.     Return DigitalRoot(s.ToUpper().Sum(Function(c) Asc(c) - Asc("@"c)))
    3. End Function
    4.  
    5. Private Function DigitalRoot(n As Integer) As Integer
    6.     Return n - 9 * ((n - 1) \ 9)
    7. End Function

    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

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