New answers tagged arithmetic
1
vote
80% of respondents agreed…
R, 53 bytes
f=\(n,x=1)`if`(all(abs(n-100*(1:x)/x)>.5),f(n,x+1),x)
Try it online!
2
votes
The vanilla factorial challenge
Vyxal 3L, 2 bytes
/*
Try it Online!
Fold / the input by multiplication *. Fold treats whole ...
3
votes
80% of respondents agreed…
Python 3, 85 72 bytes
-13 bytes since I saw answers to other questions use a function
lambda x:[a for a in range(68)for b in range(a)if abs(x-100*b/a)<=.5][0]
...
5
votes
80% of respondents agreed…
C (gcc), 56 bytes
i;j;f(n){for(i=j=1;fabs(100.*j/i-n)>.5;j=--j?:++i);j=i;}
Try it online!
4
votes
80% of respondents agreed…
APL+WIN, 35 bytes
Prompts for input:
⌊/(⎕=(⌈n-.5),⌊.5+n←100×÷m)/m,m←⍳100
Try it online! Thanks to Dyalog Classic
4
votes
80% of respondents agreed…
Vyxal 3, 14 bytes
₅Dᵒ÷×ṚTȧd1≤ᶤaꜝ
Try it Online!
Takes an integer argument and returns an integer argument. This is a Vyxal 3 translation of @JonathanAllan’s Jelly ...
1
vote
Shift right by half a bit
JavaScript (Node.js), 31 bytes
n=>(g=i=>i*i*2<n*n?g(++i):i)(0)
Try it online!
or BigInt version, anyway really large values impractical to run
Brute-...
1
vote
Shift right by half a bit
Python 3.8 (pre-release), 40 bytes
This one works with python integers, giving the correct result even for arbitrarily large integers. Always rounds down.
...
7
votes
80% of respondents agreed…
Jelly, 14 12 bytes
-2 thanks to Nick Kennedy! (Make a 100 by 100 table with values over 100% rather than a triangle of values.)
ạȷ2÷þפḤỊ§TḢ
A monadic Link that ...
3
votes
2
votes
80% of respondents agreed…
Charcoal, 24 bytes
NθI⌊Φφ∧ιΦφ¬‹·⁵↔⁻θ∕×λ¹⁰⁰ι
Try it online! Link is to verbose version of code. Very slow as it does about 1,000 times as much work as necessary. ...
3
votes
80% of respondents agreed…
05AB1E, 15 bytes
∞.ΔтLт*s/α2zs@à
Try it online or verify all outputs.
Explanation:
...
9
votes
80% of respondents agreed…
K (ngn/k), 27 bytes
**&~`i$(199*%/:/&=69)-1.99*
Try it online!
Based on the formula from the OEIS entry:
Find the smallest N such that there is some x >...
5
votes
80% of respondents agreed…
JavaScript (Node.js), 45 bytes
x=>g=i=>(x-i%69/(y=i>>7)*100)**2<.26?y:g(-~i)
Try it online!
2
votes
Count without 3
Swift, 84 bytes
func f(_ n:Int)->Int{var n=n+1;while String(n).contains("3")||n%3==0{n+=1};return n}
1
vote
Find the Factorial!
Kotlin, 46 bytes
{x:Int->x.downTo(1).reduceOrNull{a,i->a*i}?:1}
Bit of a shame that we need that type hint...
Attempt This Online!
2
votes
1
vote
Find the Factorial!
Vyxal 3, 3 bytes
Ω}Π
Try it Online!
-2 bytes thanks to @lyxal
Explanation (outdated):
...
1
vote
Count without 3
Google Sheets, 63 bytes
=+let(n,sequence(254),filter(n,n>A1,mod(n,3),iserr(find(3,n))))
Put the input in cell A1 and the ...
1
vote
Count without 3
GNU sed -E, 148 bytes
Could be easier with a language with a modulo operator. Or division. Or counting. Or some idea about what numbers are. ...
2
votes
Multiplicative persistence
Go, 87 bytes
import."fmt"
func f(n int){Println(n)
s:=1
if n<10{return}
for;n>0;n/=10{s*=n%10}
f(s)}
Attempt This Online!
0
votes
Additive Persistence
Go, 77 bytes
func f(n int)int{s:=0
if n<10{return 0}
for;n>0;n/=10{s+=n%10}
return 1+f(s)}
Attempt This Online!
1
vote
Multiplicative persistence
Vyxal 3, 2 bytes
ᵘΠ
Try it Online!
2 bytes with a new modifier. Come try Vyxal 3!
1
vote
0
votes
German pharmacy payment
*><>, 90 89 bytes
-1 byte by removing a semicolon that shouldn't have been there
I'm still trying to condense it using the dive u and rise ...
4
votes
German pharmacy payment
APL (Dyalog Unicode), 13 bytes
2⍕⊢⌊10⌊5⌈.1×⊢
Try it online!
Tacit prefix function. Takes input as number.
Thanks to Graham for original APL solution.
How it works:...
1
vote
Sum the array times n, except the last
Desmos, 32 31 bytes
f(a,n)=na.total-(n-1)a[a.count]
Try it on Desmos
-1 byte thanks to Aiden Chow.
2
votes
Sum the array times n, except the last
TI-BASIC, 17 bytes
Input N
Nsum(Ans)-(N-1)Ans(dim(Ans
Takes input of \$A\$ in Ans.
Alternatives that are also 17 bytes:
...
1
vote
0
votes
German pharmacy payment
Rust, 50 bytes
|x|(500f64.max(1e1*x).min(1e3).round()/1e2).min(x)
Try it online!
-1
votes
German pharmacy payment
Desmos, 30 actually 35 bytes
A one-liner Desmos equation for once (thanks to Aiden Chow for the fix and optimization):
f(x)=round([x,5,10,x/10].sort[2],2)
Try it ...
1
vote
German pharmacy payment
Python, 41 bytes
lambda x:min(max(5,(x*20+1)//2/100),x,10)
Attempt This Online!
Straight-forward implementation of OP's formula. For the rounding it uses floor ...
0
votes
German pharmacy payment
Scala 3, 106 100 bytes
Saved 6 bytes thanks to @corvus_192
Golfed version. Attempt This Online!
...
0
votes
German pharmacy payment
AWK, 48 bytes
{y=$0/10;a=y>5?y>10?10:y:5>$0?$0:5;print a+1e-4}
Try it online!
Ungolfed :
...
0
votes
German pharmacy payment
05AB1E, 16 (or 25) bytes
T/5‚à‚Tªß4°z+2.ò
Try it online or verify all test cases.
This outputs 5.0 or ...
0
votes
German pharmacy payment
PARI/GP, 44 bytes
x->printf("%.2f",min(x,min(max(x/10,5),10)))
Attempt This Online!
A port of @Evargalo's R answer.
Prints the result to stdout.
0
votes
German pharmacy payment
Wolfram Language (Mathematica), 36 bytes
#~Min~Max[.1#,5]~Min~10~Round~.01&@x
Alas, we cannot save an extra byte by using infix notation with ...
0
votes
German pharmacy payment
sclin, 26 bytes
dup.1*5|10,` &/100* |~100/
Try it on scline!
sclin's default real/rational number representation comes in handy here.
Explanation
Prettified ...
5
votes
German pharmacy payment
R, 39 bytes
\(x)round(.001+min(x,max(.1*x,5),10),2)
Basically using the formula given by the OP.
-5 bytes thanks to @doubleunary
-3 bytes and fix a rounding error ...
0
votes
2
votes
German pharmacy payment
Ruby, 42 36 bytes
->a{[a,[a/10,5].max,10].min.round 2}
Try it online!
Ruby, 38 bytes
Formatted as per initial requirement:
...
2
votes
German pharmacy payment
Google Sheets / Microsoft Excel, 31 bytes
=fixed(min(10,A1,max(5,A1/10)))
Put the input in cell A1 and the formula in ...
0
votes
German pharmacy payment
Charcoal, 21 bytes
NθI⌊⟦θχ⌈⟦⁵∕⌊⁺·⁵×θχ¹⁰⁰
Try it online! Link is to verbose version of code. Explanation: Port of the formula given in the question, but with ...
2
votes
German pharmacy payment
Retina 0.8.2, 95 92 bytes
\.
.+
$*
^(.{1000}(?=.{8995})|.(.{499,998})(?=\2{9}....)|.{0,500}).*
00$.1
0*(.+)(..)
$1.$2
Try it online! Link includes test cases. ...
8
votes
German pharmacy payment
Jelly, 14 bytes
÷⁵«⁵»5«+ȷ-4ær2
A monadic Link that accepts the full price as a float and outputs the charge as a float.
Try it online!
How?
...
1
vote
German pharmacy payment
JavaScript (ES6), 45 bytes
x=>~~(Math.min(x*10,x<50?50:x,100)*10+.5)/100
Try it online!
40 bytes
This one works in theory, but fails on some test cases ...
6
votes
German pharmacy payment
APL+WIN, 21 bytes
Prompts for input
2⍕(n⌊((.1×n←⎕)⌈5))⌊10
Try it online! Thanks to Dyalog Classic
3
votes
German pharmacy payment
JavaScript (Node.js), 40 bytes
x=>Math.min(x,x<50?5:(x*10+.5|0)/100,10)
Try it online!
Python 3, 45 bytes
...
0
votes
Is it a Cyclic number?
Scala 3, 85 bytes
n=>(2 to n.size).forall(i=>n.sorted==(BigInt(n)*i).toString.padTo(n.size,'0').sorted)
Attempt This Online!
0
votes
Column-wise Sum
Scala, 64 bytes
Golfed version. Try it online!
l=>{l.map(x=>x.padTo(l.map(_.size).max,0)).transpose.map(_.sum)}
Ungolfed version. Try it online!
...
Top 50 recent answers are included
Related Tags
arithmetic × 543code-golf × 506
math × 183
number × 151
sequence × 60
integer × 58
number-theory × 47
string × 39
array × 39
decision-problem × 18
expression-building × 16
code-challenge × 14
ascii-art × 13
base-conversion × 13
restricted-source × 12
geometry × 12
combinatorics × 12
primes × 11
matrix × 10
parsing × 10
atomic-code-golf × 10
kolmogorov-complexity × 9
grid × 9
binary × 9
game × 8