David Van Breemen

Greenhorn

Posts: 2

posted 2 years ago

I am teaching myself Java and am trying to write a function that will determine all of the perfect squares between 1 and 100 but am running into a problem...

Here's my code:

and here is the output:

run:

0.0

1.0 is a perfect square.

0.0

4.0 is a perfect square.

0.0

9.0 is a perfect square.

0.0

11.0 is a perfect square.

0.0

14.0 is a perfect square.

0.0

16.0 is a perfect square.

0.0

17.0 is a perfect square.

0.0

21.0 is a perfect square.

0.0

22.0 is a perfect square.

0.0

25.0 is a perfect square.

0.0

27.0 is a perfect square.

0.0

30.0 is a perfect square.

0.0

33.0 is a perfect square.

0.0

34.0 is a perfect square.

0.0

35.0 is a perfect square.

0.0

36.0 is a perfect square.

0.0

39.0 is a perfect square.

0.0

41.0 is a perfect square.

0.0

42.0 is a perfect square.

0.0

44.0 is a perfect square.

0.0

46.0 is a perfect square.

0.0

47.0 is a perfect square.

0.0

49.0 is a perfect square.

0.0

53.0 is a perfect square.

0.0

54.0 is a perfect square.

0.0

55.0 is a perfect square.

0.0

56.0 is a perfect square.

0.0

57.0 is a perfect square.

0.0

62.0 is a perfect square.

0.0

64.0 is a perfect square.

0.0

67.0 is a perfect square.

0.0

68.0 is a perfect square.

0.0

69.0 is a perfect square.

0.0

70.0 is a perfect square.

0.0

71.0 is a perfect square.

0.0

74.0 is a perfect square.

0.0

79.0 is a perfect square.

0.0

81.0 is a perfect square.

0.0

83.0 is a perfect square.

0.0

84.0 is a perfect square.

0.0

85.0 is a perfect square.

0.0

86.0 is a perfect square.

0.0

88.0 is a perfect square.

0.0

90.0 is a perfect square.

0.0

91.0 is a perfect square.

0.0

93.0 is a perfect square.

0.0

98.0 is a perfect square.

0.0

99.0 is a perfect square.

0.0

100.0 is a perfect square.

There are 49 perfect squares between 1 and 100.

BUILD SUCCESSFUL (total time: 6 seconds)

Which is clearly wrong. Is there something wrong with my code or is this due to inherent imprecision in the double type or the Math.sqrt function?

Any help on this would be greatly appreciated.

Here's my code:

and here is the output:

run:

0.0

1.0 is a perfect square.

0.0

4.0 is a perfect square.

0.0

9.0 is a perfect square.

0.0

11.0 is a perfect square.

0.0

14.0 is a perfect square.

0.0

16.0 is a perfect square.

0.0

17.0 is a perfect square.

0.0

21.0 is a perfect square.

0.0

22.0 is a perfect square.

0.0

25.0 is a perfect square.

0.0

27.0 is a perfect square.

0.0

30.0 is a perfect square.

0.0

33.0 is a perfect square.

0.0

34.0 is a perfect square.

0.0

35.0 is a perfect square.

0.0

36.0 is a perfect square.

0.0

39.0 is a perfect square.

0.0

41.0 is a perfect square.

0.0

42.0 is a perfect square.

0.0

44.0 is a perfect square.

0.0

46.0 is a perfect square.

0.0

47.0 is a perfect square.

0.0

49.0 is a perfect square.

0.0

53.0 is a perfect square.

0.0

54.0 is a perfect square.

0.0

55.0 is a perfect square.

0.0

56.0 is a perfect square.

0.0

57.0 is a perfect square.

0.0

62.0 is a perfect square.

0.0

64.0 is a perfect square.

0.0

67.0 is a perfect square.

0.0

68.0 is a perfect square.

0.0

69.0 is a perfect square.

0.0

70.0 is a perfect square.

0.0

71.0 is a perfect square.

0.0

74.0 is a perfect square.

0.0

79.0 is a perfect square.

0.0

81.0 is a perfect square.

0.0

83.0 is a perfect square.

0.0

84.0 is a perfect square.

0.0

85.0 is a perfect square.

0.0

86.0 is a perfect square.

0.0

88.0 is a perfect square.

0.0

90.0 is a perfect square.

0.0

91.0 is a perfect square.

0.0

93.0 is a perfect square.

0.0

98.0 is a perfect square.

0.0

99.0 is a perfect square.

0.0

100.0 is a perfect square.

There are 49 perfect squares between 1 and 100.

BUILD SUCCESSFUL (total time: 6 seconds)

Which is clearly wrong. Is there something wrong with my code or is this due to inherent imprecision in the double type or the Math.sqrt function?

Any help on this would be greatly appreciated.

posted 2 years ago

From a short review ... Yes, it is due to the imprecision of floating point numbers. If floating point was completely precise, then your application should be reporting 100 perfect squares between 1 and 100.

Having said that, if you also think that 100 perfect squares is the wrong answer, then that part is due to your code ...

Henry

- 1

David Van Breemen wrote:

There are 49 perfect squares between 1 and 100.

BUILD SUCCESSFUL (total time: 6 seconds)

Which is clearly wrong. Is there something wrong with my code or is this due to inherent imprecision in the double type or the Math.sqrt function?

From a short review ... Yes, it is due to the imprecision of floating point numbers. If floating point was completely precise, then your application should be reporting 100 perfect squares between 1 and 100.

Having said that, if you also think that 100 perfect squares is the wrong answer, then that part is due to your code ...

Henry

David Van Breemen

Greenhorn

Posts: 2

Campbell Ritchie

Marshal

Posts: 56541

172

Stefan Evans

Bartender

Posts: 1837

10

posted 2 years ago

I always thought a perfect square was one in which the square root of that number was an integer.

I might suggest you turn it around. Rather than starting from the square and taking the square root. If you start from the root you can avoid a whole lot of calculations and checking.

I might suggest you turn it around. Rather than starting from the square and taking the square root. If you start from the root you can avoid a whole lot of calculations and checking.