Coder Social home page Coder Social logo

math--sqrt-x-'s Introduction

Name: Sqrt(x)

Tags: Math

Implement int sqrt(int x).
Compute and return the square root of x.
x is guaranteed to be a non-negative integer.

Example 1:

Input: 4
Output: 2

Example 2:

Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since we want to return an integer, the decimal part will be truncated.

Answer:

Swift 4.X

/**
 This is impressed Newton version, and it costs O(1) time.
 */
class Solution {
   func mySqrt(_ x: Int) -> Int {
    assert(x >= 0)
    
    if (x == 0 || x == 1)
    {
        return x
    }
    
    var tmp = Float(x)
    let xhalf = Float(0.5) * tmp
    var i = withUnsafePointer(to: &tmp) {
        return $0.withMemoryRebound(to: Int.self, capacity: 1) {
            return $0.pointee
        }
    }
    
    i = 0x5f375a86 - (i >> 1);
    
    tmp = withUnsafePointer(to: &i) {
        return $0.withMemoryRebound(to: Float.self, capacity: 1) {
            return $0.pointee
        }
    }
    
    
    tmp = tmp * (Float(1.5) - xhalf * tmp * tmp)
    tmp = tmp * (Float(1.5) - xhalf * tmp * tmp)
    tmp = tmp * (Float(1.5) - xhalf * tmp * tmp)
    
    let ret = Int(1 / tmp)
    
    if (ret * ret > x)
    {
        return ret - 1
    }
    
    return ret
    }

}

C

/*
 This is impressed Newton version, and it costs O(1) time.
 */
int mySqrt(int x) {
    
     
     assert(x >= 0);  
    
        if (x == 0 || x == 1)  
        {  
            return x;  
        }  
    
        float tmp = x;  
        float xhalf = 0.5f * tmp;  
        int i = *(int *)&tmp;  
          
        i = 0x5f375a86 - (i >> 1);   
          
        tmp = *(float *)&i;  
        tmp = tmp * (1.5f - xhalf * tmp * tmp);  
        tmp = tmp * (1.5f - xhalf * tmp * tmp);  
        tmp = tmp * (1.5f - xhalf * tmp * tmp);  
  
        int ret = 1  /  tmp;  
        if (ret * ret > x)  
        {  
            return ret - 1;  
        }  
    
        return ret;  
}

math--sqrt-x-'s People

Contributors

ccbobby avatar

Watchers

haoht avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.