Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rooting without using sqrt in Swift

I am trying to approach the rooting Swift exercise without using sqrt() method.

My idea was following:

import Foundation


    func rooting(number:Int) -> Int {
    
            for i in 1...100 {
                if i * i == number {
                    var Root = i
                    break }
                
                return Root //here
            }
        return Root //here
    }
            
            print(rooting(number:9))

But on the lines I left comments for you, I get Cannot find 'Root' in scope error. When I try to initialize root as an integer before I run the function, the function either uses the value I initialized the Root variable with or I get some errors if I try to initialize it as an optional integer or an empty array.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What’s wrong with my thinking here?

>Solution :

OK, so here’s your code after I aligned it.

import Foundation

func rooting(number: Int) -> Int {
  for i in 1...100 {
    if i * i == number {
      var Root = i
      break
    }
    return Root
  }
  return Root
}

print(rooting(number: 9))

The problem is that you are creating the Root variable in a scope that ends before you try to use the variable. Essentially, the Root variable only exists in the following segment:

import Foundation

func rooting(number: Int) -> Int {
  for i in 1...100 {
    if i * i == number {
      var Root = I //Root variable starts existing
      break
    } //Root variable no longer exists
    return Root
  }
  return Root
}

print(rooting(number: 9))

This is because it is scoped to the if statement. To solve this specific problem, you could change your code to this:

import Foundation

func rooting(number: Int) -> Int {
  for i in 1...100 {
    if i * i == number {
      return i
    }
  }
}

print(rooting(number: 9))

since you don’t do anything with Root other than return it. Of course, this still won’t work, as you aren’t guaranteed to return anything (what if number is 8?), and you said that the function will always return an Int.


Here’s a quick example of an implementation of a rooting function:

import Foundation

func rooting(number: Int) -> Int? {
  guard number > 0 else { return nil }
  for i in 0...(number/2) {
    if i * i == number {
      return i
    }
  }
  return nil
}

print(rooting(number: 9))

If the number has a square root, it will be returned; otherwise, the function returns nil.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading