perfect-square

How to square each element of an array in Array class in Ruby?

核能气质少年 提交于 2019-12-05 05:00:27
Part of my code is as follows: class Array def square! self.map {|num| num ** 2} self end end When I call: [1,2,3].square! I expect to get [1,4,9], but instead I get [1,2,3]. Why is this the case? When I call: [1,2,3].map {|num| num ** 2} outside of the class method, I get the correct answer. You have to use Array#map! , not Array#map . Array#map -> Invokes the given block once for each element of self.Creates a new array containing the values returned by the block. Array#map! -> Invokes the given block once for each element of self, replacing the element with the value returned by the block.

Counting and generating perfect squares

大兔子大兔子 提交于 2019-11-30 16:45:52
I need some advice on how to write a Python program where it gives you a list of the first n perfect squares in list format. The output should look like this: How many squares?: 5 [1, 4, 9, 16, 25] This is what I have so far: n = int(raw_input("How many squares? ")) Now for the next part I need to create a list of the first n squares. Any suggestions on how? Thank you for your time and advice. Use a list comprehension: [ a*a for a in xrange(1, n + 1) ] Now for the next part i need to start to create a list of the first n squares. Any suggestions on how? Thank You for your time and advice. This

Counting and generating perfect squares

匆匆过客 提交于 2019-11-30 00:08:17
问题 I need some advice on how to write a Python program where it gives you a list of the first n perfect squares in list format. The output should look like this: How many squares?: 5 [1, 4, 9, 16, 25] This is what I have so far: n = int(raw_input("How many squares? ")) Now for the next part I need to create a list of the first n squares. Any suggestions on how? Thank you for your time and advice. 回答1: Use a list comprehension: [ a*a for a in xrange(1, n + 1) ] 回答2: Now for the next part i need

Perfect square or not?

匆匆过客 提交于 2019-11-28 04:12:26
This is a code to check if a number is perfect square or not. Why does it work ? static bool IsSquare(int n) { int i = 1; for (; ; ) { if (n < 0) return false; if (n == 0) return true; n -= i; i += 2; } } Because all perfect squares are sums of consecutive odd numbers: 1 = 1 4 = 1 + 3 9 = 1 + 3 + 5 16 = 1 + 3 + 5 + 7 and so on. Your program attempts to subtract consecutive odd numbers from n , and see if it drops to zero or goes negative. You can make an informal proof of this by drawing squares with sides of {1,2,3,4,...} and observe that constructing a square k+1 from square k requires

What's a good algorithm to determine if an input is a perfect square? [duplicate]

别等时光非礼了梦想. 提交于 2019-11-26 18:30:55
Possible Duplicate: Fastest way to determine if an integer's square root is an integer What's a way to see if a number is a perfect square ? bool IsPerfectSquare(long input) { // TODO } I'm using C# but this is language agnostic. Bonus points for clarity and simplicity (this isn't meant to be code-golf). Edit: This got much more complex than I expected! It turns out the problems with double precision manifest themselves a couple ways. First, Math.Sqrt takes a double which can't precisely hold a long (thanks Jon). Second, a double's precision will lose small values ( .000...00001) when you have

Check if a number is a perfect square

隐身守侯 提交于 2019-11-26 11:15:23
How could I check if a number is a perfect square? Speed is of no concern, for now, just working. The problem with relying on any floating point computation ( math.sqrt(x) , or x**0.5 ) is that you can't really be sure it's exact (for sufficiently large integers x , it won't be, and might even overflow). Fortunately (if one's in no hurry;-) there are many pure integer approaches, such as the following...: def is_square(apositiveint): x = apositiveint // 2 seen = set([x]) while x * x != apositiveint: x = (x + (apositiveint // x)) // 2 if x in seen: return False seen.add(x) return True for i in

What&#39;s a good algorithm to determine if an input is a perfect square? [duplicate]

一个人想着一个人 提交于 2019-11-26 06:27:49
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Fastest way to determine if an integer's square root is an integer What\'s a way to see if a number is a perfect square? bool IsPerfectSquare(long input) { // TODO } I\'m using C# but this is language agnostic. Bonus points for clarity and simplicity (this isn\'t meant to be code-golf). Edit: This got much more complex than I expected! It turns out the problems with double precision manifest themselves a couple

Check if a number is a perfect square

丶灬走出姿态 提交于 2019-11-26 05:49:32
问题 How could I check if a number is a perfect square? Speed is of no concern, for now, just working. 回答1: The problem with relying on any floating point computation ( math.sqrt(x) , or x**0.5 ) is that you can't really be sure it's exact (for sufficiently large integers x , it won't be, and might even overflow). Fortunately (if one's in no hurry;-) there are many pure integer approaches, such as the following...: def is_square(apositiveint): x = apositiveint // 2 seen = set([x]) while x * x !=

Fastest way to determine if an integer&#39;s square root is an integer

心不动则不痛 提交于 2019-11-25 22:28:03
问题 I\'m looking for the fastest way to determine if a long value is a perfect square (i.e. its square root is another integer): I\'ve done it the easy way, by using the built-in Math.sqrt() function, but I\'m wondering if there is a way to do it faster by restricting yourself to integer-only domain. Maintaining a lookup table is impractical (since there are about 2 31.5 integers whose square is less than 2 63 ). Here is the very simple and straightforward way I\'m doing it now: public final