| Bytes | Lang | Time | Link |
|---|---|---|---|
| 071 | Python 3 | 231206T111654Z | guest430 |
| 024 | APLDyalog Unicode | 250616T085751Z | Mat_rdv |
| 043 | Perl 5 ap | 241107T204307Z | Xcali |
| nan | edit saving one byte with a loop | 231207T121543Z | Evargalo |
| 066 | AWK | 231211T132021Z | Marius_C |
| 045 | JavaScript Node.js | 231204T132603Z | l4m2 |
| 056 | C gcc | 231205T120746Z | Noodle9 |
| 035 | APL+WIN | 231205T111825Z | Graham |
| 014 | Vyxal 3 | 231205T085339Z | Nick Ken |
| 012 | Jelly | 231204T200256Z | Jonathan |
| 015 | 05AB1E | 231204T153443Z | Flummox |
| 015 | 05AB1E | 231204T144359Z | Kevin Cr |
| 024 | Charcoal | 231204T150841Z | Neil |
| 027 | K ngn/k | 231204T142918Z | ovs |
Python 3, 85 72 71 bytes
-13 bytes since I saw answers to other questions use a function
-1 byte from @Adamátor: if abs(x)<=v -> if-v<=x<=v
lambda x:[a for a in range(68)for b in range(a)if-.5<=x-100*b/a<=.5][0]
try it online!, or see all the numbers
pre-golf:
x=int(input())
for a in range(68): # for all denominators (starting from lowest)
for b in range(a): # and numerators (bonus, doesn't let /0 through)
if abs(x-100*b/a)<=.5: # is it within rounding range, up or down?
print(a) # then print the denominator
exit() # anything else will be bigger
credit to oeis for sequence and formula
APL(Dyalog Unicode), 24 bytes SBCS
{2⊃∊⍸⍵=⌊.5+100×∘.÷⍨⍳100}
Explanation
Because of rounding the result is not greater than 100, thus it is possible to check every pair \$(m,n)\$ (\$m\$ responders agreed from total of \$n\$). Moreover, the minimal suitable \$n\$ would be paired with the minimal suitable \$m\$ ( because if \$m_1 \leq m_2\$ and \$n_1 \geq n_2\$, hence \$\frac{m_1}{n_1} \leq \frac{m_2}{n_2}\$).
⍳100 numbers from 1 to 100
⍨ Selife: use the argument as both left and right arguments of a dyadic function
÷ Divide
∘. Outer product
∘.÷⍨⍳100 division table for numbers from 1 to 100
100× Multiply by 100
⌊.5+ idiom: Round to the nearest integer (add 0.5 and round the result down (⌊))
= Equals: compares a scalar value with each element of the matrix
⍵ the right argument (x)
⍸ Where: finds indices (i.e. pairs (m,n)) of all 1 in the matrix; indices are listed by the lexicographic order
∊ Enlist: list all elements of the vector of pairs as one vector in order
2⊃ Pick the second element (the minimal suitable n)
edit: saving one byte with a loop
R, 52 bytes
f=\(n){x=1;while(all(abs(n-100*(1:x)/x)>.5))x=x+1;x}
Previous solution, recursive
R, 53 bytes
f=\(n,x=1)`if`(all(abs(n-100*(1:x)/x)>.5),f(n,x+1),x)
APL+WIN, 35 bytes
Prompts for input:
⌊/(⎕=(⌈n-.5),⌊.5+n←100×÷m)/m,m←⍳100
Vyxal 3, 14 bytes
₅Dᵒ÷×ṚTȧd1≤ᶤaꜝ
Takes an integer argument and returns an integer argument. This is a Vyxal 3 translation of @JonathanAllan’s Jelly answer. It’s two bytes longer, partly because the outer modifier returns the result in the transposed reverse of what is needed for this problem.
₅Dᵒ÷×ṚTȧd1≤ᶤaꜝ
₅ # 100
D # Triplicate
ᵒ÷ # Outer divide by using (100..1),(100..1)
× # Multiply (by 100)
Ṛ # Reverse
T # Transpose
ȧ # Absolute difference with main input
d # Double
1≤ # Less than or equal to 1
ᶤa # Find first where any true
ꜝ # Increment by 1
💎
Created with the help of Luminespire.
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 accepts a positive integer from \$[1,100]\$, the reported percentage, and yields another positive integer, the minimal number of respondents.
Try it online! Or see all 100.
How?
ạȷ2÷þפḤỊ§TḢ - Link: positive intger from [1..100], P
¤ - nilad followed by link(s) as a nilad:
ȷ2 - 10^2 -> 100
þ - {[1..100]} x {[1..100]} table of:
÷ - divide
× - multiply by {100} (vectorises)
-> [[100],[50.0,100],[33.33,66.66,100],[25.0,50.0,75.0,100],...]
ạ - {P} absolute difference (vectorises) {that}
Ḥ - double (vectorises)
Ị - is insignificant? (vectorises) - i.e. abs(x)<=1
§ - sums
T - truthy indices
Ḣ - head
05AB1E, 15 bytes
∞.ΔDÝs/т*α2zs@à
Explanation:
∞ Push infinite list
.Δ Find first item in list where:
D Duplicate item
Ý Create an array 0-N
s/ Switch last two and divide
т* Multiply by 100
α Absolute difference (input is automatically in stack)
2zs Push 0.5 before the last item
@ Compare 0.5 with list (>=)
à Check if the list contains 1
05AB1E, 15 bytes
∞.ΔтLт*s/α2zs@à
Try it online or verify all outputs.
Explanation:
∞ # Push an infinite positive list: [1,2,3,...]
.Δ # Pop and find the first/smallest that's truthy for:
тL # Push a list in the range [1,100]
т* # Multiply each by 100: [100,200,...,10000]
s/ # Divide each by the current integer
α # Get the absolute difference of each with the (implicit) input
2zs@ # Check for each whether it's <= 0.5:
2 # Push 2
z # Pop and push 1/2
s # Swap so the list is at the top
@ # >= check
à # Pop and check if any in the list is truthy
# (after which the result is output implicitly)
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. Explanation: Uses the OEIS formula.
Nθ First input as a number
φ Predefined variable `1000`
Φ Filter on implicit range
ι Current value
∧ Logical And
φ Predefined variable `1000`
Φ Filter on implicit range
λ Inner value
× ¹⁰⁰ Multiplied by `100`
∕ Divided by
ι Outer value
↔⁻ Absolute difference with
θ Input number
¬‹·⁵ Is less than or equal to `0.5`
⌊ Take the minimum
I Cast to string
Implicitly print
K (ngn/k), 27 bytes
**&~`i$(199*%/:/&=69)-1.99*
Based on the formula from the OEIS entry:
Find the smallest N such that there is some x > 0 with abs(100*x/N - n) <= 0.5.
For golfing reasons this actually checks -1 < 1.99*(100*x/N - n) < 1, which happens to be equivalent on the domain of the problem.