| Bytes | Lang | Time | Link |
|---|---|---|---|
| 040 | YASEPL | 250904T132735Z | madeforl |
| 082 | Python 3 | 220902T131042Z | Rhaixer |
| nan | 220928T135251Z | bigyihsu | |
| 059 | Scala 3 | 240503T081256Z | 138 Aspe |
| 031 | ><> | 221111T120438Z | Emigna |
| 030 | Julia 1.0 | 221027T200441Z | Ashlin H |
| 028 | Julia 1.0 | 221027T211545Z | amelies |
| nan | Fig | 220928T023341Z | south |
| 036 | Ruby | 220929T210705Z | touka |
| 012 | Japt f | 220928T102633Z | Shaggy |
| 008 | Vyxal | 220810T002446Z | naffetS |
| 016 | Brachylog | 220914T174926Z | DLosc |
| 015 | Pip p | 220914T171550Z | DLosc |
| 053 | Prolog SWI | 220902T064403Z | Aiden Ch |
| 816 | Nibbles | 220811T204737Z | Dominic |
| 026 | x86 64bit machine code | 220811T144952Z | m90 |
| 030 | PARI/GP | 220810T011406Z | alephalp |
| 040 | Desmos | 220810T060154Z | Aiden Ch |
| 031 | R | 220810T050714Z | pajonk |
| 035 | Perl 5 n | 220810T173957Z | Xcali |
| 033 | lin | 220810T164310Z | Mama Fun |
| 038 | Haskell | 220810T163145Z | Benji |
| 052 | C gcc | 220810T124932Z | Noodle9 |
| 031 | Regex ECMAScript 2018 / Pythonregex / .NET | 220810T050749Z | Deadcode |
| 023 | APL Dyalog Classic | 220810T145106Z | atpx8 |
| 016 | Charcoal | 220810T143145Z | Neil |
| 064 | Java 8 | 220810T065949Z | Kevin Cr |
| 034 | Knight | 220810T054616Z | Aiden Ch |
| 013 | MathGolf | 220810T061918Z | Kevin Cr |
| 008 | 05AB1E | 220810T060404Z | Kevin Cr |
| 039 | JavaScript SpiderMonkey | 220810T053632Z | tsh |
| 061 | Factor | 220810T040454Z | chunes |
| 008 | Jelly | 220810T015046Z | Unrelate |
| 031 | Raku | 220810T004928Z | Jo King |
| 046 | Python 2 | 220810T004612Z | xnor |
YASEPL, 40 bytes
=i$2=n'@3`1=l$n%i*:i]2@2+2[3`2>i`3!+}2,n
explanation:
=i$2=n'@3`1=l$n%i*:i]2@2+2[3`2>i`3!+}2,n packed
=i$2 set (i)ncrement to 2
=n' get i(n)put
@3 if its equal to 1, go to label 3
`1 ! }2,n while i < n ...
=l$n%i set l to n % i
* times 2
:i set l to i - l
]2@2 `2>i if l is 0 or 1, print i
+2[3 if l is not -1, do not print i
`2>i otherwise print i
`3 label 3
!+ add 1 to i and check while
Python 3, 102 91 90 88 87 82 bytes
+14 bytes because of Jo King
-3 bytes because of booleans
-1 byte because of unintentional non-functional whitespace
-11 bytes because lambdas are small
-1 byte because stray ")"
-2 bytes because of a bug that can be exploited in 3.11
-1 byte due to obvious golf
-5 bytes looking back
lambda n:[m for m in range(2,n)if m%2<1and n%m==m/2or(m%2and n%m in[~-m/2,-~m/2])]
Explained:
lambda n: # create a function for that n where
[ # list begins
m # pick this item
for m in range(2,n) # in the range 2 to n
if m%2==0 # if it is even
and n%m==m/2 # and fits condition 1
or(m%2==1 # or if it is odd
and n%m in[(m-1)/2,(m+1)/2]) # and is in one of the 2 possibilities of condition 2
] # close list
Go, 119 117 bytes
func(n int)(a[]int){for i:=2;i<n;i++{k:=n%i
if i%2<1&&k==i/2||i%2>0&&(k==(i-1)/2||k==(i+1)/2){a=append(a,i)}}
return}
- -2 bytes by making an anonymous function
Scala 3, 59 bytes
Golfed version. Attempt This Online!
n=>{(2 to n-1).filter(m=>{val c=n%m*2-m;c> -2&&c<2}).toSeq}
Ungolfed version. Attempt This Online!
object Main {
def main(args: Array[String]): Unit = {
val inputs = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 18, 20, 234, 325)
def f(n: Int): List[Int] = {
(2 until n).filter(m => {
val calc = n % m * 2 - m
calc > -2 && calc < 2
}).toList
}
inputs.foreach { n =>
println(s"$n -> ${f(n)}")
}
}
}
Julia 1.0, 37 30 bytes
~n=filter(m->-1<n%m-m/2<1,2:n)
-7 bytes thanks to amelies: improve inequality, pass range directly to filter
Julia 1.0, 30 28 bytes
Golfing further Ashlin Harris's solution formula
~n=(N=2:n;@.N[-1<n%N-N/2<1])
Fig, \$14\log_{256}(96)\approx\$ 11.524 bytes
Fpax'>2A-h%x#x
Fpax'>2A-h%x#x
ax - Range [1..input]
p - Remove the first item to make this [2..input]
F ' - Filter by (where the current item is x):
%x#x - Modulo x by the input
h - Multiply that by 2
A- - Get the absolute difference between x and that
>2 - Is it less than 2?
Japt -f, 12 bytes
ÊÉ©AìøUaN%UÑ
Without Flags, 13 bytes
õ ÅkÈaU%XÑ ÊÉ
ÊÉ©AìøUaN%UÑ :Implicit filter of each U in range [0,input)
Ê :Factorial
É :Subtract 1
© :Logical AND with
A :10
ì :To digit array
ø :Contains?
Ua : Absolute difference of U and
N : Array of all inputs (singleton, in this case)
%U : Mod U
Ñ : Multiplied by 2
õ ÅkÈaU%XÑ ÊÉ :Implicit input of integer U
õ :Range [1,U]
Å :Remove first element
k :Remove elements that return true
È :When passed through the following function as X
a : Absolute difference with
U%XÑ : U mod X times 2
Ê : Factorial
É : Subtract 1
Brachylog, 16 bytes
>.&;.%×₂;.-ȧ<2≤≜
A predicate that acts as a generator. Try it online!
Explanation
Brachylog's clunky arithmetic builtins make it less than ideal for this challenge. Maybe there's a better way to do it.
>.&;.%×₂;.-ȧ<2≤≜
>. The output is less than the input
& And the input
;.% Modulo the output
×₂ Times 2
;.- Difference with the output
ȧ Absolute value
<2 Is less than 2
≤ Which is less than or equal to
≜ A specific number
Which is the output
Pip -p, 15 bytes
a%_AD_/2<1FI2,a
Explanation
a%_AD_/2<1FI2,a
a Command-line argument
2, Range from 2 (inclusive) to that number (exclusive)
FI Filter by this function:
a%_ Program argument (number) mod function argument (anti-divisor candidate)
AD Absolute difference with
_/2 Function argument divided by 2
<1 Is less than 1
Prolog (SWI), 54 53 bytes
Huge thanks to JoKing for helping me out immensely for this answer, from debugging my errors and helping me find solutions to my problems
Also -1 byte thanks to JoKing
N+X:-findall(M,(between(2,N,M),(N mod M*2-M)^2<4),X).
Nibbles, 8 bytes (16 nibbles)
>>|,$~-!=$*%@$~~
| # filter
,$ # the list 1..input
~ # by the falsy results of
!= # the abxolute difference between
$ # each element and
* ~ # twice (2 is default value '~' for multiplication)
%@$ # modulo of input by each element
- ~ # minus 1 (1 is default value '~' for subtraction)
# (note that zero and negative numbers are falsy);
>> # finally remove the first element (always 1)
x86 64-bit machine code, 26 bytes
89 F1 51 8D 44 71 01 99 01 C9 F7 F1 58 83 FA 02 77 01 AB 91 E2 EC FF 4F FC C3
Takes \$n\$ in ESI and writes the anti-divisors as consecutive 32-bit integers, terminated with a 0, to an address given in RDI.
In assembly:
f: mov ecx, esi
r: push rcx
lea eax, [2*rsi+rcx+1]
cdq
add ecx, ecx
div ecx
pop rax
cmp edx, 2
ja s
stosd
s: xchg ecx, eax
loop r
dec DWORD PTR [rdi-4]
ret
PARI/GP, 30 bytes
-2 bytes thanks to @xnor.
-1 byte thanks to @pajonk and @Dominic van Essen
n->[d|d<-[2..n],(n%d-d/2)^2<1]
Desmos, 40 bytes
l=[1...n][2...]
f(n)=l[-2<2mod(n,l)-l<2]
R, 32 31 bytes
Edit: -1 byte thanks to @Dominic van Essen.
\(n,m=1:n)m[(n%%m-m/2)^2<1][-1]
Pretty much similar to other answers.
The n=1 is problematic: we can't use m=2:n as this would result in m=2:1=c(2,1). So we use m=1:n and then remove the first element (1) with [-1] at the end.
Perl 5 -n, 35 bytes
//;map{abs($'%$_*2-$_)<2&&say}2..$_
Uses a take on xnor's formula in his python answer.
lin, 33 bytes
.#n.n.<1drop".n.+_ %.~2/ -2^1<".#
For testing purposes (use -i flag when running locally):
1 11 .-> 18 20 234 325 ( ;.$$ ).'
.#n.n.<1drop".n.+_ %.~2/ -2^1<".#
Explanation
Prettified code:
.#n .n.< 1drop (.n.+_ %.~ 2/ - 2^ 1< ).#
Assuming input n.
.n.< 1droprange [n, 2](...).#filter....n.+_ %.~ 2/ - 2^ 1<equivalent to(n%m - m/2)^2 < 1
C (gcc), 58 52 bytes
m;f(n){for(m=1;++m<n;)(n%m*2-m)/2||printf("%d ",m);}
Saved 6 bytes thanks to the suggestion of porting Kevin's answer by Neil!!!
Port of Kevin Cruijssen's Java answer.
Regex (ECMAScript 2018 / Pythonregex / .NET), 35 31 bytes
(?=((x+)\2(x?$))(?<=^\3?\2\1*))
Try it online! - ECMAScript 2018 / Try it online! - test cases only
Try it online! - Python import regex /
Try it online! - test cases only
Try it online! - .NET / Try it online! - test cases only
Takes its input in unary, as a string of x characters whose length represents the number. Outputs its result as the list of matches' \1 captures.
This version takes advantage of variable-length lookbehind that is right-to-left evaluated:
# tail = conjectured anti-divisor
(?=
( # \1 = tail
(x+)\2(x?$) # \2 = floor(tail / 2); \3 = tail % 2; tail = 0;
# head = input number
)
(?<= # Variable-length lookbehind; read from bottom to top.
^ # Assert head == 0
\3? # optionally, head -= \3
\2 # Assert head ≥ \2; head -= \2
\1* # head %= \1; due to backtracking this result may also have
# a multiple of \1 added to it, but it will not be able to
# match in that case since \2+\3 is guaranteed to be less
# than \1.
)
)
Regex (ECMAScript 2018 / Java / Pythonregex / .NET), 38 37 bytes
(?=((x+)\2(x?)))(?<=(?=^\1*\2\3?$).*)
Try it online! - ECMAScript 2018 / Try it online! - test cases only
Try it online! - Java / Try it online! - test cases only
Try it online! - Python import regex / Try it online! - test cases only
Try it online! - .NET / Try it online! - test cases only
Java has variable-length lookbehind, with some limits, including that it's not right-to-left evaluated.
# tail = conjectured anti-divisor
(?=
( # \1 = tail
(x+)\2(x?) # \2 = floor(tail / 2); \3 = tail % 2
)
)
(?<= # Variable-length lookbehind
(?=
^ # Assert we're at the beginning of the string;
# tail = input number
\1* # tail %= \1
\2 # Assert tail ≥ \2; tail -= \2
\3? # optionally, tail -= \3
$ # Assert tail == 0
)
.* # Skip to beginning of string
)
Regex (Perl / PCRE2 / Pythonregex), 44 43 bytes
(?=((x+)\2(x?)))((?<=(?=^\1*\2\3?$|(?4)).))
Try it online! - Perl / Try it online! - test cases only
Try it online! - PCRE2 / Try it online! - test cases only
Emulates variable-length lookbehind using recursion and fixed-length lookbehind.
Regex (Perl / PCRE / Pythonregex), 46 45 bytes
(?=((x+)\2(x?)))((?<=(?=z|^\1*\2\3?$|(?4)).))
Try it online! - Perl / Try it online! - test cases only
Try it online! - PCRE1 / Try it online! - test cases only
Try it online! - PCRE2 / Try it online! - test cases only
Works around PCRE1 being picky about recursion it thinks can be endless.
Both Perl/PCRE versions work on the latest version of Pythonregex, but not the one on TIO.
APL (Dyalog Classic), 23 bytes
{1↓⍵{(2>|⍵-2×⍵|⍺)/⍵}⍳⍵}
Usage:
antidiv←{1↓⍵{(2>|⍵-2×⍵|⍺)/⍵}⍳⍵}
antidiv 10
3 4 7
antidiv 325
2 3 7 10 11 21 26 31 50 59 93 130 217
Charcoal, 16 bytes
NθIΦ…²θ‹↔⁻⊗﹪θιι²
Try it online! Link is to verbose version of code. Explanation:
Nθ First input as a number
… Range from
² Literal integer `2` to
θ Input number
Φ Filtered where
θ Input number
﹪ Modulo
ι Current value
⊗ Doubled
↔⁻ Absolute difference with
ι Current value
‹ Is less than
² Literal integer `2`
I Vectorised cast to string
Implicitly print each value on its own line
Java 8, 69 64 bytes
n->{for(int i=1;++i<n;)if((n%i*2-i)/2==0)System.out.println(i);}
-5 bytes thanks to @Deadcode
Outputs the results to STDOUT on separated lines.
Explanation:
n->{ // Method with integer parameter and no return-type
for(int i=1;++i<n;) // Loop `i` in the range (1,input]:
if((n%i*2-i) // If the input modulo `i`, doubled, minus `i`
/2 // integer-divided by 2
==0) // equals 0:
System.out.println(i);} // Print `i` with trailing newline
Knight, 40 34 bytes
-6 bytes thanks to @Bubbler by looping backwards
;=i=pE P W<1=i-iT&>4^-*2%p i i 2Oi
Outputs each anti-divisor in a separate line.
I feel like there's just way too much whitespace; there must be a some rearrangement that can avoid some of this whitespace lol
MathGolf, 13 bytes
╒╞g{_k\%∞-±2<
Explanation:
╒ # Push a list in the range [1, (implicit) input]
╞ # Remove the leading 1 to make the range [2,input]
g # Filter this list by,
{ # using an arbitrary large inner code-block:
# (implicitly push the filter index and value)
_ # Duplicate the value
k # Push the input-integer
\ # Swap so the value is at the top again
% # Modulo the input by the value
∞ # Double it
- # Subtract the two values from one another
± # Get the absolute value of this
2< # Check that this is smaller than 2 (so either 0 or 1)
# (after which the entire stack is output implicitly as result)
05AB1E, 8 bytes
L¦ʒ%·yα!
Try it online or verify all test cases.
Explanation:
L # Push a list in the range [1, (implicit) input]
¦ # Remove the leading 1 to make the range [2,input]
ʒ # Filter this list by:
% # Modulo the (implicit) input by the current value
· # Double it
yα # Get the absolute difference with the current value
! # Factorial (1 remains 1; 0 becomes 1; everything else just increases)
# (only 1 is truthy in 05AB1E)
# (after which the filtered list is output implicitly)
Factor, 61 bytes
[ dup [1,b] [ [ mod 2 * ] keep - [-1,1]? ] with filter rest ]
One of six variations I tried that all come out to 61 bytes. Went with this one because I've never used [-1,1]? in a golf before and I think it's a neat word.
Jelly, 8 bytes
%RḤ_RỊTḊ
Feels... messy, but the best I can think of to reuse the range ties: Ḋ%Ḥ_Ịʋ@Ƈ
%R n mod each [1 .. n]
Ḥ times 2
_R minus each corresponding [1 .. n]
Ị in [-1 .. 1]?
T Find truthy indices
Ḋ and remove the first (always 1).
Raku, 31 bytes
{grep {1>$_%$^a-$a/2>-1},2..$_}
Anonymous code block that takes a number and returns a list.
