简体   繁体   English

计算从1到n的素数-崩溃

[英]Calculating primes from 1 to n — Crash

I've writen some code in C that calculates all the prime numbers from 1 to n (n is input from user). 我已经在C语言中编写了一些代码,可以计算从1到n的所有素数(n是从用户输入的)。

As giving form to one of the actual alogrithms to do this is a bit outside of my skill range I decided to do it by brute force: 由于将形式赋予实际的一种算法来执行此操作超出了我的技能范围,因此我决定通过蛮力进行:

EDIT:img ( Can't post yet, less than 10 rep http://i44.tinypic.com/332t9uv.jpg ) that's how I actually code, dunno why my code format was changed when posting, sorry. 编辑:img(尚不能发布,少于10个代表http://i44.tinypic.com/332t9uv.jpg ),这就是我实际编码的方式,不知道为什么发布时更改了我的代码格式,对不起。

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n, i = 1, x = 1;
  printf("Porfavor escriba el numero: ");
  scanf(" %d", &n);
  while (i != n)
  {
    x = 1;
    while (x <= i)
    {
      if (x % i == 0)
      {
        continue;
        ++x;
        if (x == i && x % i != 0)
          printf("%d ", x);
      }
    }
    i++;
  }
  return EXIT_SUCCESS;
}

It all goes fine and asks for input, but after the user inputs n the program crashes, and I am a bit stumped as to why it does that. 一切都很好,并要求输入,但是在用户输入n之后,程序崩溃了,为什么这样做会令我有些困惑。

Any help would be greatly appreciated! 任何帮助将不胜感激! Thanks! 谢谢!

EDIT: After making them NOT 0, it still doesn't work, doesn't crash anymore but it never calculates anything and I have to manually exit the program. 编辑:使它们不为0后,它仍然不起作用,不再崩溃,但它从不计算任何东西,我必须手动退出程序。 Any idea of how to fix this? 关于如何解决这个问题的任何想法?

Your 'i' and 'x' both start from 0. So when you do 您的“ i”和“ x”都从0开始。因此,当您执行

x%i

The computer can't divide by 0 and gives floating point exception 计算机无法除以0并给出浮点异常

EDIT: Further the logic of program is flawed. 编辑:进一步的程序逻辑是有缺陷的。 SO even if you do 所以即使你这样做

int i=1,x=1;

The program will go into infinite loop because 该程序将进入无限循环,因为

//x=1 and i=1
while(x <= i){
        if(x%i == 0){
        //This condition is always true
        continue; //Program will go to start of loop without changing numbers
        ++x;    //This will never happen

So the loop becomes infinite. 因此循环变为无限。 Although there are libraries in C++ that will do this for you, if you want to persist with brute-force you need to do the following 尽管C ++中有一些库可以为您做到这一点,但是如果您想继续使用蛮力,则需要执行以下操作

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, i = 2, x = 2;
    printf("Enter the number: ");
        scanf("%d", &n);
    while(i <= n)  //For each number i in range (2,n)
    {
        x = 2;
        while(x < i)
        {
            if(i%x == 0)  //Check if i divisible by x
            {
            break; //Since it divides this number can't be prime
            }
            else
            {
                //Check next number
                x++;
            }
        }
        //We come out of above loop through break or natural end to execution. 
        //Check if natural end has occurred
        if(x==i)
        {
            //All numbers were checked but i isn't divided by any
            //It is PRIME!!
            printf("\n%d", x);
        }
        i++;
    }


    return EXIT_SUCCESS;
}

If you run into such problems in the future, you can trace them easily by running your program in gdb : 如果将来遇到此类问题,可以通过在gdb运行程序来轻松跟踪它们:

# compile:
gcc -Wall -ggdb main.c
# run in gdb:
gdb --args ./a.out
# ... lots of notes of gdb ...
> run
Porfavor escriba el numero: 7
# program crashes at some point
# let's have a look at it with a backtrace
# (it will also print the line where the program crashed)
> bt

Divide by zero error. 除以零误差。 You need to use int i = 1 . 您需要使用int i = 1 The program gives a runtime error when it divides by 0. 程序除以0时会给出运行时错误。

This is irrelevant but you should really consider using the Standard Allman style , Java style or GNU style indentation in C. Your current Banner style is difficult to read and is discouraged by most people. 这无关紧要,但是您应该真正考虑在C 语言中使用Standard Allman样式Java样式GNU样式缩进。您当前的Banner样式难以阅读,并且大多数人不赞成使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM