-5

How to use only addition and subtraction to find out square root of a natural number?
Thanks.
[I have looked over the internet but i didn't find any content related to this problem.]
Explanation to my problem: I want to create a c function which will receive only a natural number and return square root of it.
You may say to use the "sqrt" function but i just thought of creating one which will utilize addition and subtraction operator to create the square root.
You don't have to write the program, just writing the formula for it will be just fine. Thanks.

Update: This question is not specifically about coding rather about mathematics.(I tagged "c" as it had some link but this question is NOT about coding.)

laverya
  • 242
  • 4
  • 12

2 Answers2

2

n2 is the equivalent of the sum of the n first odd numbers.

You can iterate over the n (consequently adding only the next odd number to the previously calculated sum) until the square is equal or exceeds your number.

k = 0
sum = 0
while sum < target:
    k += 1
    sum += 2k-1
if sum > target:
    the target doesn't have integer root
else:
    k is the square root
mszymborski
  • 1,615
  • 1
  • 20
  • 28
  • 1
    He wants square root, not square. – zch Apr 07 '18 at 23:03
  • @zch: it's why I should not post on SO at night. I reused my answer in a different way. – mszymborski Apr 07 '18 at 23:07
  • I knew that someone would tell me to use iteration or recursion technique(as I had already thought when nature called me!). That's why I asked to show a formula using only subtraction and addition operator. @mszymborski – ProgrammingEnthusiast Apr 07 '18 at 23:12
  • 1
    @GaousMuhammadSaklaen: in general square roots are calculated using iterative methods, from the primitive squaring a rectangle method to more interesting ones. I don't think a formula not involving iteration exists. – mszymborski Apr 07 '18 at 23:32
  • @mszymborski and that's the reason I started thinking about it and posted about the topic in stackoverflow. :) – ProgrammingEnthusiast Apr 07 '18 at 23:36
  • 1
    Would it not be more suitable for math.stackexchange.com? They would likely deliver either proof it does not exist, or a method for doing so. – mszymborski Apr 07 '18 at 23:37
  • I am wondering who is voting negative without understanding what I have asked. – ProgrammingEnthusiast Apr 07 '18 at 23:37
  • @mszymborski thanks for suggesting. I will ask again in math.stackexchange.com if it not possible to get an answer here. I hope none copies my question and post it over there, haha... – ProgrammingEnthusiast Apr 07 '18 at 23:42
  • 1
    @GaousMuhammadSaklaen people are downvoting you because the question is off-topic for Stack Overflow, here we primarily discuss code and not maths (unless maths is an important part of the coding question). – Pablo Apr 07 '18 at 23:50
  • I can clearly see that. Thanks anyway. @Pablo – ProgrammingEnthusiast Apr 07 '18 at 23:55
0

We can use the fact that (n+1)² = n² + 2n + 1.

def sqrt(n):
    k = 0
    s = 0
    while s <= n:
        s = s+k+k+1
        k = k+1
    return k-1

print (sqrt(0))     # 0^2
print (sqrt(1))     # 1^2
print (sqrt(2))     # (1.4142135623730951...)^2
print (sqrt(144))   # 12^2
print (sqrt(169))   # 13^2
print (sqrt(196))   # 14^2
print (sqrt(255))   # 15^2
print (sqrt(1000))  # (31.622776601683793...)^2
print (sqrt(2000))  # (44.721359549995796...)^2

UPD
Sorry, I thought you were asking for code :)

lcastillov
  • 2,163
  • 1
  • 11
  • 17