Skip to main content
Fixed MathJax
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

A more efficient way to do this is to keep doubling the product while you can....

So, for example, \$5^2\$ is:

 5 +  5 -> 10
10 + 10 -> 20

$$\begin{eqnarray*} 5 +& 5 &\longrightarrow 10 \\ 10 +& 10 &\longrightarrow 20 \end{eqnarray*} $$

then add the last 5 to get 25.

This can be done efficiently by using bit shifting:

public static int sqr (int val)
{
    int mask = 1;
    int factorsum = val;
    int sum = 0;
    while (val >= mask)
    {
        if ((val & mask) == mask)
        {
            sum += factorsum;
        }
        factorsum += factorsum;
        mask += mask;
    }
    return sum;
}

This is a \$O(log(n))\$\$O(\log(n))\$ solution to the problem. See it in ideone too

A more efficient way to do this is to keep doubling the product while you can....

So, for example, \$5^2\$ is:

 5 +  5 -> 10
10 + 10 -> 20

then add the last 5 to get 25.

This can be done efficiently by using bit shifting:

public static int sqr (int val)
{
    int mask = 1;
    int factorsum = val;
    int sum = 0;
    while (val >= mask)
    {
        if ((val & mask) == mask)
        {
            sum += factorsum;
        }
        factorsum += factorsum;
        mask += mask;
    }
    return sum;
}

This is a \$O(log(n))\$ solution to the problem. See it in ideone too

A more efficient way to do this is to keep doubling the product while you can....

So, for example, \$5^2\$ is:

$$\begin{eqnarray*} 5 +& 5 &\longrightarrow 10 \\ 10 +& 10 &\longrightarrow 20 \end{eqnarray*} $$

then add the last 5 to get 25.

This can be done efficiently by using bit shifting:

public static int sqr (int val)
{
    int mask = 1;
    int factorsum = val;
    int sum = 0;
    while (val >= mask)
    {
        if ((val & mask) == mask)
        {
            sum += factorsum;
        }
        factorsum += factorsum;
        mask += mask;
    }
    return sum;
}

This is a \$O(\log(n))\$ solution to the problem. See it in ideone too

add example
Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419

A more efficient way to do this is to keep doubling the product while you can....

So, for example, \$5^2\$ is:

 5 +  5 -> 10
10 + 10 -> 20

then add the last 5 to get 25.

This can be done efficiently by using bit shifting:

public static int sqr (int val)
{
    int mask = 1;
    int factorsum = val;
    int sum = 0;
    while (val >= mask)
    {
        if ((val & mask) == mask)
        {
            sum += facorsum;factorsum;
        }
        factorsum += factorsum;
        mask += mask;
    }
    return sum;
}

This is a \$O(log(n))\$ solution to the problem. See it in ideone too

A more efficient way to do this is to keep doubling the product while you can....

So, for example, \$5^2\$ is:

 5 +  5 -> 10
10 + 10 -> 20

then add the last 5 to get 25.

This can be done efficiently by using bit shifting:

int mask = 1;
int factorsum = val;
int sum = 0;
while (val >= mask) {
    if (val & mask == mask) {
        sum += facorsum;
    }
    factorsum += factorsum;
    mask += mask;
}
return sum;

This is a \$O(log(n))\$ solution to the problem.

A more efficient way to do this is to keep doubling the product while you can....

So, for example, \$5^2\$ is:

 5 +  5 -> 10
10 + 10 -> 20

then add the last 5 to get 25.

This can be done efficiently by using bit shifting:

public static int sqr (int val)
{
    int mask = 1;
    int factorsum = val;
    int sum = 0;
    while (val >= mask)
    {
        if ((val & mask) == mask)
        {
            sum += factorsum;
        }
        factorsum += factorsum;
        mask += mask;
    }
    return sum;
}

This is a \$O(log(n))\$ solution to the problem. See it in ideone too

Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419

A more efficient way to do this is to keep doubling the product while you can....

So, for example, \$5^2\$ is:

 5 +  5 -> 10
10 + 10 -> 20

then add the last 5 to get 25.

This can be done efficiently by using bit shifting:

int mask = 1;
int factorsum = val;
int sum = 0;
while (val >= mask) {
    if (val & mask == mask) {
        sum += facorsum;
    }
    factorsum += factorsum;
    mask += mask;
}
return sum;

This is a \$O(log(n))\$ solution to the problem.