The Challenge

Given an integer, calculate its score as described below.

The score is calculated by raising each digit of the input Integer to the power n, where n denotes the index of the digit in Input (beginning with 1) when counted from left to right (the index of leftmost digit should be 1), and then summing them all together. If the original number happens to be prime, then add the largest digit of the input integer to the score, else subtract the smallest digit of the input integer from it.


Your program should take an integer as input. You can take the input in whatever way you want to.


Output the score of the input integer through whatever medium your programming language supports. Function return is also allowed.

Test Cases

 5328   ->  (5^1)+(3^2)+(2^3)+(8^4)-2 = 4116
 3067   ->  (3^1)+(0^2)+(6^3)+(7^4)+7 = 2627
 7      ->  (7^1)+7                   = 14


This is , so the shortest code in bytes wins!

  • \$\begingroup\$ Hello and welcome to the site! In order for questions to be considered on-topic here it needs an "objective winning criterion" that means that there needs to be some goal everyone is attempting to attain with there answers. The most common one is code-golf which means answers should aim to minimize their source length. \$\endgroup\$ – Wheat Wizard Mar 24 '17 at 2:44
  • 3
    \$\begingroup\$ Aside from being off topic because there is no winning criterion (a formality, as tagging it with, e.g., code-golf would solve that), there are a few issues. 1. Where do we get 5^1 + 3^2 + 2^3 + 8^4 from? That doesn't seem to be explained anywhere. 2. Strings are forbidden, but what about arrays? 3. If we write a program, how are we supposed to take input if we're not allowed to use strings? \$\endgroup\$ – Dennis Mar 24 '17 at 2:46
  • 3
    \$\begingroup\$ There is a Sandbox where you can post challenge ideas and get feedback from the community without it affecting your site reputation. It's generally a good idea to leave it in there for at least a week \$\endgroup\$ – MercyBeaucou Mar 24 '17 at 2:56
  • \$\begingroup\$ To be clear, you mean the largest/smallest digit numerically, not the rightmost/leftmost digit, right? \$\endgroup\$ – Greg Martin Mar 28 '17 at 17:06
  • \$\begingroup\$ This challenge was made code golf so it could be reopened. That means the shortest answer should be accepted. \$\endgroup\$ – Dennis Mar 29 '17 at 17:07

10 Answers 10


Ohm, 17 16 bytes (CP437)

EDIT: Fixed so that it checks the input's primality.


One of these days I need to get around to implementing vectorization. Maybe then I'll finally beat 05AB1E!


▓_^≥ⁿ;Σ┼Dp?╧+¿╤-    Main wire: arguments: n

▓    ;              Map the following over all digits of n:
 _^≥ⁿ                 Raise the digit to the (index + 1)th power
      Σ             Push the sum of all that
       ┼Dp          Is n prime?
          ?         If so...
           ╧+         Add the maximum digit of n to the sum
             ¿      Else...
              ╤-      Subtract the minimum digit of n from the sum
                    (Implicit output)
  • \$\begingroup\$ Unfortunately, you should be checking if the original input is prime, not the sum. Also I have no idea how to run this D: \$\endgroup\$ – Defacto Mar 29 '17 at 16:43
  • \$\begingroup\$ @Someguy That's not what the challenge says...? \$\endgroup\$ – Nick Clifford Mar 29 '17 at 16:44
  • \$\begingroup\$ Also, this is a fairly new language, so there's not a TIO link. You'll have to go to the page linked in the header and download the interpreter. \$\endgroup\$ – Nick Clifford Mar 29 '17 at 16:45
  • \$\begingroup\$ just found out that a moderator edited my question and changed it. I'll switch it back, that was not your fault. \$\endgroup\$ – Defacto Mar 29 '17 at 16:56
  • 1
    \$\begingroup\$ Congratulations, you tied for first place! \$\endgroup\$ – Defacto Apr 2 '17 at 20:25

Mathematica, 63 bytes


Pure function taking a positive integer as input and returning an integer. Directly performs the indicated computation, with all the long Mathematica command names that requires. Perhaps the one innovation is noticing that, if d is the list of digits of the input, then the last addition/subtraction is the same as adding Max[d] if the initial sum was prime, and adding Max[-d] if not.


Mathematica, 68 bytes



enter image description here

Sets x equal to the list of digits of the input, then raises that to the power Range[Tr[1^x]], effectively raising the first digit to the power 1, the second digit to the power 2, and so on up to Tr[1^x], the length of x. Tr sums the resulting list, which is then passed into the function #+If[PrimeQ@#,Max@x,-Min@x]&.

  • \$\begingroup\$ Nicely done, congratulations! \$\endgroup\$ – Defacto Mar 29 '17 at 16:55

05AB1E, 16 bytes


Try it online!


D                  # duplicate input
 piZ               # if input is prime then max digit of input
    ëW(            # else -min digit of input
       }           # end if
        ¹gL        # range [1 ... len(input)]
           ¹S      # input split into digits
             m     # pow
              O    # sum
               +   # add
  • \$\begingroup\$ Dg needs simplified with a desperate intensity. I wish there were a get(a) push length. \$\endgroup\$ – Magic Octopus Urn Mar 28 '17 at 20:09
  • \$\begingroup\$ @carusocomputing: True. That's a function that actually often needs a get. \$\endgroup\$ – Emigna Mar 28 '17 at 20:22
  • \$\begingroup\$ I don't see a good byte for it though, unfortunately, that won't break existing code and is 1 byte. \$\endgroup\$ – Magic Octopus Urn Mar 28 '17 at 20:26
  • \$\begingroup\$ @carusocomputing: Yeah, there aren't any useless 1-byte commands left. \$\endgroup\$ – Emigna Mar 28 '17 at 20:31
  • 1
    \$\begingroup\$ @Someguy Well, I was the culprit. I wrongly interpreted the test cases, so my edit actually went against your original intention. And none of Suggested Edit Reviewer figured out the mistake! (Don't be angry; as I am the one who got this reopened and I am one who got you eleven up votes; besides, when the question was reopened, I was the one who posted in the chat room, The Nineteenth Byte, that this question, having been reopened, needed to be out of the abyss it was in at that time (it had 4 down votes). So, just don't be angry! :) I hope you are not :). Thanks for reading such long comment!! \$\endgroup\$ – Arjun Mar 30 '17 at 2:30

Python 3: 260 167 bytes

def s(n):
 for i,d in enumerate(l):t+=int(d)**(int(i)+1)
  for i in range(2,n-1):
   if n%i==0:t-=int(min(l))
 return t

93 bytes saved by Wheat Wizard

  • \$\begingroup\$ This is what I use: https://mothereff.in/byte-counter. I recommend removing some whitespace to save bytes \$\endgroup\$ – math junkie Mar 28 '17 at 19:02
  • \$\begingroup\$ Hello and welcome to the site! I see a couple of improvements that you can make here. 1) When double indenting use spaces for one level of indentation and tabs for 2 this will save you bytes on indentation. 2) Your first for loop can be put on one list by simply removing the whitespace between the : and the t. 3) you can move the two breaks outside of the if/else statement and combine them into a single break to save bytes. Once that is done you can put each part of the if/else onto its own line. \$\endgroup\$ – Wheat Wizard Mar 28 '17 at 19:06
  • \$\begingroup\$ Here is a implementation of my suggestions. \$\endgroup\$ – Wheat Wizard Mar 28 '17 at 19:09

JavaScript, 173 bytes

Add an f= at the beginning and invoke like f(n).

n=>{k=(n+"").split``;t=k.map((c,i)=>c**(i+1)).reduce((c,p)=>c+p);j=1;for(i=2;i<n;i++){j=n%i||n==2?1:0;if(!j)break}return j?t+Math.max.apply(Math,k):t-Math.min.apply(Math,k)}


Takes in an Integer. Converts it into String. Then, converts it into Array. Uses map over the Array to make a new array having all its elements raised to their index+1th (+1 because JavaScript used zero-based indexes). Then uses reduce to sum them all together. Checks whether the obtained number is prime. If it is then it returns the obtained number incremented by the largest digit of the Array otherwise returns the obtained number decremented by smallest digit of it.

Test Cases

f=n=>{k=(n+"").split``;t=k.map((c,i)=>c**(i+1)).reduce((c,p)=>c+p);j=1;for(i=2;i<n;i++){j=n%i||n==2?1:0;if(!j)break}return j?t+Math.max.apply(Math,k):t-Math.min.apply(Math,k)}


  • \$\begingroup\$ For an input of 3067, your program should return 2627. It appears that your program is not adding the largest number given a prime input! For that reason, I can not accept your solution. \$\endgroup\$ – Defacto Mar 29 '17 at 16:47
  • \$\begingroup\$ @Someguy The code was made to add the largest number only when the obtained number happened to be a prime. It's just the recent edit that you made that made my solution go wrong for the test cases. Nevertheless, I have edited the solution to reflect the edition of the question. \$\endgroup\$ – Arjun Mar 30 '17 at 2:12

Ruby, 98 bytes

->n{i= ~s=0
s>1&&(2...s).all?{|i|s%i>0}?s+a.max: s-a.min}
  • 1
    \$\begingroup\$ I've never coded in Ruby but can't you remove the whitespace in a.max: s-a.min? \$\endgroup\$ – caird coinheringaahing Mar 28 '17 at 20:00
  • \$\begingroup\$ Nope. :foo is a Symbol literal. \$\endgroup\$ – m-chrzan Mar 28 '17 at 20:02
  • 1
    \$\begingroup\$ It's been a while since I golfed in Ruby, but I think every instance of each can be replaced with map unless you're making use of the unchanged return value. \$\endgroup\$ – Martin Ender Mar 28 '17 at 20:10
  • \$\begingroup\$ You're right. I was using the slightly different behavior of each while golfing, realized I didn't need it, but forgot to switch back to map. \$\endgroup\$ – m-chrzan Mar 28 '17 at 20:14

PHP, 174 bytes

function s($i){$i=str_split($i);foreach($i as $k=>$x){$r+=pow($x,$k+1);}if(p($r)){$r+=max($i);}else{$r-=min($i);}print$r;}function p($n){for($i=$n;--$i&&$n%$i;);return$i==1;}

Try it online!


Python 3, 118 bytes

x=sum(n**-~i for i,n in enumerate(a))
print(x+(-min(a),max(a))[all(x%z for z in range(2,x))*x>1])

Try it online!

Thanks to @math_junkie for pointing out a bug.

  • \$\begingroup\$ This fails for input 1. It should output 0, not 2 \$\endgroup\$ – math junkie Mar 28 '17 at 19:04

Python 2, 111 bytes

g=lambda n,z=1:n and(n%10)**len(`n`)+g(n/10,0)+int(['-'+min(`n`),max(`n`)][all(n%x for x in range(2,n))*n>1])*z

Try it Online!

Recursive function which calculates the sum starting with the last digit.

n and(n%10)**len(`n`)+g(n/10,1) is the recursive portion of the function

int(['-'+min(`n`),max(`n`)][...])*z returns 0 for all iterations except the first

all(n%x for x in range(2,n))*n>1] is the primality test

Alternate solution (115 bytes):

lambda n:sum(int(j)**(i+1)for i,j in enumerate(`n`))+int(['-'+min(`n`),max(`n`)][all(n%x for x in range(2,n))*n>1])

Try it Online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.