views:

243

answers:

3

I want a function to calculate numerology.For example if i enter "XYZ" then my output should be 3 .

Here is how it became 3:

X = 24
Y = 25
Z = 26

on adding it becomes 75 which again adds up to 12 (7+5) which again adds up to 3(1+2) . Similarly whatever names i should pass,my output should be a single digit score.

+1  A: 

I have no idea what this could possible be used for but it was fun to write anyway.

 Private Function CalcStupidNumber(ByVal s As String) As Integer
    s = s.ToLower
    If (s.Length = 1) Then 'End condition
        Try
            Return Integer.Parse(s)
        Catch ex As Exception
            Return 0
        End Try
    End If
    'cover to Values 
    Dim x As Int32
    Dim tot As Int32 = 0
    For x = 0 To s.Length - 1 Step 1
        Dim Val As Integer = ConvertToVal(s(x))
        tot += Val
    Next
    Return CalcStupidNumber(tot.ToString())
End Function

Private Function ConvertToVal(ByVal c As Char) As Integer
    If (Char.IsDigit(c)) Then
        Return Integer.Parse(c)
    End If

    Return System.Convert.ToInt32(c) - 96 ' offest of a 
End Function
rerun
but that's vb.net, not vb script
Rubens Farias
yea I didn't notice the vb script part
rerun
+2  A: 

In vbscript:

Function numerology(literal)

    result = 0
    for i = 1 to Len(literal)
        '' // for each letter, take its ASCII value and substract 64,
        '' so "A" becomes 1 and "Z" becomes 26
        result = result + Asc(Mid(literal, i, 1)) - 64
    next

    '' // while result is bigger than 10, let's sum it's digits
    while(result > 10)
        partial = 0
        for i = 1 to Len(CStr(result))
            partial = partial + CInt(Mid(CStr(result), i, 1))
        next
        result = partial
    wend

    numerology = result

End Function
Rubens Farias
FYI: the *// while result is bigger than 10* part is actually a digital root calculation (http://en.wikipedia.org/wiki/Digital_root), which can be done using a simple formula: `1 + (result - 1) Mod 9`. ;)
Helen
@Helen, lovely to know that! I saw your answer added that, so I'll keep this version as is, ok?
Rubens Farias
Hi Rubens FariasThank you so much for posting the answer i used this one. The program was very efficient. I just added literal=Ucase(literal) line for correct calculation. That's it. Thanks for the help
Deepa
+5  A: 

Here you are:

Function Numerology(Str)
  Dim sum, i, char

  ' Convert the string to upper case, so that 'X' = 'x'
  Str = UCase(Str)

  sum = 0
  ' For each character, ...
  For i = 1 To Len(Str)
    ' Check if it's a letter and raise an exception otherwise
    char = Mid(Str, i , 1)
    If char < "A" Or char > "Z" Then Err.Raise 5 ' Invalid procedure call or argument

    ' Add the letter's index number to the sum
    sum = sum + Asc(char) - 64
  Next

  ' Calculate the result using the digital root formula (http://en.wikipedia.org/wiki/Digital_root)
  Numerology = 1 + (sum - 1) Mod 9
End Function
Helen
Thank you so much for the answer Helen. It is a good answer. Thank you
Deepa