g | x | w | all
Bytes Lang Time Link
050Juby250507T001010ZJordan
00705AB1E legacy210504T131733ZKevin Cr
045R210412T144244ZRobin Ry
035JavaScript ES7210412T145619ZArnauld
008Husk210412T151629ZDominic
005Vyxal210413T003658Zlyxal
040Haskell210412T145440ZDelfad0r
013Charcoal210412T202416ZNeil
015Retina 0.8.2210412T195736ZNeil
037Python 2210412T191916Zxnor
039Wolfram Language Mathematica210412T181211Zatt
007Jelly210412T142002Zcaird co
045R210412T150106ZDominic
048Python 2210412T152934Zhyperneu
01105AB1E210412T151256Zcaird co
039C gcc210412T145614Zl4m2
023Husk210412T145405ZRazetime

J-uby, 50 bytes

:digits|:flat_map+(~:digits&5)|:sum+(~:/&2r|:ceil)

Attempt This Online!

05AB1E (legacy), 7 bytes

5δв˜;îO

Port of @Dominic van Essen's Husk answer, so make sure to upvote him as well!

Uses the legacy version of 05AB1E to get rid of a leading S that would be required in the new version of 05AB1E in this case, due to the way the δ acts on strings/integers.

Try it online or verify all test cases.

Explanation:

 δ       # Map each digit in the (implicit) input-integer to:
5 в      #  A base-5 list
   ˜     # Flatten this list of lists
    ;    # Halve each integer
     î   # Ceil each float
      O  # Sum the integers in the list together
         # (after which the result is output implicitly)

R, 64 52 50 45 bytes

sum(c(1,2,1:3)[.6*utf8ToInt(scan(,""))-28.4])

Try it online!

Same strategy as Delfad0r's Haskell answer, which is nicely explained.

First, scan(,"") reads in input as a string. Then, utf8ToInt(...)-48 takes a string of digits and converts it to a vector of integer digits. This works out shorter than taking input as an integer and splitting it into digits.

The digits from 0 to 9 then have to be mapped to the vector c(0,1,1,2,2,1,2,2,3,3). To do this, consider the vector a=c(1,2,1,2,3). For digit d, a[.4+d*.6] is the value we want. This uses the fact that when calling a non-integer index in a vector (say a[2.8]), R rounds down the index (here a[2]), and also the fact that a[0] returns NULL which will be ignored in the sum.

Putting this operation with the operation to convert the string to digits simplifies to .6*utf8ToInt(...)-28.4.

Note that Dominic van Essen has a solution of (currently) the same length with a different strategy.

JavaScript (ES7),  36  35 bytes

Similar to other answers. Using a Black Magic formula instead of a lookup table.

f=n=>n&&(n%10)**29%3571%4+f(n/10|0)

Try it online!

Here is a script that looks for \$(p,m)\$ pairs such that \$(n^p\bmod m)\bmod 4=a_n\$ for all \$n\in[0..9]\$.

It's worth noting that this code takes IEEE-754 precision errors into account. So the results are correct but the math is wrong.

With exact values, we could do instead:

$$\big((n \bmod 10)^{18}\bmod 4011\big)\bmod 4$$

Or better yet, as suggested by @dingledooper:

$$\big((n \bmod 10)^{55}\bmod 767\big)\bmod 4$$

For instance, in Python:

Python 2, 40 bytes

f=lambda n:n and(n%10)**55%767%4+f(n/10)

Try it online!


JavaScript (ES7), 71 bytes

Expects the amount as a string.

A naive recursive approach that subtracts one coin/bill at a time.

n=>(k=10**n.length,g=t=>+n?k>n?g(t,k/=~++i%3?2:2.5):g(t+1,n-=k):t)(i=0)

Try it online!

Husk, 11 9 8 bytes

Edit: -1 byte thanks to caird coinheringaahing

ṁo⌈½ṁB5d

Try it online!

       d   # get the digits
    ṁB5    # convert them all to base-5
           # (this gives a 1 for each 5-denomination coin needed,
           # as well as the leftover for each digit.
           # We'll need 2 more coins for those with leftover 3 or 4, 
           # and only one more coin if the leftover is 1 or 2.)
ṁo         # So: map across all the base-5 digits
   ½       # dividing each of them by 2
  ⌈        # and then getting the ceiling;
           # and finally output the sum

Vyxal, d, 5 bytes

5vτ½⌈

Try it Online!

A port of the Jelly answer which is a port of short husk answer.

Explained

5vτ½⌈
5vτ   # convert each digit of the input to base 5
   ½  # halve each item in that list (halving vectorises all the way down)
    ⌈ # ceiling each item in that list
      # -d deep sums the list and implicitly outputs

Haskell, 5541 40 bytes

a=0:tail[i+read[j]|i<-a,j<-"0112212233"]

Try it online!

a is the infinite sequence.

How?

It's not hard to find the recursive formula $$ a(n)=a\left(\left\lfloor\frac{n}{10}\right\rfloor\right)+a(n \operatorname{mod} 10) $$ with the base cases for \$n\in\{0,\ldots,9\}\$ given by $$ [0,1,1,2,2,1,2,2,3,3]. $$

This means that the infinite list a satisfies the equality

a==[i+j|i<-a,j<-[0,1,1,2,2,1,2,2,3,3]]

Now, Haskell is magical, but not magical enough to compute a from the definition

a=[i+j|i<-a,j<-[0,1,1,2,2,1,2,2,3,3]]

However, giving the first term explicitly is enough:

a=0:tail[i+j|i<-a,j<-[0,1,1,2,2,1,2,2,3,3]]

The 40-bytes code above is equivalent to this, but shorter.

Charcoal, 13 bytes

IΣ⭆S⭆↨Iι⁵L↨λ³

Try it online! Link is to verbose version of code. Explanation:

   S            Convert input to a string
  ⭆             Map over characters and join
       ι        Current character
      I         Cast to integer
     ↨  ⁵       Convert to base 5
    ⭆           Map over base 5 digits and join
           λ    Current digit
          ↨ ³   Convert to base 3
         L      Take the length
 Σ              Take the (digital) sum
I               Cast to string
                Implicitly print

Table of base 5 digit to base 3 length:

Decimal Base 3 Length
0       []     0
1       [1]    1
2       [2]    1
3       [1, 0] 2
4       [1, 1] 2

Retina 0.8.2, 20 15 bytes

.
$*1,
1{5}|11?

Try it online! Link includes test cases. Explanation:

.
$*1,

Convert each digit to unary separately.

1{5}|11?

Count the number of 5s, 2s and 1s needed to make each digit.

Python 2, 37 bytes

f=lambda n:n and n/5%2-n%5/-2+f(n/10)

Try it online!

Uses a formula rather than a lookup table for each digit. n/5%2 counts the five-cent coin, and subtracting -n%5/-2 is equivalent to adding (n%5+1)/2 for the one- and two-cent coins.

n%10      0 1 2 3 4 5 6 7 8 9
-----------------------------
n/5%2     0 0 0 0 0 1 1 1 1 1
0-n%5/-2  0 1 1 2 2 0 1 1 2 2
Total     0 1 1 2 2 1 2 2 3 3

Wolfram Language (Mathematica), 39 bytes

⌈.4#⌉-⌊#/5⌋&@*IntegerDigits/*Tr

Try it online!

Jelly, 7 bytes

Db5FHĊS

Try it online!

Steals Ports Dominic Van Essen's Husk answer, be sure to upvote that!

How it works

Db5FHĊS - Main link. Takes an integer n on the left
D       - Convert to digits
 b5     - Convert each digit to base 5
   F    - Flatten
    H   - Halve each
     Ċ  - Ceiling of each
      S - SUm

Jelly, 11 bytes

Dị“FȮŀO’D¤S

Try it online!

How it works

Dị“FȮŀO’D¤S - Main link. Takes n on the left
D           - Convert n to digits
         ¤  - Create a nilad:
  “FȮŀO’    -   Compressed integer: 1122122330
        D   -   To digits
 ị          - Index into the digits of this integer
          S - Sum

R, 54 51 47 45 bytes

Edit: converted to console input instead of a full function to try not to fall behind Robin Ryder's answer

d=utf8ToInt(scan(,''))-48;sum(d>0,d>5,d%%5>2)

Try it online!

Python 2, 48 bytes

f=lambda x:x and int("0112212233"[x%10])+f(x/10)

Try it online!

49 bytes in Python 3 because you'd need // for floor division.

05AB1E, 11 bytes

S<•δ¬Èº•sèO

Try it online!

Same approach as my Jelly answer.

How it works

S<•δ¬Èº•sèO - Program. Input: n
S           - Cast n to digits
 <          - Decrement
  •δ¬Èº•    - Compressed integer: 1122122330
        sè  - Using n's digits, index into the digits of 1122122330
          O - Sum

Kudos to Kevin Cruijssen's excellent integer compressor!

C (gcc), 39 bytes

f(n){n=n?f(n/10)+""[n%10]:0;}

Try it online!

JavaScript (Node.js), 37 bytes

f=n=>n&&+"0112212233"[n%10]+f(n/10|0)

Try it online!

Husk, 23 bytes

L◄Lfo=⁰Σ↑o≤⁰LṖƒ(+İ€m*10

Try it online!

Extremely slow past 11.

Explanation

L◄Lfo=⁰Σ↑o≤⁰LṖƒ(+İ€m*10
              ƒ(        create an infinite list using:
                 ݀     currency denomination builtin: [1,1/2,1/5,...200]
                +       plus
                   m*10 the input mapped to *10
                        this gives [1,1/2,...100] + 10*([1,1/2,...100] + (10*[1,1/2,...100]...))
             Ṗ          powerset(unordered)
        ↑o≤⁰L           take all sequences which have length ≤ input
                        this makes sure everything till [1]*n is in the list
   fo=⁰Σ                filter out the ones which do not sum to the input
 ◄L                     maximum element by length
L                       take the length