24
\$\begingroup\$

Given a positive integer n, output all of its anti-divisors in any order.

From OEIS A006272:

Anti-divisors are the numbers that do not divide a number by the largest possible margin. E.g. 20 has anti-divisors 3, 8 and 13. An alternative name for anti-divisor is unbiased non-divisors.

In other words, 1 < m < n is an anti-divisor of n if either

  • m is even and n % m == m/2, or
  • m is odd and n % m is equal to either (m-1)/2 or (m+1)/2.

Notably, 1 is not an anti-divisor of any number because it does not satisfy the phrase "do not divide a number".

Standard rules apply. Shortest code in bytes wins.

Test cases

1 -> []
2 -> []
3 -> [2]
4 -> [3]
5 -> [2, 3]
6 -> [4]
7 -> [2, 3, 5]
8 -> [3, 5]
9 -> [2, 6]
10 -> [3, 4, 7]
18 -> [4, 5, 7, 12]
20 -> [3, 8, 13]
234 -> [4, 7, 12, 36, 52, 67, 156]
325 -> [2, 3, 7, 10, 11, 21, 26, 31, 50, 59, 93, 130, 217]
\$\endgroup\$

33 Answers 33

1
2
1
\$\begingroup\$

><>, 31 bytes

!\:1-:2(?;::{:@}%2*-:*1)?
o\:na

Try it online

\$\endgroup\$
1
\$\begingroup\$

Python 3, 102 91 90 88 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

lambda n:[m for m in range(2,n)if m%2==0and n%m==m/2 or(m%2and n%m in[(m-1)/2,(m+1)/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
)                                                          # print is done

NOTE: This will be broken in a future version of Python 3.

\$\endgroup\$
0
0
\$\begingroup\$

Pip -p, 15 bytes

a%_AD_/2<1FI2,a

Try It Online!

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
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.