perfect-square

Finding if n! + 1 is a perfect square

◇◆丶佛笑我妖孽 提交于 2021-01-29 03:30:44
问题 I'm trying to write a program to look for a number, n, between 0 and 100 such that n! + 1 is a perfect square. I'm trying to do this because I know there are only three so it was meant as a test of my Python ability. Refer to Brocard's problem. 回答1: For very large numbers it's better to avoid using floating point square roots altogether because you will run into too many precision issues and you can't even guarantee that you will be within 1 integer value of the correct answer. Fortunately

Find pairs in two arrays such that when multiplied becomes a perfect square

天大地大妈咪最大 提交于 2020-02-15 11:10:07
问题 Given two integer arrays like this:- int[] a = { 2, 6, 10, 13, 17,18 }; int[] b = { 3, 7, 8, 9, 11, 15 }; How can I find pairs from these two arrays such that when multiplied they become perfect square? For eg, in above arrays {2,8} & {18,8} are two pairs. Right now my approach is brute-force, where I am looping through both arrays like this:- int count = 0; for (int i = 0; i < arr1.Length; i++) { for (int j = 0; j < arr2.Length; j++) { var x = arr1[i] * arr2[j]; long s = (long)Math.Sqrt(x);

Perfect Square in Leetcode

て烟熏妆下的殇ゞ 提交于 2020-01-14 03:24:20
问题 I am having trouble understanding one of a Leetcode Problem. Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. Solution: int numSquares(int n) { static vector<int> dp {0}; while (dp.size() <= n) { int m = dp.size(), squares = INT_MAX; for (int i=1; i*i<=m; ++i) squares = min(squares, dp[m-i*i] + 1); dp.push_back

Perfect square or not?

时光毁灭记忆、已成空白 提交于 2019-12-28 08:13:00
问题 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; } } 回答1: 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

Perfect square or not?

我只是一个虾纸丫 提交于 2019-12-28 08:12:16
问题 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; } } 回答1: 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

How do I square a number's digits?

雨燕双飞 提交于 2019-12-10 12:13:34
问题 How do I square a number's digits? e.g.: square(21){}; should result in 41 instead of 441 回答1: function sq(n){ var nos = (n + '').split(''); var res=""; for(i in nos){ res+= parseInt(nos[i]) * parseInt(nos[i]); } return parseInt(res); } var result = sq(21); alert(result) 回答2: This is easily done with simple math. No need for the overhead of string processing. var result = []; var n = 21; while (n > 0) { result.push(n%10 * n%10); n = Math.floor(n/10); } document.body.textContent = result

How to check if an integer is a perfect square [duplicate]

醉酒当歌 提交于 2019-12-07 03:53:50
问题 This question already has answers here : What's a good algorithm to determine if an input is a perfect square? [duplicate] (3 answers) Closed 4 years ago . How could I write an if-then statement that checks if an inputted integer is a perfect square or not (i.e. if I took the square root, it would be an integer as well: 4, 9, 16, 25, 36, etc.) in DrJava? Thank you! 回答1: I am aware that this question already has an answer.... But just in case, this also works. int x = (int) Math.sqrt(input);

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

自闭症网瘾萝莉.ら 提交于 2019-12-07 01:23:07
问题 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. 回答1: 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

What's the best way in JavaScript to test if a given parameter is a square number?

喜欢而已 提交于 2019-12-05 19:53:04
问题 I created a function that will test to see if a given parameter is a square number. Read about square numbers here: https://en.wikipedia.org/?title=Square_number If the number is a square number, it returns true and otherwise false . Negative numbers also return false . Examples: isSquare(-12) // => false isSquare( 5) // => false isSquare( 9) // => true isSquare(25) // => true isSquare(27) // => false Right now, I am using this method: http://jsfiddle.net/marcusdei/ujtc82dq/5/ But, is there a

How to check if an integer is a perfect square [duplicate]

℡╲_俬逩灬. 提交于 2019-12-05 07:15:50
This question already has an answer here: What's a good algorithm to determine if an input is a perfect square? [duplicate] 3 answers How could I write an if-then statement that checks if an inputted integer is a perfect square or not (i.e. if I took the square root, it would be an integer as well: 4, 9, 16, 25, 36, etc.) in DrJava? Thank you! Jaskaranbir Singh I am aware that this question already has an answer.... But just in case, this also works. int x = (int) Math.sqrt(input); if(Math.pow(x,2) == input) //Do stuff 来源: https://stackoverflow.com/questions/34056609/how-to-check-if-an-integer