All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Algorithms
Year Yearn 5 (Posted on 2023-06-28) Difficulty: 3 of 5
Define sod(x) as the sum of the digits of x.
For example, sod(1245984)= 1+2+4+5+9+8+4= 33.

Devise an algorithm to determine the value of:
sod(20232023).

See The Solution Submitted by K Sengupta    
Rating: 5.0000 (1 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
Solution Solution | Comment 2 of 4 |
Method Three:  just run sod(2023**2023) on immediate window of Spyder Python:   30112
I initially skipped over this obvious way because I didn't think it would work.

Method One:
Checking sod(n^n) for the first several integers (OEIS A066588 btw) fails to show an obvious pattern.  So this did not work out.

Method Two:
I already had a library of several functions used to multiply large integers by converting to a list of single digits and doing arithmetic operations on lists.  I only had to write an exponentiation function.
So the program is a single line of code (plus a gazillion function calls)

Program:  print(sum(large_exponentiate(2023,2023)))
Output:  30112

Program:  len(large_exponentiate(2023,2023))
Output: 6689

---------   various functions used  -----------
----------- none of which was necessary -----------
def sod(n):
    """ Input an integer.  Returns the Sum of the Digits  """
    aList = list(str(n))
    ans = 0
    for c in aList:
        ans = ans + int(c)
    return ans

def un_bloat(l):  # l is the lowercase letter L
    """ l is a list of integers some of which are more than one digit
    need to be just single digit entries in the list
    so this function moves all the carries to the left
    outputting a list of single digit integers """
    rev_l = l[::-1]
    rev_l.append(0)
    for i, val in enumerate(rev_l[:-1]):
        rev_l[i+1] += rev_l[i]//10
        rev_l[i] = rev_l[i] % 10
    size_of_last_entry = len(str(rev_l[-1]))
    while size_of_last_entry > 1:
        rev_l.append(0)
        rev_l[-1] += rev_l[-2]//10
        rev_l[-2] = rev_l[-2] % 10
        size_of_last_entry -= 1
    return  rev_l[::-1]

def type_parser(n):  # working
    """ input n which can be an integer, a string, a list of integers
    or a list of strings of integers; 
    returns a list of integers """
    if type(n) == int:
        ans = [int(x) for x in str(n)]
    if type(n) == str:
        ans = [int(x) for x in n]
    elif type(n) == list:
        if type(n[0]) == int:
            ans = n[:]
        elif type(n[0]) == str:
            ans = [int(x) for x in n] 

    return un_bloat(ans)

def digit_multiply(n,i):
    """ n is a list of integers and i is a single digit """
    n = type_parser(n)
    if i == 0:
        ans = [0 for x in n]
    elif i == 1:
        ans = n[:]
    else:
        bloat_ans =  [i*x for x in n]    # could do this for all cases
        return un_bloat(bloat_ans)
    return ans


def add2lists(a,b):
    """ a and b are lists of integers, output a list of integers
    representing their sum """
    if len(a) > len(b):
        ans = a[:]
        shorter = b[:]
    else:
        ans = b[:]
        shorter = a[:]
    diff = len(ans) - len(shorter)
    for i in range(diff):
        shorter.insert(0,0)
    for i,val in enumerate(shorter):
        ans[i] += val
    return un_bloat(ans)

def strip_left(aList):
    if aList[0] != 0:
        return aList
    else:
        del aList[0]
        return strip_left(aList)
    return

def large_multiply(n,m):
    top = type_parser(n)
    bot = type_parser(m)
    running_sum = []
    for i, val in enumerate(reversed(bot)):
        one_line = top[:]
        for repeat_number in range(i):
            one_line.append(0)
        running_sum = add2lists(running_sum,digit_multiply(one_line,val))

    return strip_left(un_bloat(running_sum))
        
def large_exponentiate(n,m):
    """ return n^m in list format where n is integer or list; m is integer  """
    listn = type_parser(n)
    ans = listn
    for i in range(m-1):
        ans = large_multiply(ans,listn)
    return ans

  Posted by Larry on 2023-06-28 09:51:44
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (3)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information