14
A cyclic difference set is a set of positive integers with a unique property:
- Let
n
be the largest integer in the set. - Let
r
be any integer (not necessarily in the set) greater than 0 but less than or equal ton/2
. - Let
k
be the number of solutions to(b - a) % n = r
wherea
andb
are any members of the set. Each solution is an ordered pair(a,b)
. (Also note that this version of modulo makes negative numbers positive by addingn
to it, unlike the implementations in many languages.) - Finally, if and only if this is a cyclic difference set, the value of
k
does not depend on your choice ofr
. That is, all values ofr
give the same number of solutions to the above congruence.
This can be illustrated with the following example:
Cyclic difference set: {4,5,6,8,9,11}
0 < r <= 11/2, so r = 1,2,3,4,5
r=1: (4,5) (5,6) (8,9)
r=2: (4,6) (6,8) (9,11)
r=3: (5,8) (6,9) (8,11)
r=4: (4,8) (5,9) (11,4) since (4-11)%11=(-7)%11=4
r=5: (4,9) (6,11) (11,5)
Each value of r
has the same number of solutions, 3 in this case, so this is a cyclic difference set.
Input
Input will be a list of positive integers. Since this is a set property, assume that input is not sorted. You can assume that n
is at least 2
, although k
may be zero.
Output
Your program/function should output a truthy value if the set is a cyclic difference set, or a falsey value otherwise.
Test Cases
Valid cyclic difference sets:
10,12,17,18,21
7,5,4
57,1,5,7,17,35,38,49
1,24,35,38,40,53,86,108,114,118,135,144,185,210,254,266,273
16,3,19,4,8,10,15,5,6
8,23,11,12,15,2,3,5,7,17,1
(data source, although their convention is different)
Invalid cyclic difference sets:
1,2,3,4,20
57,3,5,7,17,35,38,49
3,4,5,9
14,10,8
1Can
a
andb
be the same member (not necessarilya ≠ b
)? – Erik the Outgolfer – 2018-02-09T16:24:27.0072@EriktheOutgolfer if
b
anda
are the same number, then(b-a)%n = 0
, but 0 isn't one of the values that you're looking for solutions for. So there's not an explicit prohibition on them being the same number, but they never will be. – PhiNotPi – 2018-02-09T16:27:56.227Please add
14,10,8
to the list of non difference sets (to catch programs that ignore0
counts) – Ton Hospel – 2018-02-09T18:11:08.8571I'd really prefer it if
7 7 7
was invalid input. A set doesn't repeat values – Ton Hospel – 2018-02-09T18:24:06.3931@TonHospel Done and done.
7 7 7
was a requested by another user, but I've removed it because it is not a set. – PhiNotPi – 2018-02-09T18:54:31.6831Golfing idea: we don't need to bound
r
by0 < r <= max(input)/2
, but instead0 < r < max(input)
because we can obtainr > max(input)/2
cases by simply flipping the subtraction inr <= max(input)/2
cases. – JungHwan Min – 2018-02-09T20:53:51.360