如何从平方根符号下方分解出完全平方数?

huangapple go评论122阅读模式
英文:

How to factor out perfect squares from under the square root sign?

问题

I am trying to calculate the square root, and keep the part which could not change into an integer in C++, and I found This post on Stack Overflow, but it will still have a decimal.
For example, if I want to calculate √8, I want the result to be 2√2 but not 2.828. I also found This post, but it will only determine if it's an integer, but not solving it.
At first, I tried to keep each number that could not be changed into an integer completely, and using a while loop to achieve that, but I found that it is almost impossible to do that because there are too many numbers (2, 3, 5, 7, 10, 13, 17, 19, 23, 27...). When I type √573818832 into Microsoft Math Solver, it could show me that the number is equal to 12√3984853, and I wonder how it could solve the problem.
Is there any way to improve my first method?

英文:

I am trying to calculate the square root, and keep the part which could not change into integer in C++, and I found This post on Stack Overflow, but it will still have decimal.
For example, if I want to calculate √8, I want the result to be 2√2 but not 2.828. I also found This post, but it will only determine if its an integer, but not solving it.

At first, I tried to keep each number that could not be changed into integer completely, and using while loop to achieve that, but I found that it is almost impossible to do that, because there are too many numbers(2,3,5,7,10,13,17,19,23,27...). When I typing √573818832 into Microsoft Math Solver, it could show me that the number is equal to 12√3984853, and I wonder how could it solve the problem.

Is there are any ways to improve my first method?

答案1

得分: 3

  1. sqrt(n) = a * sqrt( b ) = sqrt( a<sup>2</sup> * b )
  2. 按递增顺序搜索因子。可以使用类似筛选的方法,但为简单起见,现在只通过蛮力来做。基本上,您正在寻找重复的因子(上面的 a<sup>2</sup>),因此,如果找到一个因子(使用 % 运算符),请检查该因子是否再次出现。如果它是一个重复的因子,它将放在 a 中,否则放在 b 中。
  3. #include <iostream>
  4. using namespace std;
  5. using INT = unsigned long long;
  6. void surd( INT n, INT &a, INT& b ) // 将 n 写成 a*sqrt( b );
  7. {
  8. a = b = 1;
  9. INT i = 2;
  10. while( i * i <= n ) // 寻找重复的因子
  11. {
  12. if ( n % i == 0 ) // i 是一个因子
  13. {
  14. n /= i;
  15. if ( n % i == 0 ) // i 是一个重复的因子
  16. {
  17. n /= i;
  18. a *= i;
  19. }
  20. else // i 不是一个重复的因子
  21. {
  22. b *= i;
  23. }
  24. }
  25. else
  26. {
  27. i++;
  28. }
  29. }
  30. b *= n; // 剩下的部分留在平方根内
  31. }
  32. int main()
  33. {
  34. INT n, a, b;
  35. cout << "输入 n: "; cin >> n;
  36. surd( n, a, b );
  37. cout << "sqrt( " << n << " ) = " << a;
  38. if ( b != 1 ) cout << " * sqrt( " << b << ")\n";
  39. }

示例:

  1. 输入 n: 573818832
  2. sqrt( 573818832 ) = 12 * sqrt( 3984853)
  3. 输入 n: 152399025
  4. sqrt( 152399025 ) = 12345
英文:

Let sqrt(n) = a * sqrt( b ) = sqrt( a<sup>2</sup> * b )

Search for factors in increasing order. A sieve-like method is possible, but, for simplicity, just do so by brute force for now. You are basically looking for repeated factors (the a<sup>2</sup> in the above) so, if you find a factor (use the % operator), check whether that factor occurs again. If it's a repeated factor it goes in a, otherwise in b.

  1. #include &lt;iostream&gt;
  2. using namespace std;
  3. using INT = unsigned long long;
  4. void surd( INT n, INT &amp;a, INT&amp; b ) // write n as a*sqrt( b );
  5. {
  6. a = b = 1;
  7. INT i = 2;
  8. while( i * i &lt;= n ) // seek repeated factors
  9. {
  10. if ( n % i == 0 ) // i is a factor
  11. {
  12. n /= i;
  13. if ( n % i == 0 ) // i is a repeated factor
  14. {
  15. n /= i;
  16. a *= i;
  17. }
  18. else // i is not a repeated factor
  19. {
  20. b *= i;
  21. }
  22. }
  23. else
  24. {
  25. i++;
  26. }
  27. }
  28. b *= n; // what&#39;s left over stays inside the square root
  29. }
  30. int main()
  31. {
  32. INT n, a, b;
  33. cout &lt;&lt; &quot;Enter n: &quot;; cin &gt;&gt; n;
  34. surd( n, a, b );
  35. cout &lt;&lt; &quot;sqrt( &quot; &lt;&lt; n &lt;&lt; &quot; ) = &quot; &lt;&lt; a;
  36. if ( b != 1 ) cout &lt;&lt; &quot; * sqrt( &quot; &lt;&lt; b &lt;&lt; &quot;)\n&quot;;
  37. }

As examples:

  1. Enter n: 573818832
  2. sqrt( 573818832 ) = 12 * sqrt( 3984853)

and

  1. Enter n: 152399025
  2. sqrt( 152399025 ) = 12345

huangapple
  • 本文由 发表于 2023年5月15日 00:12:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定