C中除了2以外的整数进行比较是否不正确?

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

Comparison in c incorrect for integers other than 2?

问题

我正在编写一个函数来确定一个数字的质因数,并将它们返回到一个双向链表中。代码如下。

  1. DLL* primefactors(int a, DLL *factors){
  2. int i;
  3. int primes[40] = {
  4. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
  5. 31, 37, 41, 43, 37, 53, 59, 61, 67, 71,
  6. 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
  7. 127, 131, 137, 139, 149, 151, 157, 163, 167, 173};
  8. factors = create_list(1); // 从节点"1"开始创建
  9. i = 0;
  10. while(i < 40 && a != 1){
  11. if (a % primes[i] == 0){
  12. printf("%d 是 %d 的因数; ", primes[i], a);
  13. add_to_tail(factors, primes[i]);
  14. a = a / primes[i];
  15. printf("余数是 %d\n", a);
  16. } else {
  17. printf("%d 不是 %d 的因数; 增加到 %d\n", primes[i], a, primes[i + 1]);
  18. i++;
  19. }
  20. }
  21. print_DLL(factors);
  22. return factors;
  23. }

该函数在测试因子为2的情况下工作正常,但无法识别更高的质因数。

例如,primefactors(8) 正确给出 1, 2, 2, 2;但 primefactors(24) 给出 1, 2, 2, 2,然后无法识别3为因子,而是运行到质数列表的末尾(请忽略列表开头的1)。

这是为什么,以及如何修复它呢?我认为 a / prime[i] 应该给出一个准确的整数,因为a和prime[i]都是整数,避免了浮点数比较问题。

谢谢!

英文:

I am writing a function to determine the prime factors of a number and return them in a doubly linked list. The code is as below.
(create_list, add_to_tail, and print_DLL perform operations on the DLL and work fine).

  1. DLL* primefactors(int a, DLL *factors){
  2. int i;
  3. int primes[40] = {
  4. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
  5. 31, 37, 41, 43, 37, 53, 59, 61, 67, 71,
  6. 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
  7. 127, 131, 137, 139, 149, 151, 157, 163, 167, 173};
  8. factors = create_list(1); // create with node of &quot;1&quot; to begin with
  9. i = 0;
  10. while(i &lt; 40 &amp;&amp; a != 1){
  11. if (a % primes[0] == 0){
  12. printf(&quot;%d is a factor of %d; &quot;, primes[i], a);
  13. add_to_tail(factors, primes[0]);
  14. a = a / primes[0];
  15. printf(&quot;remainder is %d\n&quot;, a);
  16. } else {
  17. printf(&quot;%d is not a factor of %d; increasing to %d\n&quot;, primes[i], a, primes[i + 1]);
  18. i++;
  19. }
  20. }
  21. print_DLL(factors);
  22. return factors;
  23. }

The function works correctly for testing factors of 2, but does not recognise higher primes as factors.
e.g. primefactors(8) correctly gives 1, 2, 2, 2; but primefactors(24) gives 1, 2, 2, 2 and then doesn't recognise 3 as a factor, instead running to the end of the list of primes (ignore the 1 at the beginning of the list please).

Why is this and how can it be fixed please? I'd thought that a / prime[i] would give an accurate integer as both a and prime[i] are ints, avoiding float comparison issues.

Thank you!

答案1

得分: 1

正如评论中Barmar和Some programmer dude指出的,除法应该是以primes[i]而不是primes[0]进行的。有了这个修正,它就可以正常工作了。谢谢!

英文:

As pointed out by Barmar and Some programmer dude in the comments, the division should be with primes[i] instead of primes[0]. With this correction it works. Thanks!

huangapple
  • 本文由 发表于 2023年6月22日 14:45:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529207.html
匿名

发表评论

匿名网友

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

确定