codewar problem-looking for alternative way to solve this problem-mathematical

Thread Starter

terabaaphoonmein

Joined Jul 19, 2020
90
https://www.codewars.com/kata/5552101f47fc5178b1000050/train/python



This is how I youtube'd and found a solution-:

Code:
n=89
p=1
my_sum=0
for num in str(n):
    my_sum=my_sum+(int(num))**p
    p=p+1
if(my_sum%n==0):
    print(int(my_sum/n))
else:
    print(-1)
What is another way to solve this problem? I prefer no code solutions with flowcharts/algorithms rather than writing codes so that I can try writing codes on my own. But I of course won't mind code with comments tbh.

I want a more mathematical way of solving this problem. like using that given equation in code and finding a solution...I know we are already using it...but I want sth different.
 

KeithWalker

Joined Jul 10, 2017
2,295
I assume that this is written in python. Although currently it is a very popular software language, a lot of the older members, including myself, evolved through 4-bit assembler, Cobol, Lisp, Basic, Fortran, Forth and a whole slew of other languages to C and C++ and have never needed to use Python.
If you can describe, line by line, what the program is doing, I will see if I can offer any alternate ways of getting the result.
 

click_here

Joined Sep 22, 2020
533
The brute force solution is fine, but it might be a good opportunity to make some reusable code.

I would make a function called "isFactor(a,b)" that returned a non-zero number if number "a" was a factor of "b"

When dealing with larger numbers you would usually get all the "prime factors" and see if the number can be made from the product of them, but this is overkill for the simple numbers here...

You can also make a function that returns the value of a specific digit in a decimal number "foo(1234,3)" returns 3...
 
Top