| Bytes | Lang | Time | Link |
|---|---|---|---|
| 050 | Juby | 250507T001010Z | Jordan |
| 007 | 05AB1E legacy | 210504T131733Z | Kevin Cr |
| 045 | R | 210412T144244Z | Robin Ry |
| 035 | JavaScript ES7 | 210412T145619Z | Arnauld |
| 008 | Husk | 210412T151629Z | Dominic |
| 005 | Vyxal | 210413T003658Z | lyxal |
| 040 | Haskell | 210412T145440Z | Delfad0r |
| 013 | Charcoal | 210412T202416Z | Neil |
| 015 | Retina 0.8.2 | 210412T195736Z | Neil |
| 037 | Python 2 | 210412T191916Z | xnor |
| 039 | Wolfram Language Mathematica | 210412T181211Z | att |
| 007 | Jelly | 210412T142002Z | caird co |
| 045 | R | 210412T150106Z | Dominic |
| 048 | Python 2 | 210412T152934Z | hyperneu |
| 011 | 05AB1E | 210412T151256Z | caird co |
| 039 | C gcc | 210412T145614Z | l4m2 |
| 023 | Husk | 210412T145405Z | Razetime |
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])
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)
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)
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)
Husk, 11 9 8 bytes
Edit: -1 byte thanks to caird coinheringaahing
ṁo⌈½ṁB5d
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τ½⌈
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, 55…41 40 bytes
- -1 byte thanks to xnor, for using a string instead of the hard-coded list.
a=0:tail[i+read[j]|i<-a,j<-"0112212233"]
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)
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
Jelly, 7 bytes
Db5FHĊS
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
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)
Python 2, 48 bytes
f=lambda x:x and int("0112212233"[x%10])+f(x/10)
49 bytes in Python 3 because you'd need // for floor division.
05AB1E, 11 bytes
S<•δ¬Èº•sèO
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
C (gcc), 39 bytes
f(n){n=n?f(n/10)+""[n%10]:0;}
JavaScript (Node.js), 37 bytes
f=n=>n&&+"0112212233"[n%10]+f(n/10|0)
Husk, 23 bytes
L◄Lfo=⁰Σ↑o≤⁰LṖƒ(+İ€m*10
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