# Output all the different counts of points

Consider a 10 by 10 grid of points at integer coordinates. Given a coordinate (x, y) of one of the points in the grid and a radius r, some of the points in the grid will fall inside a circle of radius r that is centered at (x, y). You will count the number of such points.

# Input

An integer coordinate (x, y) in a 10 by 10 grid.

# Output

All the different counts for the number of points in the grid that are inside a circle centered at (x, y), for every possible radius r. Your output should not contain duplicate values but can be in any order.

You may not assume that r is an integer so there are an infinite number of possible radii but your output will nonetheless be of finite size. For example, the same count arises for all 0<r<1.

The numbers 1 and 100 will always be in your output no matter what (x, y) are.

If (x, y) is a corner, the output should be:

[1, 3, 4, 6, 8, 9, 11, 13, 15, 17, 19, 20, 22, 26, 28, 30, 31, 33, 35, 37, 39, 41, 43, 45, 48, 50, 52, 54, 56, 58, 62, 64, 65, 67, 69, 71, 73, 75, 79, 81, 83, 85, 86, 88, 90, 92, 94, 95, 97, 99, 100]


If (x, y) is on a side and not too close to a corner then the output should include 1, 4, 6, 9, 13, 15, 18, 22, 26.

If (x, y) is somewhere in the middle then your output should include 1, 5, 9, 13, 21, 25, 29. For example if it is (4, 3) the output should be [1, 5, 9, 13, 21, 25, 29, 37, 45, 48, 54, 58, 64, 72, 76, 80, 82, 86, 87, 89, 91, 94, 96, 97, 99, 100].

• can you add at least 5 test cases please? Commented Dec 29, 2023 at 20:06
• Can we take input as a complex number?
– Tbw
Commented Dec 29, 2023 at 22:06
• @Tbw Yes you can.
– Simd
Commented Dec 30, 2023 at 4:40

# Vyxal 3, 12 bytes

₀ʀ2ẋᵗϩİ?ȧS†@


10x10 grid located in coordinates 0-9. Takes input as a complex number.

Try it Online!

Explanation

₀ʀ2ẋᵗϩİ?ȧS†@­⁡​‎‎⁪⁡⁪⁠⁪⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁢⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁢⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁡⁪‏‏​⁡⁠⁡‌⁢⁡​‎‎⁪⁡⁪⁠⁪⁣⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁣⁪‏‏​⁡⁠⁡‌⁢⁢​‎‎⁪⁡⁪⁠⁪⁣⁤⁪‏‏​⁡⁠⁡‌­
2ẋ          # ‎⁡2nd cartesian power of
₀ʀ            # ‎⁢‎⁢Range 0-9
ᵗϩ        # ‎⁣Map through all coordinates and dump to stack
İ?ȧ     # ‎⁤Absolute difference between input and coordinate
S†   # ‎⁢⁡‎⁢⁡Sort list and return length of consecutive groups
@  # ‎⁢⁢Cumulatively add all lengths
💎


₀ʀ2ẋƛ?-²∑}S†@­⁡​‎‎⁪⁡⁪⁠⁪⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁪‏⁠⁪⁪⁠⁪⁪‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁢⁡⁪‏⁠⁪⁪⁠‎⁪⁡⁪⁠⁪⁣⁣⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁢⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁢⁪‏‏​⁡⁠⁡‌⁢⁡​‎‎⁪⁡⁪⁠⁪⁣⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁡⁪‏‏​⁡⁠⁡‌⁢⁢​‎‎⁪⁡⁪⁠⁪⁤⁢⁪‏‏​⁡⁠⁡‌­
2ẋ            # ‎⁡2nd cartesian power of
₀ʀ              # ‎⁢Range 0-9
ƛ    }      # ‎⁣Map through all coordinates
?-²∑       # Squared euclidean distance‎⁤ between input and coordinate
S†    # ‎⁢⁡Sort list and return length of consecutive groups
@   # ‎⁢⁢Cumulatively add all lengths
💎


Created with the help of Luminespire.

• Very nice method
– Simd
Commented Dec 29, 2023 at 21:19
• Maybe write "Explanation created..." instead of "Created..."? Commented Dec 29, 2023 at 21:36
• That's just the default Luminespire text.
– Tbw
Commented Dec 29, 2023 at 22:36

# Wolfram Language (Mathematica), 101 bytes

Union@Table[p=#;Length@Select[Join@@Array[{#,#2}&,{10,10},0],p~EuclideanDistance~#<r&],{r,1,20,.01}]&


Try it online!

# Jelly, 10 bytes

⁵pÆịạ⁸ĠẈÄ


Try it online!

A monadic link taking a co-ordinate on a grid from 1..10 on both axes and expressed as a complex number. A list of integers is returned.

## Explanation

⁵p        | Cartesian product of 1..10 with itself
Æị      | Convert to complex
ạ⁸    | Absolute difference with link argument
Ġ   | Indices to group identical values (ordered by the values)
Ẉ  | Group lengths
Ä | Cumulative sum


# Wolfram Language (Mathematica), 57 bytes

i+=#2&@@@Tally@Sort@{Array[# #+#2#2&,10+{i=0,0},-#,##&]}&


Try it online!

                     Array[# #+#2#2&,10+{  0,0},-#,   ]     distance to each point
Sort@{                              ##& }    ascending by distance
#2&@@@Tally@                                             counts of each
i+=  &@@@                                i=                 cumulative


# Desmos, 75 bytes

f(p)=[L[L<=i].countfori=L].unique
I=[0...9]
L=[distance(p,(a,b))fora=I,b=I]


Input as a point (x,y), ranging between coordinates 0 to 9.

Try It On Desmos!

Try It On Desmos! - Prettified

# R, 47 bytes

\(x)cumsum(table(abs(x-outer(0:9,0:9*1i,+))))


Attempt This Online!

A function taking a complex argument and returning a list of integers. Uses a zero-indexed grid from 0..9 on both axes.

# TI-BASIC, 73 bytes

abs(Ans-seq(10fPart(.1I)𝒾+int(.1I),I,0,99→L
SortA(ʟL
ΔList(augment(ʟL,{0
cumSum(1 or Ans)not(not(Ans→L
SortD(ʟL
seq(ʟL(I),I,1,sum(1 and ʟL


Takes input in Ans as a complex number with the x and y from 0 to 9.

# APL(Dyalog Unicode), 3227 bytes SBCS

{⍸⌽≠{⍵[⍒⍵]},+.×⍨⍤-∘⍵¨⍳2⍴10}


Try it on APLgolf!

-5 bytes thanks to att.

A dfn which takes a vector on the right and outputs the list of possible counts.

## Old explanation

{+\{≢⍵}⌸{⍵[⍋⍵]},∘.(+.×⍨⍵-,)⍨⍳10}­⁡​‎‎⁪⁡⁪⁠⁪⁢⁤⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁣⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁢⁡⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁤⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁢⁣⁢⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁢⁢⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁡⁪‏‏​⁡⁠⁡‌⁢⁡​‎‎⁪⁡⁪⁠⁪⁢⁡⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁣⁪‏‏​⁡⁠⁡‌⁢⁢​‎‎⁪⁡⁪⁠⁪⁤⁤⁪‏‏​⁡⁠⁡‌⁢⁣​‎‎⁪⁡⁪⁠⁪⁣⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁣⁪‏⁠⁪⁪‏​⁡⁠⁡‌⁢⁤​‎‎⁪⁡⁪⁠⁪⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁪‏‏​⁡⁠⁡‌⁣⁡​‎‎⁪⁡⁪⁠⁪⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁪‏‏​⁡⁠⁡‌­
⍳10   # ‎⁡range 1-10
∘.(       )⍨      # ‎⁢For every pair of elements
,        # ‎⁣pair into vector
⍵-         # ‎⁤subtract from input vector
+.×⍨           # ‎⁢⁡dot product with itself
,                  # ‎⁢⁢matrix to list
{⍵[⍋⍵]}                   # ‎⁢⁣sort
{≢⍵}⌸                          # ‎⁢⁤frequencies of groups
+\                               # ‎⁣⁡cumulative sums
💎


Created with the help of Luminespire.

• 27 bytes
– att
Commented Jan 3 at 10:48

# Scala, 156 146 bytes

Use Double scala.math.hypot(Double length, Double base) to calculate the square root of the sum of squares of the 2 numbers.

Saved 10 bytes thanks to @ceilingcat

Golfed version. Try it online!

(x,y)=>{val p=for{i<-0 to 99}yield(i/10,i%10);p.foldLeft(Set(1,100)){(a,t)=>a+p.count{case(a,b)=>math.hypot(a-x,b-y)<=math.hypot(t._1-x,t._2-y)}}}


Ungolfed version. Try it online!

object Main {
val gridSize = 10

def main(args: Array[String]): Unit = {
// Example usage:

println(countsForCorner.toList.sorted.mkString("[", ", ", "]"))
println(countsForSide.toList.sorted.mkString("[", ", ", "]"))
println(countsForMiddle.toList.sorted.mkString("[", ", ", "]"))
}

def countPointsInCircleForAllRadii(x: Int, y: Int): Set[Int] = {
val points = for {
i <- 0 until gridSize
j <- 0 until gridSize
} yield (i, j)

val counts = points.foldLeft(Set[Int]()) { (acc, point) =>
val (px, py) = point
val maxRadius = math.sqrt(math.pow(px - x, 2) + math.pow(py - y, 2))
val count = points.count { case (cx, cy) =>
math.sqrt(math.pow(cx - x, 2) + math.pow(cy - y, 2)) <= maxRadius
}
acc + count
}

// Ensure 1 and 100 are always included as per the specifications
counts + 1 + 100
}
}


# Charcoal, 37 bytes

ＮθＮηＦχＦχ⊞υ⁺Ｘ⁻θι²Ｘ⁻ηκ²Ｗυ«⟦ＩＬυ⟧≔⁻υ⟦⌈υ⟧υ


Try it online! Link is to verbose version of code. 0-indexed. Outputs the counts in descending order. Explanation:

ＮθＮη


Input the centre point.

ＦχＦχ


Loop over all of the points in the grid.

⊞υ⁺Ｘ⁻θι²Ｘ⁻ηκ²


Calculate the squared distance from the centre to that point.

Ｗυ«


Repeat until no more squared distances remain.

⟦ＩＬυ⟧


Output the current count of squared distances.

≔⁻υ⟦⌈υ⟧υ


Remove the highest squared distance(s).

Output in ascending order costs 2 bytes, but 2 bytes can be saved by using the newer version of Charcoal on ATO and taking input as a Gaussian integer:

Ｎθ≔ΣＥχＥχ↔⁻θ⁺ι×λＩ1jηＷ⁻ηυ⊞υ⌊ιＩＥυＬΦη¬›λι


Attempt This Online! Link is to verbose version of code. 0-indexed. Explanation:

Ｎθ


Input the centre point.

≔ΣＥχＥχ↔⁻θ⁺ι×λＩ1jη


Calculate the distances to all of the points in the grid.

Ｗ⁻ηυ⊞υ⌊ι


Sort and deduplicate the distances.

ＩＥυＬΦη¬›λι


For each unique distance, output the count of points not greater than that distance.

# JavaScript (Node.js), 89 bytes

x=>y=>[...w=Array(i=100)].map(t=>w[j=(--i%10)**2+(i/10|0)**2]=-~w[j])&&w.flatMap(t=>i+=t)


Try it online!

# JavaScript (Node.js), 104 bytes

x=>y=>[...[...Array(i=100)].map(e=t=>(--i%10)**2+(i/10|0)**2+1e7).sort(),0].flatMap((t,i)=>e-(e=t)?i:[])


Try it online!

# Perl 5-la, 69 66 bytes

map/.$/&++$a[("@F"-$)**2+($F[1]-$&)**2],0..99;$_&&say$t+=$_ for@a


Try it online!

Grid is labeled 0-9 on both axes. Input of X and Y is on one line, space separated. Output is line separated.

# Rust, 322259254 125 bytes

A port of my Scala answer in Rust.

Saved 68 bytes thanks to @ceilingcat

Saved 129 bytes thanks to @att

Golfed version. Attempt This Online!

|x,y|->HashSet<_>{let p=||(0..=99).map(|i|(|a,b|a*a+b*b)(i/10-x,i%10-y));p().map(|a|p().filter(|&b|b<=a).count()).collect()};


Ungolfed version. Attempt This Online!

use std::collections::HashSet;

const GRID_SIZE: i32 = 10;

fn main() {
// Example usage:
let mut sorted_counts_for_corner: Vec<_> = counts_for_corner.into_iter().collect();
sorted_counts_for_corner.sort_unstable();
println!("{:?}", sorted_counts_for_corner);

let mut sorted_counts_for_side: Vec<_> = counts_for_side.into_iter().collect();
sorted_counts_for_side.sort_unstable();
println!("{:?}", sorted_counts_for_side);

let mut sorted_counts_for_middle: Vec<_> = counts_for_middle.into_iter().collect();
sorted_counts_for_middle.sort_unstable();
println!("{:?}", sorted_counts_for_middle);
}

fn count_points_in_circle_for_all_radii(x: i32, y: i32) -> HashSet<i32> {
let points = (0..GRID_SIZE)
.flat_map(|i| (0..GRID_SIZE).map(move |j| (i, j)))
.collect::<Vec<_>>();

let mut counts = HashSet::new();

for point in &points {
let (px, py) = *point;
let max_radius = (((px - x).pow(2) + (py - y).pow(2)) as f64).sqrt();
let count = points.iter().filter(|&&(cx, cy)| {
(((cx - x).pow(2) + (cy - y).pow(2)) as f64).sqrt() <= max_radius
}).count() as i32;

counts.insert(count);
}

// Ensure 1 and 100 are always included as per the specifications
counts.insert(1);
counts.insert(100);

counts
}

• you want 0..10 or 0..=9` for inclusive range
– att
Commented Jan 12 at 20:36
• 125
– att
Commented Jan 12 at 23:19