I've been a zone leader with DZone since 2008, and I'm crazy about community. Every day I get to work with the best that JavaScript, HTML5, Android and iOS has to offer, creating apps that truly make at difference, as principal front-end architect at Avego. James is a DZone Zone Leader and has posted 639 posts at DZone. You can read more from them at their website. View Full User Profile

Thursday Code Puzzler: Compute Squares

05.23.2013
| 5094 views |
  • submit to reddit

Thursday: the week's almost over and it's time for another code puzzler. The idea is simple: solve the coding problem as efficiently as you can, in any language or framework that you find suitable.

Note: Even though there really is nothing stopping you from finding a solution to this on the internet, try to keep honest, and come up with your own answer.  It's all about the participation!

Compute  Squares 

Write a function that computes the square of a number without using *, ^, or +.

Catch up on all our previous puzzlers here

Comments

Thomas Mueller replied on Thu, 2013/05/23 - 9:51am

I have a fairly fast solution that uses division, shift, or. But I wonder if division could be avoided? Another solution is using just negation and minus, but it's much slower: O(n).

Jeff Friesen replied on Thu, 2013/05/23 - 10:15am

 I've written a Java application that uses subtraction and negation to square integers, and uses division to square floating-point values:

public class SquareDemo
{
   public static void main(String[] args)
   {
      for (int i = -10; i <= 10; i++)
         System.out.printf("square %d = %d%n", i, square(i));
      System.out.println();
      for (double d = -10; d <= 10; d += 0.5)
         System.out.printf("square %f = %f%n", d, square(d));
   }

   public static int square(int x)
   {
      x = Math.abs(x);
      int total = 0;
      for (int i = 0; i < x; i++)
         total = total - -x;
      return total;
   }

   public static double square(double x)
   {
      return x/(1/x);
   }
}


Frank Dietrich replied on Thu, 2013/05/23 - 6:35pm

 Java

import java.math.BigDecimal;

class SquarePuzzler {
    private static int useAPI(int i) {
        return new BigDecimal(i).pow(2).intValue();
    }

    private static int useDIY(int i) {
        i = (i < 0 ? ~--i : i);
        int j = i;
        int x = 0;
        while(i-- != 0) {
            x -= j;
        }
        return -x;
    }

    public static void main(String...args) {
        int[] testValues = { 1, -1, 2, -2, 12, -12, 42, -42 };

        for (int i : testValues) {
            System.out.printf("i: %4d  API: %4d  DIY: %4d\n", i, useAPI(i), useDIY(i));
        }
    }
}

Mark Bernard replied on Fri, 2013/05/24 - 8:23am

public class SquarePuzzler {
public static void main(String args[]) {
System.out.println(square(123));
System.out.println(square(7));
System.out.println(square(25));
System.out.println(square(7.0));
System.out.println(square(25.0));
}

public static int square(int x) {
return (int)square((double)x);
}
public static double square(double x) {
return x / (1 / x);
}

 

ravikumar donth... replied on Fri, 2013/05/31 - 7:20am

public class SquareFinder {
	
	public static void main(String args[]){
		String string = args[0];
		
		int parseInt = Integer.parseInt(string);
		
		int [][] nums = new int[parseInt][parseInt];
		int start=1;
		 for (int row = 0; row < nums.length ; row++ ) {
			 for (int col = 0; col < nums[row].length; col++ ) {
			        nums[row][col]=start;
			       start++;
			      }
			
		 }
		 
		
		 System.out.println(nums[nums.length-1][nums.length-1]);
		 
		
	}
}

ravikumar donth... replied on Fri, 2013/05/31 - 7:42am in response to: ravikumar donthamsetti

performacne is low

Helge Fredriksen replied on Mon, 2013/06/03 - 2:54am in response to: Mark Bernard

Hehe, loved that one, using the resiprocal and turning it into a square. I've started out similar, but my algorithm was based on a while loop, counting a variable s upwards from x using ++ and ending when the condition

s/x == x

was true.

But it worked only on whole numbers, and it is of course rather slow compared to this one :-)


Ayurveda Yatan replied on Sat, 2013/12/07 - 10:48am

 Menopause is probably almost all effortlessly grasped because reverse method to help menarche, the beginning of the regular times.

Menopausal Symptoms

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.