# 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.

# Input

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

# Output

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


# Scoring

This is , so the shortest code in bytes wins!

• 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. Commented Mar 24, 2017 at 2:44
• 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? Commented Mar 24, 2017 at 2:46
• 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 Commented Mar 24, 2017 at 2:56
• To be clear, you mean the largest/smallest digit numerically, not the rightmost/leftmost digit, right? Commented Mar 28, 2017 at 17:06
• This challenge was made code golf so it could be reopened. That means the shortest answer should be accepted. Commented Mar 29, 2017 at 17:07

# Mathematica, 63 bytes

s+Max@If[PrimeQ[s=Tr[d^Range@Length[d=IntegerDigits@#]]],d,-d]&


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

#+If[PrimeQ@#,Max@x,-Min@x]&@Tr[(x=IntegerDigits@#)^Range[Tr[1^x]]]&


## Explanation

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]&.

• Nicely done, congratulations! Commented Mar 29, 2017 at 16:55

# 05AB1E, 16 bytes

DpiZëW(}¹gL¹SmO+


Try it online!

Explanation

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

• Dg needs simplified with a desperate intensity. I wish there were a get(a) push length. Commented Mar 28, 2017 at 20:09
• @carusocomputing: True. That's a function that actually often needs a get. Commented Mar 28, 2017 at 20:22
• I don't see a good byte for it though, unfortunately, that won't break existing code and is 1 byte. Commented Mar 28, 2017 at 20:26
• @carusocomputing: Yeah, there aren't any useless 1-byte commands left. Commented Mar 28, 2017 at 20:31
• @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!! Commented Mar 30, 2017 at 2:30

# Python 3: 260 167 bytes

def s(n):
t,l=0,list(str(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))
else:t+=int(max(l))
break
return t


93 bytes saved by Wheat Wizard

• This is what I use: https://mothereff.in/byte-counter. I recommend removing some whitespace to save bytes Commented Mar 28, 2017 at 19:02
• 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. Commented Mar 28, 2017 at 19:06
• Here is a implementation of my suggestions. Commented Mar 28, 2017 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)}


Explanation

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)}

console.log(f(5328));
console.log(f(3067));
console.log(f(7));
console.log(f(123));
console.log(f(1729));
console.log(f(2));
console.log(f(1458));
console.log(f(91));

• 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. Commented Mar 29, 2017 at 16:47
• @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. Commented Mar 30, 2017 at 2:12

# Ohm, 17 16 bytes (CP437)

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

▓_^≥ⁿ;Σ┼Dp?╧+¿╤-


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

Explanation:

▓_^≥ⁿ;Σ┼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)

• Unfortunately, you should be checking if the original input is prime, not the sum. Also I have no idea how to run this D: Commented Mar 29, 2017 at 16:43
• @Someguy That's not what the challenge says...? Commented Mar 29, 2017 at 16:44
• 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. Commented Mar 29, 2017 at 16:45
• just found out that a moderator edited my question and changed it. I'll switch it back, that was not your fault. Commented Mar 29, 2017 at 16:56
• Congratulations, you tied for first place! Commented Apr 2, 2017 at 20:25

# Ruby, 98 bytes

->n{i= ~s=0
a=n.digits
a.map{|d|s+=d**(a.size-i+=1)}
s>1&&(2...s).all?{|i|s%i>0}?s+a.max: s-a.min}

• I've never coded in Ruby but can't you remove the whitespace in a.max: s-a.min? Commented Mar 28, 2017 at 20:00
• Nope. :foo is a Symbol literal. Commented Mar 28, 2017 at 20:02
• 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. Commented Mar 28, 2017 at 20:10
• 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. Commented Mar 28, 2017 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

*a,=map(int,input())
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.

• This fails for input 1. It should output 0, not 2 Commented Mar 28, 2017 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!