How much does this Integer score?

7

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!

Defacto

Posted 2017-03-24T02:34:59.210

Reputation: 545

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 [tag:code-golf] which means answers should aim to minimize their source length. – Post Rock Garf Hunter – 2017-03-24T02:44:47.967

3Aside 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? – Dennis – 2017-03-24T02:46:47.893

3

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

– MildlyMilquetoast – 2017-03-24T02:56:09.557

To be clear, you mean the largest/smallest digit numerically, not the rightmost/leftmost digit, right? – Greg Martin – 2017-03-28T17:06:20.940

This challenge was made code golf so it could be reopened. That means the shortest answer should be accepted. – Dennis – 2017-03-29T17:07:03.313

Answers

1

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)

Nick Clifford

Posted 2017-03-24T02:34:59.210

Reputation: 1 184

Unfortunately, you should be checking if the original input is prime, not the sum. Also I have no idea how to run this D: – Defacto – 2017-03-29T16:43:23.077

@Someguy That's not what the challenge says...? – Nick Clifford – 2017-03-29T16:44:44.997

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. – Nick Clifford – 2017-03-29T16:45:14.477

just found out that a moderator edited my question and changed it. I'll switch it back, that was not your fault. – Defacto – 2017-03-29T16:56:39.350

@Someguy Alright, fixed! – Nick Clifford – 2017-03-29T17:02:56.810

1Congratulations, you tied for first place! – Defacto – 2017-04-02T20:25:38.580

3

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.

Greg Martin

Posted 2017-03-24T02:34:59.210

Reputation: 13 940

3

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
               +   # add

Emigna

Posted 2017-03-24T02:34:59.210

Reputation: 50 798

Dg needs simplified with a desperate intensity. I wish there were a get(a) push length. – Magic Octopus Urn – 2017-03-28T20:09:24.507

@carusocomputing: True. That's a function that actually often needs a get. – Emigna – 2017-03-28T20:22:30.033

I don't see a good byte for it though, unfortunately, that won't break existing code and is 1 byte. – Magic Octopus Urn – 2017-03-28T20:26:16.713

@carusocomputing: Yeah, there aren't any useless 1-byte commands left. – Emigna – 2017-03-28T20:31:08.233

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. – Defacto – 2017-03-29T16:39:49.703

@Someguy: For the input 3067, the sum is 2620 which is definitely not prime, so it should subtract the smallest number 0 leading to the result 2620. – Emigna – 2017-03-29T16:55:33.677

@Emigna 3067 is a prime number. If you can edit your solution to fix this problem (it shouldn't be too hard) then I will declare you the victor! – Defacto – 2017-03-29T16:59:43.947

@Someguy: You just edited the question though. It said If the number achieved by following the this procedure over the input integer 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. – Emigna – 2017-03-29T17:00:53.927

I will actually save a byte changing the code to the new spec though. – Emigna – 2017-03-29T17:01:29.503

That was not the original specification, for some reason a moderator came in and changed my entire problem lol. It's not your fault. But if you post a new solution I'll give you the win. – Defacto – 2017-03-29T17:02:14.130

@Someguy: That explains it. It's been changed to follow the new/original spec :) – Emigna – 2017-03-29T17:05:41.737

@Emigna nicely done, congratulations! – Defacto – 2017-03-29T17:10:17.640

1@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!! – Arjun – 2017-03-30T02:30:22.727

@Arjun I am not mad at you at all! In fact, I'm glad you got this reopened. The mistake you made was nominal and it's really not a big deal. – Defacto – 2017-04-02T20:22:49.557

@Arjun And you should be a moderator! – Defacto – 2017-04-02T20:23:26.377

3

Mathematica, 68 bytes

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

Explanation

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

ngenisis

Posted 2017-03-24T02:34:59.210

Reputation: 4 600

Nicely done, congratulations! – Defacto – 2017-03-29T16:55:12.600

2

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

Arjun

Posted 2017-03-24T02:34:59.210

Reputation: 4 544

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. – Defacto – 2017-03-29T16:47:06.360

@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. – Arjun – 2017-03-30T02:12:44.583

2

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

bendl

Posted 2017-03-24T02:34:59.210

Reputation: 233

This is what I use: https://mothereff.in/byte-counter. I recommend removing some whitespace to save bytes

– math junkie – 2017-03-28T19:02:48.360

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. – Post Rock Garf Hunter – 2017-03-28T19:06:03.727

Here is a implementation of my suggestions. – Post Rock Garf Hunter – 2017-03-28T19:09:37.530

1

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}

m-chrzan

Posted 2017-03-24T02:34:59.210

Reputation: 1 390

1I've never coded in Ruby but can't you remove the whitespace in a.max: s-a.min? – caird coinheringaahing – 2017-03-28T20:00:07.810

Nope. :foo is a Symbol literal. – m-chrzan – 2017-03-28T20:02:14.240

1It'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. – Martin Ender – 2017-03-28T20:10:43.000

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. – m-chrzan – 2017-03-28T20:14:28.667

1

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!

Tom291

Posted 2017-03-24T02:34:59.210

Reputation: 313

0

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!

math junkie

Posted 2017-03-24T02:34:59.210

Reputation: 2 490

0

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.

Trelzevir

Posted 2017-03-24T02:34:59.210

Reputation: 987

This fails for input 1. It should output 0, not 2 – math junkie – 2017-03-28T19:04:56.867