Skip to content

Instantly share code, notes, and snippets.

@sopherwang
Created September 9, 2014 04:44
Show Gist options
  • Select an option

  • Save sopherwang/b07e4785c5be091677a0 to your computer and use it in GitHub Desktop.

Select an option

Save sopherwang/b07e4785c5be091677a0 to your computer and use it in GitHub Desktop.

Revisions

  1. sopherwang created this gist Sep 9, 2014.
    32 changes: 32 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    public double pow(double x, int n)
    {
    if (n == 0)
    {
    return 1;
    }
    if (n < 0)
    {
    return 1.0 / power(x, -n);
    }
    else
    {
    return power(x, n);
    }
    }

    private double power(double x, int n)
    {
    if (n == 0)
    {
    return 1;
    }
    double tem = power(x, n / 2);
    if (n % 2 == 0)
    {
    return tem * tem;
    }
    else
    {
    return tem * tem * x;
    }
    }