# Uniquely separated pixels

For an N by N image, find a set of pixels such that no separation distance is present more than once. That is, if two pixels are separated by a distance d, then they are the only two pixels that are separated by exactly d (using Euclidean distance). Note that d need not be integer.

The challenge is to find a larger such set than anyone else.

### Specification

No input is required - for this contest N will be fixed at 619.

(Since people keep asking - there's nothing special about the number 619. It was chosen to be large enough to make an optimal solution unlikely, and small enough to let an N by N image be displayed without Stack Exchange automatically shrinking it. Images can be displayed full size up to 630 by 630, and I decided to go with the largest prime that doesn't exceed that.)

The output is a space separated list of integers.

Each integer in the output represents one of the pixels, numbered in English reading order from 0. For example for N = 3, the locations would be numbered in this order:

0 1 2
3 4 5
6 7 8


You may output progress information during running if you wish, as long as the final scoring output is easily available. You may output to STDOUT or to a file or whatever is easiest for pasting into the Stack Snippet Judge below.

### Example

N = 3

Chosen coordinates:

(0,0)
(1,0)
(2,1)


Output:

0 1 5


### Winning

The score is the number of locations in the output. Of those valid answers which have the highest score, the earliest to post output with that score wins.

Your code does not need to be deterministic. You may post your best output.

### Related areas for research

(Thanks to Abulafia for the Golomb links)

While neither of these is the same as this problem, they are both similar in concept and may give you ideas on how to approach this:

Note that the points required for this question are not subject to the same requirements as a Golomb rectangle. A Golomb rectangle extends from the 1 dimensional case by requiring that the vector from each point to each other be unique. This means that there can be two points separated by a distance of 2 horizontally, and also two points separated by a distance of 2 vertically.

For this question, it is the scalar distance that must be unique, so there cannot be both a horizontal and a vertical separation of 2. Every solution to this question will be a Golomb rectangle, but not every Golomb rectangle will be a valid solution to this question.

### Upper bounds

Dennis helpfully pointed out in chat that 487 is an upper bound on the score, and gave a proof:

According to my CJam code (619,2m*{2f#:+}%_&,), there are 118800 unique numbers that can be written as the sum of the squares of two integers between 0 and 618 (both inclusive). n pixels require n(n-1)/2 unique distances between each other. For n = 488, that gives 118828.

So there are 118,800 possible different lengths between all the potential pixels in the image, and placing 488 black pixels would result in 118,828 lengths, which makes it impossible for them all to be unique.

I'd be very interested to hear if anyone has a proof of a lower upper bound than this. # Stack Snippet Judge

canvas=document.getElementById('canvas');ctx=canvas.getContext('2d');information=document.getElementById('information');problemList=document.getElementById('problemList');width=canvas.width;height=canvas.height;area=width*height;function verify(){stop();checkPoints();}function checkPoints(){valid=true;numbers=[];points=[];lengths=[];lengthDetails=[];duplicatedLengths=[];clearCanvas();rawData=pointData.value;splitData=rawData.split(' ');for(i=0;i<splitData.length;i++){datum=splitData[i];if(datum!==''){n=parseInt(datum);if(n>=area||n<0){valid=false;information.innerHTML='Out of range:'+n;break;}if(numbers.indexOf(n)>-1){valid=false;information.innerHTML='Duplicated value:'+n;break;}numbers.push(n);x=n%width;y=Math.floor(n/width);points.push([x,y]);}}if(valid){ctx.fillStyle='black';for(i=0;i<points.length-1;i++){p1=points[i];x1=p1;y1=p1;drawPoint(x1,y1);for(j=i+1;j<points.length;j++){p2=points[j];x2=p2;y2=p2;d=distance(x1,y1,x2,y2);duplicate=lengths.indexOf(d);if(duplicate>-1){duplicatedLengths.push(lengthDetails[duplicate]);duplicatedLengths.push([d,[numbers[i],numbers[j]],[x1,y1,x2,y2]]);}lengths.push(d);lengthDetails.push([d,[numbers[i],numbers[j]],[x1,y1,x2,y2]]);}}p=points[points.length-1];x=p;y=p;drawPoint(x,y);if(duplicatedLengths.length>0){ctx.strokeStyle='red';information.innerHTML='Duplicate lengths shown in red';problemTextList=[];for(i=0;i<duplicatedLengths.length;i++){data=duplicatedLengths[i];length=data;numberPair=data;coords=data;line(coords);specificLineText='<b>'+length+': '+numberPair+' to '+numberPair+'</b> [('+coords+', '+coords+') to ('+coords+', '+coords+')]';if(problemTextList.indexOf(specificLineText)===-1){problemTextList.push(specificLineText);}}problemText=problemTextList.join('<br>');problemList.innerHTML=problemText;}else{information.innerHTML='Valid: '+points.length+' points';}}}function clearCanvas(){ctx.fillStyle='white';ctx.fillRect(0,0,width,height);}function line(coords){x1=coords;y1=coords;x2=coords;y2=coords;ctx.beginPath();ctx.moveTo(x1+0.5,y1+0.5);ctx.lineTo(x2+0.5,y2+0.5);ctx.stroke();}function stop(){information.innerHTML='';problemList.innerHTML='';}function distance(x1,y1,x2,y2){xd=x2-x1;yd=y2-y1;return Math.sqrt(xd*xd+yd*yd);}function drawPoint(x,y){ctx.fillRect(x,y,1,1);}
<input id='pointData' placeholder='Paste data here' onblur='verify()'></input><button>Verify data</button><p id='information'></p><p id='problemList'></p><canvas id='canvas' width='619' height='619'></canvas>

• I would have loved to see a Piet answer here – C5H8NNaO4 Jul 5 '15 at 8:46
• @C5H8NNaO4 the competition is open ended - no one is anywhere near an optimal solution so there's plenty of room for new answers... – trichoplax Jul 5 '15 at 13:19
• Since you're offering bounties for both proved upper bound and experimental list of pixels, I assume there is some kind of application to this problem? – Fatalize Jul 5 '15 at 14:50
• @Fatalize not that I'm aware of, but I'd be fascinated to hear of one. The similar problem Costas array has practical applications listed but I've found nothing on this particular problem. – trichoplax Jul 5 '15 at 14:57
• I've been looking at this, and I believe n=487 to be the minimal upper bound on pixels. Out of curiosity, will you accept a proof that there is no lesser upper bound for the bounty? – Mego Oct 15 '15 at 14:12

# Python 3, 135136 137

10 6830 20470 47750 370770 148190 306910 373250 267230 354030 30390 361470 118430 58910 197790 348450 381336 21710 183530 305050 2430 1810 365832 99038 381324 39598 262270 365886 341662 15478 9822 365950 44526 58862 24142 381150 31662 237614 118830 380846 7182 113598 306750 11950 373774 111326 272358 64310 43990 200278 381014 165310 254454 12394 382534 87894 6142 750 382478 15982 298326 70142 186478 152126 367166 1162 23426 341074 7306 76210 140770 163410 211106 207962 35282 165266 300178 120106 336110 30958 158 362758 382894 308754 88434 336918 244502 43502 54990 279910 175966 234054 196910 287284 288468 119040 275084 321268 17968 2332 86064 340044 244604 262436 111188 291868 367695 362739 370781 375723 360261 377565 383109 328689 347879 2415 319421 55707 352897 313831 302079 19051 346775 361293 328481 35445 113997 108547 309243 19439 199037 216463 62273 174471 207197 167695 296927


Found using a greedy algorithm which, at each stage, chooses the valid pixel whose set of distances to the chosen pixels overlaps the least with that of other pixels.

Specifically, the scoring is

score(P) = sum(number of pixels with D in its distance set
for each D in P's distance set)


and the pixel with the lowest score is chosen.

The search is kicked off with the point 10 (i.e. (0, 10)). This part is adjustable, so beginning with different pixels may lead to better or worse results.

It's quite a slow algorithm, so I'm trying to add optimisations/heuristics, and maybe some backtracking. PyPy is recommended for speed.

Anyone trying to come up with an algorithm should test on N = 10, for which I've got 9 (but this took a lot of tweaking and trying different initial points): ## Code

from collections import Counter, defaultdict
import sys
import time

N = 619

start_time = time.time()

def norm(p1, p2):
return (p1//N - p2//N)**2 + (p1%N - p2%N)**2

selected = 
selected_dists = {norm(p1, p2) for p1 in selected for p2 in selected if p1 != p2}
pix2dist = {} # {candidate pixel: {distances to chosen}}
dist2pix = defaultdict(set)

for pixel in range(N*N):
if pixel in selected:
continue

dist_list = [norm(pixel, p) for p in selected]
dist_set = set(dist_list)

if len(dist_set) != len(dist_list) or dist_set & selected_dists:
continue

pix2dist[pixel] = dist_set

for dist in dist_set:

while pix2dist:
best_score = None
best_pixel = None

for pixel in sorted(pix2dist): # Sorting for determinism
score = sum(len(dist2pix[d]) for d in pix2dist[pixel])

if best_score is None or score < best_score:
best_score = score
best_pixel = pixel

del pix2dist[best_pixel]
selected.append(best_pixel)

dist2pix[d].remove(best_pixel)

to_remove = set()
for pixel in pix2dist:
new_dist = norm(pixel, best_pixel)

if (new_dist in selected_dists or new_dist in pix2dist[pixel]
continue

for pixel in to_remove:
for d in pix2dist[pixel]:
dist2pix[d].remove(pixel)

del pix2dist[pixel]

print("Selected: {}, Remaining: {}, Chosen: ({}, {})".format(len(selected), len(pix2dist),
best_pixel//N, best_pixel%N))
sys.stdout.flush()

print(*selected)
print("Time taken:", time.time() - start_time)

• I quickly brute-forced N=10 and there are many distinct layouts with 9 pts but that's the best you can do. – Will Jul 6 '15 at 7:32

# SWI-Prolog, score 131

Barely better than the initial answer, but I guess this will get things started a bit more. The algorithm is the same as the Python answer except for the fact that it tries pixels in an alternate way, starting with the top left pixel (pixel 0), then the bottom right pixel (pixel 383160), then pixel 1, then pixel 383159, etc.

a(Z) :-
N = 619,
build_list(N,Z).

build_list(N,R) :-
M is N*N,
get_list([M,-1],[],L),
reverse(L,O),
build_list(N,O,[],[],R).

get_list([A,B|C],R,Z) :-
X is A - 1,
Y is B + 1,
(X =< Y,
Z = R
;
get_list([X,Y,A,B|C],[X,Y|R],Z)).

build_list(_,[],R,_,R) :- !.
build_list(N,[A|T],R,W,Z) :-
separated_pixel(N,A,R,W,S),
is_set(S),
flatten([W|S],V),!,
build_list(N,T,[A|R],V,Z)
;build_list(N,T,R,W,Z).

separated_pixel(N,A,L,W,R) :-
separated_pixel(N,A,L,[],W,R).

separated_pixel(N,A,[A|T],R,W,S) :-
separated_pixel(N,A,T,R,W,S).

separated_pixel(N,A,[B|T],R,W,S) :-
X is (A mod N - B mod N)*(A mod N - B mod N),
Y is (A//N - B//N)*(A//N - B//N),
Z is X + Y,
\+member(Z,W),
separated_pixel(N,A,T,[Z|R],W,S).

separated_pixel(_,_,[],R,_,R).


### Input:

a(A).

### Output:

Z = [202089, 180052, 170398, 166825, 235399, 138306, 126354, 261759, 119490, 117393, 281623, 95521, 290446, 299681, 304310, 78491, 314776, 63618, 321423, 60433, 323679, 52092, 331836, 335753, 46989, 40402, 343753, 345805, 36352, 350309, 32701, 32470, 352329, 30256, 28089, 357859, 23290, 360097, 22534, 362132, 20985, 364217, 365098, 17311, 365995, 15965, 15156, 368487, 370980, 371251, 11713, 372078, 372337, 10316, 373699, 8893, 374417, 8313, 7849, 7586, 7289, 6922, 376588, 6121, 5831, 377399, 377639, 4941, 378494, 4490, 379179, 3848, 379453, 3521, 3420, 379963, 380033, 3017, 380409, 2579, 380636, 2450, 2221, 2006, 381235, 1875, 381369, 381442, 381682, 1422, 381784, 1268, 381918, 1087, 382144, 382260, 833, 382399, 697, 382520, 622, 382584, 382647, 382772, 384, 382806, 319, 286, 382915, 382939, 190, 172, 383005, 128, 383050, 93, 383076, 68, 383099, 52, 40, 383131, 21, 383145, 10, 383153, 4, 383158, 1, 383160, 0]


### Image from Stack Snippet • Since there's a theoretical maximum of 487, even an incremental increase is significant... – trichoplax Jul 2 '15 at 13:35
• Did your output as shown work with the Stack Snippet? I had specified space separated (as in my example answer) but the main reason for that was so that the Stack Snippet would work. – trichoplax Jul 2 '15 at 13:42
• @trichoplax Yeah it's a typo, I start with pixel 0, I'll fix it. To get the image I selected the part of the output between the two square brackets, and removed all commas. The Stack snippet seems to work with comma-separated pixels, though. – Fatalize Jul 2 '15 at 13:49

My inspiration was the Sieve of Eratosthenes and in particular The Genuine Sieve of Eratosthenes, a paper by Melissa E. O'Neill of Harvey Mudd College. My original version (which considered points in index order) sieved points extremely quickly, for some reason I can't recall I decided to shuffle the points before “sieving” them in this version (I think solely to make generating different answers easier by using a new seed in the random generator). Because the points are no longer in any sort of order there's not really any sieving going on anymore, and as a result it takes a couple minutes just to produce this single 115 point answer. A knockout Vector would probably be a better choice now.

So with this version as a checkpoint I see two branches, returning to the “Genuine Sieve” algorithm and leveraging the List monad for choice, or swapping out the Set operations for equivalents on Vector.

Edit: So for working version two I veered back toward the sieve algorithm, improved the generation of “multiples” (knocking indices out by finding points at integer coordinates on circles with radius equal to the distance between any two points, akin to generating prime multiples) and making a few constant time improvements by avoiding some needless recalculation.

For some reason I can't recompile with profiling turned on, but I believe that the major bottleneck now is backtracking. I think exploring a bit of parallelism and concurrency will produce linear speedups, but memory exhaustion will probably cap me at a 2x improvement.

Edit: Version 3 meandered a bit, I first experimented with a heuristic in taking the next 𝐧 indices (after sieving from earlier choices) and choosing the one that produced the next minimum knockout set. This ended up being far too slow, so I went back to a whole-searchspace brute force method. An idea to order the points by distance from some origin came to me, and led to an improvement by one single point (in the time my patience lasted). This version picks index 0 as the origin, it may be worth trying the center point of the plane.

Edit: I picked up 4 points by re-ordering the search space to prioritize the most distant points from the center. If you're testing my code, 135 136 is actually the second third solution found. Fast edit: This version seems the most likely to keep being productive if left running. I suspect I may tie at 137, then run out of patience waiting for 138.

One thing I noticed (that may be of help to someone) is that if you set the point ordering from the center of the plane (i.e., remove (d*d -) from originDistance) the image formed looks a bit like a sparse prime spiral.

{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE BangPatterns #-}

module Main where

import Data.Function (on)
import Data.List     (tails, sortBy)
import Data.Maybe    (fromJust)
import Data.Ratio
import Data.Set      (fromList, toList, union, difference, member)

import System.IO

sideLength :: Int
sideLength = 619

data Point = Point {  x :: !Int,  y :: !Int } deriving (Ord, Eq)
data Delta = Delta { da :: !Int, db :: !Int }

euclidean :: Delta -> Int
euclidean Delta{..} = da*da + db*db

instance Eq Delta where
(==) = (==) on euclidean

instance Ord Delta where
compare = compare on euclidean

delta :: Point -> Point -> Delta
delta a b = Delta (min dx dy) (max dx dy)
where
dx = abs (x a - x b)
dy = abs (y a - y b)

equidistant :: Dimension -> Point -> Point -> [Point]
equidistant d a b =
let
(dx, dy) = (x a - x b, y a - y b)
m = if dx == 0 then Nothing else Just (dy % dx)                    -- Slope
w = if dy == 0 then Nothing else Just $maybe 0 (negate . recip) m -- Negative reciprocal justW = fromJust w -- Moral bankruptcy (px, py) = ((x a + x b) % 2, (y a + y b) % 2) -- Midpoint b0 = py - (justW * px) -- Y-intercept f q = justW * q + b0 -- Perpendicular bisector in maybe (if denominator px == 1 then map (Point (numerator px)) [0..d - 1] else []) ( map (\q -> Point q (numerator . f . fromIntegral$ q))
. filter ((== 1) . denominator . f . fromIntegral)
)
(w >> return [0..d - 1])

circle :: Dimension -> Point -> Delta -> [Point]
circle d p delta' =
let
square = (^(2 :: Int))
hypoteneuse = euclidean delta'
candidates = takeWhile ((<= hypoteneuse) . square) [0..d - 1]
candidatesSet = fromList $map square [0..d - 1] legs = filter ((member candidatesSet) . (hypoteneuse -) . square) candidates pythagoreans = zipWith Delta legs$ map (\l -> floor . sqrt . (fromIntegral :: Int -> Double) $hypoteneuse - square l) legs in toList . fromList$ concatMap (knight p) pythagoreans

knight :: Point -> Delta -> [Point]
knight Point{..} Delta{..} =
[ Point (x + da) (y - db), Point (x + da) (y + db)
, Point (x + db) (y - da), Point (x + db) (y + da)
, Point (x - da) (y - db), Point (x - da) (y + db)
, Point (x - db) (y - da), Point (x - db) (y + da)
]

type Dimension = Int
type Index = Int

index :: Dimension -> Point -> Index
index d Point{..} = y * d + x

point :: Dimension -> Index -> Point
point d i = Point (i rem d) (i div d)

valid :: Dimension -> Point -> Bool
valid d Point{..} = 0 <= x && x < d
&& 0 <= y && y < d

isLT :: Ordering -> Bool
isLT LT = True
isLT _  = False

sieve :: Dimension -> [[Point]]
sieve d = [i0 : sieve' is0 [i0] [] | (i0:is0) <- tails . sortBy originDistance . map (point d) $[0..d*d - 1]] where originDistance :: Point -> Point -> Ordering originDistance = compare on ((d*d -) . euclidean . delta (point d (d*d div 2))) sieve' :: [Point] -> [Point] -> [Delta] -> [Point] sieve' [] _ _ = [] sieve' (i:is) ps ds = i : sieve' is' (i:ps) ds' where ds' = map (delta i) ps ++ ds knockouts = fromList [k | d' <- ds , k <- circle d i d' , valid d k , not . isLT$ k originDistance i
]
union fromList [k | q  <- i : ps
, d' <- map (delta i) ps
, k  <- circle d q d'
, valid d k
, not . isLT $k originDistance i ] union fromList [e | q <- ps , e <- equidistant d i q , valid d e , not . isLT$ e originDistance i
]
is' = sortBy originDistance . toList $fromList is difference knockouts main :: IO () main = do let answers = strictlyIncreasingLength . map (map (index sideLength))$ sieve sideLength
hSetBuffering stdout LineBuffering
mapM_ (putStrLn . unwords . map show) $answers where strictlyIncreasingLength :: [[a]] -> [[a]] strictlyIncreasingLength = go 0 where go _ [] = [] go n (x:xs) = if n < length x then x : go (length x) xs else go n xs  ## Output 1237 381923 382543 382541 1238 1857 380066 5 380687 378828 611 5571 382553 377587 375113 3705 8664 376356 602 1253 381942 370161 12376 15475 7413 383131 367691 380092 376373 362114 36 4921 368291 19180 382503 26617 3052 359029 353451 29716 382596 372674 352203 8091 25395 12959 382479 381987 35894 346031 1166 371346 336118 48276 2555 332400 46433 29675 380597 13066 382019 1138 339859 368230 29142 58174 315070 326847 56345 337940 2590 382663 320627 70553 19278 7309 82942 84804 64399 5707 461 286598 363864 292161 89126 371267 377122 270502 109556 263694 43864 382957 824 303886 248218 18417 347372 282290 144227 354820 382909 380301 382808 334361 375341 2197 260623 222212 196214 231526 177637 29884 251280 366739 39442 143568 132420 334718 160894 353132 78125 306866 140600 297272 54150 240054 98840 219257 189278 94968 226987 265881 180959 142006 218763 214475  • Impressive improvements. You have 2 hours left to get to 138 before the bounty is assigned. Nice work either way... – trichoplax Jul 12 '15 at 19:34 • It's looking unlikely that I'll meet that goal, I still haven't managed to generate a 137 element set. I think this method is probably tapped... – R B Jul 12 '15 at 20:14 • Interesting that two different answers with different approaches are hitting a maximum around the same size. – trichoplax Jul 12 '15 at 20:54 • I think the upper bound is probably fairly close. Consider an infinite plane and any two points. The optimal placement of those points with any distance d minimizes the number of other points excluded from consideration by tracing circles of radius d with centers of both chosen points, where the perimeter only touches three other integer coordinates (at 90, 180, and 270 degree turns about the circle), and the perpendicular bisecting line crossing no integer coordinates. So each new point n+1 will exclude 6n other points from consideration (with optimal choice). – R B Jul 12 '15 at 21:14 # Python 3, score 129 This is an example answer to get things started. Just a naive approach going through the pixels in order and choosing the first pixel that doesn't cause a duplicate separation distance, until the pixels run out. ### Code width = 619 height = 619 area = width * height currentAttempt = 0 temporaryLengths = [] lengths = [] points = [] pixels = [] for i in range(area): pixels.append(0) def generate_points(): global lengths while True: candidate = vacantPixel() if isUnique(candidate): lengths += temporaryLengths pixels[candidate] = 1 points.append(candidate) print(candidate) if currentAttempt == area: break filename = 'uniquely-separated-points.txt' with open(filename, 'w') as file: file.write(' '.join(points)) def isUnique(n): x = n % width y = int(n / width) temporaryLengths[:] = [] for i in range(len(points)): point = points[i] a = point % width b = int(point / width) d = distance(x, y, a, b) if d in lengths or d in temporaryLengths: return False temporaryLengths.append(d) return True def distance(x1, y1, x2, y2): xd = x2 - x1 yd = y2 - y1 return (xd*xd + yd*yd) ** 0.5 def vacantPixel(): global currentAttempt while True: n = currentAttempt currentAttempt += 1 if pixels[n] == 0: break return n generate_points()  ### Output 0 1 3 7 12 20 30 44 65 80 96 122 147 181 203 251 289 360 400 474 564 592 627 660 747 890 1002 1155 1289 1417 1701 1789 1895 2101 2162 2560 2609 3085 3121 3331 3607 4009 4084 4242 4495 5374 5695 6424 6762 6808 7250 8026 8356 9001 9694 10098 11625 12881 13730 14778 15321 16091 16498 18507 19744 20163 20895 23179 25336 27397 31366 32512 33415 33949 39242 41075 46730 47394 48377 59911 61256 66285 69786 73684 79197 89530 95447 102317 107717 111751 116167 123198 126807 130541 149163 149885 154285 159655 163397 173667 173872 176305 189079 195987 206740 209329 214653 220911 230561 240814 249310 269071 274262 276855 285295 305962 306385 306515 312310 314505 324368 328071 348061 350671 351971 354092 361387 369933 376153  ### Image from Stack Snippet # Python 3, 130 For comparison, here's a recursive backtracker implementation: N = 619 def norm(p1, p2): return (p1//N - p2//N)**2 + (p1%N - p2%N)**2 def solve(selected, dists): global best if len(selected) > best: print(len(selected), "|", *selected) best = len(selected) for pixel in (range(selected[-1]+1, N*N) if selected else range((N+1)//2+1)): # By symmetry, place first pixel in first half of top row added_dists = [norm(pixel, p) for p in selected] added_set = set(added_dists) if len(added_set) != len(added_dists) or added_set & dists: continue selected.append(pixel) dists |= added_set solve(selected, dists) selected.pop() dists -= added_set print("N =", N) best = 0 selected = [] dists = set() solve(selected, dists)  It finds the following 130 pixel solution quickly before it starts choking: 0 1 3 7 12 20 30 44 65 80 96 122 147 181 203 251 289 360 400 474 564 592 627 660 747 890 1002 1155 1289 1417 1701 1789 1895 2101 2162 2560 2609 3085 3121 3331 3607 4009 4084 4242 4495 5374 5695 6424 6762 6808 7250 8026 8356 9001 9694 10098 11625 12881 13730 14778 15321 16091 16498 18507 19744 20163 20895 23179 25336 27397 31366 32512 33415 33949 39242 41075 46730 47394 48377 59911 61256 66285 69786 73684 79197 89530 95447 102317 107717 111751 116167 123198 126807 130541 149163 149885 154285 159655 163397 173667 173872 176305 189079 195987 206740 209329 214653 220911 230561 240814 249310 269071 274262 276855 285295 305962 306385 306515 312310 314505 324368 328071 348061 350671 351971 354092 361387 371800 376153 378169  More importantly, I'm using it to check solutions for small cases. For N <= 8, the optimal are: 1: 1 (0) 2: 2 (0 1) 3: 3 (0 1 5) 4: 4 (0 1 6 12) 5: 5 (0 1 4 11 23) 6: 6 (0 1 9 23 32 35) 7: 7 (0 2 9 20 21 40 48) 8: 7 (0 1 3 12 22 56 61) 9: 8 (0 1 3 8 15 37 62 77) 10: 9 (0 1 7 12 30 53 69 80 89)  Listed in brackets are the first lexicographical optimals. Unconfirmed: 11: 10 (0 2 3 7 21 59 66 95 107 120) 12: 10 (0 1 3 7 33 44 78 121 130 140)  # Scala, 132 Scans left-to-right and top-to-bottom like the naive solution, but tries starting at different pixel locations. import math.pow import math.sqrt val height, width = 619 val area = height * width case class Point(x: Int, y: Int) def generate(n: Int): Set[Point] = { def distance(p: Point, q: Point) = { def square(x: Int) = x * x sqrt(square(q.x - p.x) + square(q.y - p.y)) } def hasDuplicates(s: Seq[_]) = s.toSet.size != s.size def rotate(s: Vector[Point]): Vector[Point] = s.drop(n) ++ s.take(n) val remaining: Vector[Point] = rotate((for (y <- 0 until height; x <- 0 until width) yield { Point(x, y) }).toVector) var unique = Set.empty[Point] var distances = Set.empty[Double] for (candidate <- remaining) { if (!unique.exists(p => distances.contains(distance(candidate, p)))) { val candidateDistances = unique.toSeq.map(p => distance(candidate, p)) if (!hasDuplicates(candidateDistances)) { unique = unique + candidate distances = distances ++ candidateDistances } } } unique } def print(s: Set[Point]) = { def toRowMajor(p: Point) = p.y*height + p.x println(bestPixels.map(toRowMajor).toSeq.sorted.mkString(" ")) } var bestPixels = Set.empty[Point] for (n <- 0 until area) { val pixels = generate(n) if (pixels.size > bestPixels.size) bestPixels = pixels } print(bestPixels)  # Output 302 303 305 309 314 322 332 346 367 382 398 424 449 483 505 553 591 619 647 680 719 813 862 945 1014 1247 1459 1700 1740 1811 1861 1979 2301 2511 2681 2913 3114 3262 3368 4253 4483 4608 4753 5202 5522 5760 6246 6474 6579 6795 7498 8062 8573 8664 9903 10023 10567 10790 11136 12000 14153 15908 17314 17507 19331 20563 20941 22339 25131 26454 28475 31656 38328 39226 40214 50838 53240 56316 60690 61745 62374 68522 71208 78598 80204 86005 89218 93388 101623 112924 115702 118324 123874 132852 136186 139775 144948 154274 159730 182200 193642 203150 203616 213145 214149 218519 219744 226729 240795 243327 261196 262036 271094 278680 282306 289651 303297 311298 315371 318124 321962 330614 336472 343091 346698 354881 359476 361983 366972 369552 380486 382491  • Just getting the ball rolling... – Dave Swartz Jul 6 '15 at 2:59 # Python, 134 132 Here's a simple one that randomly culls some of the search space to cover a greater area. It iterates the points in distance from an origin order. It skips points that are same distance from origin, and early-outs if it cannot improve on the best. It runs indefinitely. from random import * from bisect import * W = H = 619 pts = [] deepest = 0 lengths = set() def place(x, y): global lengths pos = (x, y) for px, py in pts: dist = (x-px)*(x-px) + (y-py)*(y-py) if dist in lengths: return False dists = set((x-px)*(x-px) + (y-py)*(y-py) for px, py in pts) if len(dists) != len(pts): return False lengths |= dists pts.append(pos) return True def unplace(): x, y = pos = pts.pop() for px, py in pts: dist = (x-px)*(x-px) + (y-py)*(y-py) lengths.remove(dist) def walk(i): global deepest, backtrack depth = len(pts) while i < W*H: d, x, y, rem = order[i] if rem+depth <= deepest: # early out if remaining unique distances mean we can't improve return i += 1 if place(x, y): j = i while j < W*H and order[j] == d: # skip those the same distance from origin j += 1 walk(j) unplace() if backtrack <= depth: break if not randint(0, 5): # time to give up and explore elsewhere? backtrack = randint(0, len(pts)) break backtrack = W*H # remove restriction if depth >= deepest: deepest = depth print (ox, oy), depth, "=", " ".join(str(y*W+x) for x, y in pts) try: primes = (0,1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97) while True: backtrack = W*H ox, oy = choice(primes), choice(primes) # random origin coordinates order = sorted((float((ox-x)**2+(oy-y)**2)+random(), x, y) for x in xrange(W) for y in xrange(H)) rem = sorted(set(int(o) for o in order)) # ordered list of unique distances rem = {r: len(rem)-bisect_right(rem, r) for r in rem} # for each unique distance, how many remain? order = tuple((int(d), x, y, rem[int(d)]) for i, (d, x, y) in enumerate(order)) walk(0) except KeyboardInterrupt: print  It quickly finds solutions with 134 points: 3097 3098 2477 4333 3101 5576 1247 9 8666 8058 12381 1257 6209 15488 6837 21674 19212 26000 24783 1281 29728 33436 6863 37767 26665 14297 4402 43363 50144 52624 18651 9996 58840 42792 6295 69950 48985 34153 10644 72481 83576 63850 29233 94735 74997 60173 43477 101533 102175 24935 113313 88637 122569 11956 36098 79401 61471 135610 31796 4570 150418 57797 4581 125201 151128 115936 165898 127697 162290 33091 20098 189414 187620 186440 91290 206766 35619 69033 351 186511 129058 228458 69065 226046 210035 235925 164324 18967 254416 130970 17753 248978 57376 276798 456 283541 293423 257747 204626 298427 249115 21544 95185 231226 54354 104483 280665 518 147181 318363 1793 248609 82260 52568 365227 361603 346849 331462 69310 90988 341446 229599 277828 382837 339014 323612 365040 269883 307597 374347 316282 354625 339821 372016 For the curious, here are some brute-forced small N: 3 = 0 2 3 4 = 0 2 4 7 5 = 0 2 5 17 23 6 = 0 12 21 28 29 30 7 = 4 6 11 14 27 36 42 8 = 0 2 8 11 42 55 56 9 = 0 2 9 12 26 50 63 71 10 = 0 2 7 10 35 75 86 89 93 11 = 0 23 31 65 66 75 77 95 114 117  • Have you tried running this through PyPy? – trichoplax Jul 3 '15 at 16:42 • @trichoplax I always run these hobby things on both pypy and cpython, and if cpython is faster I lodge tickets on pypy. In this particular case, pypy is quite a bit faster than cpython and that's how I got these numbers :) – Will Jul 4 '15 at 12:26 • I'm interested, what does "quickly" entail? – Cain Jul 9 '15 at 21:03 • @Cain 'quickly' was about 5 mins iirc – Will Jul 10 '15 at 23:03 ## Fantom 96 I used an evolution algorithm, basically add k random points at a time, do that for j different random sets, then pick the best one and repeat. Pretty terrible answer right now, but that's running it with only 2 children per generation for the sake of speed, which is nearly just random. Gonna play with the parameters a bit to see how it goes, and I probably need a better scoring function than number of free spots left. class Pixel { static const Int n := 619 static const Int stepSize := 20 static const Int generationSize := 5 static const |Int, Int -> Int| d := |Int x, Int y -> Int| { d1 := x%n - y%n d2 := x/n - y/n return d1.pow(2) + d2.pow(2) } public static Void main(){ //Initialize [Int: Int[][]] disMap := [:] Int[] freeSpots := (0..<n*n).toList Int[] pixels := [,] Int[] distances := [,] genNum := 0 children := [,] while(freeSpots.size > 0){ echo("Generation:${genNum++} \t Spots Left: ${freeSpots.size} \t Pixels added:$pixels.size \t Distances used: $distances.size uniqueDistances:$distances.unique.size" )
echo(distances)
echo("Pixels: " + pixels.join(" "))
//echo("Distances: $distances") //Generate children children = [,] generationSize.times{ //echo("\tStarting child$it")
i := Int.random(0..<freeSpots.size)
childFreeSpots := freeSpots.dup
childPixels := pixels.dup
childDistances := distances.dup

for(Int step := 0; step < stepSize; step++){

if( i < childFreeSpots.size){
//Choose a pixel
pixel := childFreeSpots.removeAt(i)
//echo("\t\tAdding pixel $pixel") //Remove neighbors that are the new distances away ///Find distances newDis := [,] childPixels.each { newDis.add(d(pixel, it)) } //Check that there are no equal distances if(newDis.size != newDis.unique.size) continue //Remove neighbors childPixels.each | Int childPixel|{ newDis.each |Int dis|{ neighbors := getNeighbors(childPixel, dis, disMap) neighbors.each| Int n |{ index := childFreeSpots.binarySearch(n) if(index >= 0) childFreeSpots.removeAt(index) } } } //echo("Removed neighbors:$test")
//Remove all the neighbors of new pixel
childDistances.each|Int dis| {
neighbors := getNeighbors(pixel, dis, disMap)
childFreeSpots.removeAll(neighbors)
}

}
}
echo("\tChild $it: pixels:$childPixels.size \t distances: $childDistances.size \t freeSpots:$childFreeSpots.size")
}

//Score children and keep best one as new parent
Obj?[][] parent := children.max |Int[][] a, Int[][] b -> Int| { return (a.last.size  + a.first.size*10000) <=> (b.last.size + b.first.size*10000)  }
pixels = parent.first
distances = parent
freeSpots = parent.last

}//End while

//Return result
echo("Size: " + pixels.size)
echo(pixels.join(" "))

}

private static Bool checkValid(Int[] pixels){
distances := [,]
pixels[0..-2].each|Int p, Int i|{
for(Int j := i + 1; j < pixels.size; j++){
}
}
if(distances.size > distances.unique.size){
echo("Duplicate distance found!!!!")
echo("Pixel \$pixels.last is not valid")
return false
}
return true
}

public static Int[] getNeighbors(Int spot, Int distance, [Int : Int[][]] disMap ){
result := [,]
//Check hash map
pairs := disMap.get(distance, null)

//Find possible int pairs if not already in the map
if(pairs == null){
for(Int i := 0; i*i <= distance; i++ ){
for(Int j := i; j*j + i*i <= distance; j++){
if(i.pow(2) + j.pow(2) == distance){
}
}
}
}

pairs.each|Int[] pair|{
//Find neighbors with pair
x := pair.first
y := pair.last
2.times{
//Positive x

//negative x

//Swap x and y and repeat
temp := x
x = y
y = temp
}
}

return result.findAll |Int i -> Bool| { i >= 0 }.unique
}

}


Output

17595 17596 17601 17627 17670 17726 17778 17861 17956 18117 18324 18733 19145 19597 20244 21139 21857 22742 24078 25343 28577 30152 32027 34406 37008 39864 42313 44820 48049 52193 55496 59707 64551 69976 74152 79758 84392 91782 98996 104625 150212 158877 169579 178660 189201 201343 213643 225998 238177 251012 263553 276797 290790 304915 319247 332702 347266 359665 373683 125899 144678 170677 195503 220092 244336 269861 289473 308633 326736 343756 358781 374280 131880 172485 212011 245015 277131 302055 321747 347911 363717 379166 249798 284200 313870 331913 360712 378024 9704 141872 249686 293656 357038 357596 370392 381963

• Oh wow, you're right, I'm sorry. Hmm, must have not copied all of it early when I tested. I will fix whatever is going on and respond with an update – Cain Jul 9 '15 at 14:26
• Ahh, I figured it out, when adding a new pixel, I wasn't checking that it's not equidistant from two other pixels – Cain Jul 9 '15 at 19:53
• Fixed it, but it really sucks now, I think I might be accidentally finding a worst solution instead of best one – Cain Jul 9 '15 at 21:54
• At least it works now, so you can tweak the parameters and see if you can improve the result. Great to see another new approach. +1 – trichoplax Jul 10 '15 at 7:21

# Python 3, 119

I no longer remember why I named this function mc_usp, though I suspect it had something to do with Markov chains. Here I publish my code that I ran with PyPy for about 7 hours. The program tries to build up 100 different sets of pixels by randomly picking pixels until it has checked every pixel in the image, and returning one of the best sets.

On another note, at some point, we should really attempt to find an upper bound for N=619 that's better than 488, because judging from the answers on here, that number is way too high. Rowan Blush's comment about how every new point n+1 can potentially remove 6*n points with optimal choice seemed like a good idea. Unfortunately, upon inspection of the formula a(1) = 1; a(n+1) = a(n) + 6*n + 1, where a(n) is the number of points removed after adding n points to our set, this idea may not be the best fit. Checking when a(n) is greater than N**2, a(200) being larger than 619**2 seems promising, but the a(n) larger than 10**2 is a(7) and we've proved that 9 is the actual upper bound for N=10. I'll keep you posted as I try to look a better upper bound, but any suggestions are welcome.

Onto my answer. First, my set of 119 pixels.

15092 27213 294010 340676 353925 187345 127347 21039 28187 4607 23476 324112 375223 174798 246025 185935 186668 138651 273347 318338 175447 316166 158342 97442 361309 251283 29986 98029 339602 292202 304041 353401 236737 324696 42096 102574 357602 66845 40159 57866 3291 24583 254208 357748 304592 86863 19270 228963 87315 355845 55101 282039 83682 55643 292167 268632 118162 48494 378303 128634 117583 841 178939 20941 161231 247142 110205 211040 90946 170124 362592 327093 336321 291050 29880 279825 212675 138043 344012 187576 168354 28193 331713 329875 321927 129452 163450 1949 186448 50734 14422 3761 322400 318075 77824 36391 31016 33491 360713 352240 45316 79905 376004 310778 382640 383077 359178 14245 275451 362125 268047 23437 239772 299047 294065 46335 112345 382617 79986


Second, my code, which randomly picks a starting point from an octant of the 619x619 square (since the starting point is otherwise equal under rotation and reflection) and then every other point from the rest of the square.

import random
import time

start_time = time.time()
print(start_time)

def mc_usp_v3(N, z, k=100, m=1.0):
"""
At m=1.0, it keeps randomly picking points until we've checked every point. Oh dear.
"""
ceil = -(-N//2)
a=random.randint(0,ceil)
b=random.randint(a,ceil)
r=[a*N+b]

best_overall = r[:]
all_best = []
best_in_shuffle = r[:]
num_shuffles = 0
num_missteps = 0
len_best = 1

while num_shuffles < k and len(best_overall) < z:
dist = []
missteps = []
points_left = list(range(N*N))
points_left.remove(r)

while len_best + num_missteps < m*N*N and len(points_left):
index = random.randint(0, len(points_left)-1)
point = points_left[index]
points_left.pop(index)
dist, better = euclid(r, point, dist, N)

if better and len(r) + 1 > len_best:
r.append(point)
best_in_shuffle = r[:]
len_best += 1
else:
missteps.append(point)
num_missteps += 1

else:
print(num_shuffles, len(best_overall), len_best, num_missteps, time.time() - start_time)

num_shuffles += 1
num_missteps = 0
missteps = []

if len(best_in_shuffle) == len(best_overall):
all_best.append(best_in_shuffle)
print(best_in_shuffle)

if len(best_in_shuffle) > len(best_overall):
best_overall = best_in_shuffle[:]
all_best = [best_overall]
print(best_overall)
a=random.randint(0,ceil)
b=random.randint(a,ceil)
r=[a*N+b]
best_in_shuffle = r[:]
len_best = 1
return len(best_overall), all_best

def euclid(point_set, new_point, dist, N):
new_dist = []
unique = True
a,b=divmod(new_point, N)
for point in point_set:
c,d=divmod(point, N)
current_dist = (a-c)**2+(b-d)**2
if current_dist in dist or current_dist in new_dist:
unique = False
break
new_dist.append(current_dist)
if unique:
dist += new_dist
return dist, unique

def mcusp_format(mcusp_results):
length, all_best = mcusp_results
return " ".join(str(i) for i in all_best)

print(mcusp_format(mc_usp_v3(10, 20, 100, 1.0)))
print(mcusp_format(mc_usp_v3(619, 488, 100, 1.0)))
print(time.time()-start_time)