You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

康托尔对角线论证网格中给定n的行列位置计算公式问询

Find Row and Column for a Given Integer n in the Cantor Diagonal Grid

Alright, let's break down how to find the exact (row, column) position of any positive integer ( n ) in the provided grid. First, let's confirm the pattern of the grid:

  • Each diagonal (running from top-right to bottom-left) corresponds to a fixed value ( m = \text{row} + \text{column} - 1 ).
  • The ( m )-th diagonal contains exactly ( m ) elements.
  • The total number of elements in all diagonals before the ( m )-th one is the ( (m-1) )-th triangular number: ( S(m-1) = \frac{(m-1)m}{2} ).

Step-by-Step Calculation

  1. Determine the diagonal number ( m )
    We need to find the smallest integer ( m ) such that the sum of elements in all prior diagonals is less than ( n ), and the sum up to the ( m )-th diagonal is at least ( n ). Formally, ( m ) satisfies:
    [
    \frac{(m-1)m}{2} < n \leq \frac{m(m+1)}{2}
    ]
    To compute ( m ) directly, use this formula with the ceiling function (rounding up to the nearest integer):
    [
    m = \left\lceil \frac{\sqrt{8n + 1} - 1}{2} \right\rceil
    ]
    If ( \sqrt{8n+1} ) is an integer (meaning ( n ) is a triangular number), ( m ) will equal ( \frac{\sqrt{8n+1}-1}{2} ) exactly.

  2. Calculate the column number ( j )
    The column ( j ) is the position of ( n ) within the ( m )-th diagonal. Subtract the total number of elements in all previous diagonals from ( n ):
    [
    j = n - \frac{(m-1)m}{2}
    ]

  3. Calculate the row number ( i )
    Since ( m = i + j - 1 ), rearrange the equation to solve for ( i ):
    [
    i = m - j + 1
    ]

Example Verifications

Let's test these formulas against the grid values and your examples:

  • For ( n=1 ):
    ( m = \lceil \frac{\sqrt{9}-1}{2} \rceil = \lceil 1 \rceil =1 )
    ( j=1 - \frac{0*1}{2}=1 )
    ( i=1-1+1=1 )
    Result: ( (1,1) ) ✔️

  • For ( n=2 ):
    ( m = \lceil \frac{\sqrt{17}-1}{2} \rceil = \lceil 1.56 \rceil=2 )
    ( j=2 - \frac{1*2}{2}=2-1=1 )
    ( i=2-1+1=2 )
    Result: ( (2,1) ) ✔️ (matches the grid's second row, first column value)

  • For ( n=3 ):
    ( m = \lceil \frac{\sqrt{25}-1}{2} \rceil = \lceil 2 \rceil=2 )
    ( j=3 - \frac{1*2}{2}=3-1=2 )
    ( i=2-2+1=1 )
    Result: ( (1,2) ) ✔️ (matches the grid's first row, second column value)

  • For ( n=6 ):
    ( m = \lceil \frac{\sqrt{49}-1}{2} \rceil = \lceil 3 \rceil=3 )
    ( j=6 - \frac{2*3}{2}=6-3=3 )
    ( i=3-3+1=1 )
    Result: ( (1,3) ) ✔️ (matches the grid's first row, third column value)

  • For ( n=7 ):
    ( m = \lceil \frac{\sqrt{57}-1}{2} \rceil = \lceil 3.27 \rceil=4 )
    ( j=7 - \frac{3*4}{2}=7-6=1 )
    ( i=4-1+1=4 )
    Result: ( (4,1) ) ✔️ (matches the grid's fourth row, first column value)

Alternative Calculation Using Floor Function

If you prefer using the floor function instead of ceiling, compute ( k = \lfloor \frac{\sqrt{8n+1}-1}{2} \rfloor ):

  • If ( k(k+1)/2 = n ), set ( m=k )
  • Otherwise, set ( m=k+1 )
    Then proceed with steps 2 and 3 as above.

内容的提问来源于stack exchange,提问作者Supware

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:44:35