> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/f/find_square_difference.md).

# Find square difference

```ruby
var N = 1000
 
# Binary search
var n = bsearch_ge(1, N, {|k|
    k**2 - (k-1)**2 <=> N+1
})
 
# Closed-form
var m = ceil((N + 1 + N&1) / 2)
 
assert(n**2 - (n-1)**2 > N)
assert_eq(n, m)
 
say "#{n}^2 - #{n-1}^2 = #{n**2 - (n-1)**2}"
```

#### Output:

```
501^2 - 500^2 = 1001
```
