我在这里和其他地方都看到过许多不同的论坛,尤其是我的代码似乎找不到什么错。我没有指针,所以只能是我正在使用的数组-我试图更改它们,看看会发生什么,试图记下正在发生的事情,但有时仍然显示错误。我只知道它与内存和访问我不应该使用的内容有关,但无法识别问题-可能有更多的问题。
#include <stdio.h>
#include <math.h>
typedef int bool;
#define true 1
#define false 0
int main(int argc, char *argv[]){
int number;
int test;
while ((test = scanf("%d", &number)) == 1){
if (number == 0){
return 0;
}
if (number < 0){
fprintf(stderr, "Error: Chybny vstup!\n");
return 100;
}
printf("Prvociselny rozklad cisla %d je:\n", number);
if (number == 1){
printf("%d", number);
}
else{
bool prime[number+1];
for(int i = 0; i <= number; i++){
prime[i] = true;
}
for(int j = 2; j * j <= number; j++){
if (prime[j] == true){
for (int multiples = 2; prime[j * multiples] <= number; multiples++){
prime[j * multiples] = false;
}
}
}
int result[50];
int multipliers[50];
for(int i = 0; i <= 50; i++){
result[i] = 0;
}
for(int i = 0; i <= 50; i++){
multipliers[i] = 1;
}
int counter = 0;
for (int test=2; test <= number; test++){
if (prime[test]){
if (number % test == 0){
number /= test;
result[counter] = test;
counter++;
while(number % test == 0){
multipliers[counter-1]++;
number /= test;
}
}
}
}
for (int c = 0; c < counter; c++){
if (multipliers[c] > 1){
printf("%d^%d", result[c], multipliers[c]);
}
if (multipliers[c] == 1){
printf("%d", result[c]);
}
if (result[c+1] > 1){
printf(" x ");
}
}
}
printf("\n");
}
if (test == 0){
fprintf(stderr, "Error: Chybny vstup!\n");
return 100;
}
}
答案 0 :(得分:0)
您应该始终做的是,逐段编写程序,并在完成每个功能之后验证其是否正常工作,然后再移至新功能。由于您随后分别测试了要实现的每个功能,因此您知道最后实现的功能很可能存在所有错误。
在您的情况下,这个问题立即引起我的注意:
for (int multiples = 2; prime[j * multiples] <= number; multiples++){
prime[j * multiples] = false;
}
在这里,您测试prime[j * multiples] <= number
,即将 flag 与一个数字进行比较。由于后期标志被清零,直到数组末尾,j*multiples
将超出prime[]
数组的范围,并最终使程序崩溃。
测试应该是j * multiples <= number
。
因为我一次只处理一个功能和一个问题,所以我不知道这是否是您的代码唯一的问题。找出是您的任务。如果您是我,我会分段编写代码,或者在测试时撒上printf()
,以验证每个部分是否正常工作,以便我可以依靠自己已经编写的代码,并专注于手头的部分,并以最小的挫败感稳定前进。
最近有许多关于Eratosthenes筛子的问题。我一直发现实现抽象的,动态分配的筛子类型以及更改它的简单访问器/修饰符功能很有用。由于程序中只能有一个筛子,因此可以使用全局变量来描述它:
#include <stdlib.h>
#include <inttypes.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
/* This is the number of bits in an unsigned long.
The exact value is floor(log(ULONG_MAX + 1)/log(2)),
but the following definition will be correct on all
modern architectures: */
#define ULONG_BITS (sizeof (unsigned long) * CHAR_BIT)
/* Prime sieve, with a flag for zero and each odd integer,
one bit per flag. */
static unsigned long *sieve = NULL;
/* Largest positive integer within the sieve. */
static uint64_t sieve_max = 4;
/* Number of unsigned longs allocated and initialized in the sieve. */
static size_t sieve_words = 0;
/* Function to discard the sieve, if no longer needed. */
static inline void sieve_free(void)
{
free(sieve); /* free(NULL); is safe, and does nothing. */
sieve = NULL;
sieve_max = 4; /* Since 0, 1, 2, 3, 4 give built-in answers */
sieve_words = 0;
}
初始sieve_max
为4,因为我们的is_prime()
测试函数处理0、1、2和3质数,而所有较大的偶数无论如何都不是质数;这意味着is_prime()
始终知道整数0到4:
/* Return 1 if number is prime according to sieve,
0 if known composite/not prime. */
static inline int is_prime(const uint64_t number)
{
/* 0, 1, 2, and 3 are considered prime. */
if (number <= 3)
return 1; /* Prime */
/* All larger even integers are not prime. */
if (!(number & 1))
return 0; /* Not prime */
/* Outside the sieve? */
if (number > sieve_max) {
fprintf(stderr, "is_prime(): %" PRIu64 " is outside the sieve!\n", number);
exit(EXIT_FAILURE);
}
{
const uint64_t half = number / 2;
const size_t w = half / ULONG_BITS;
const unsigned long m = 1uL << (half % ULONG_BITS);
/* The flag for odd number is (number/2)th.
half / ULONG_BIT is the word where bit
half % ULONG_BIT is in.
Return 0 if the bit is set, 1 if clear. */
return !(sieve[w] & m);
}
}
我们可以很容易地增加筛子的大小,将所有新标记默认设置为“非素数”:
static void sieve_upto(const uint64_t max)
{
/* Number of words needed for (max+1)/2 bits. */
const uint64_t nwords = 1 + max / (2 * ULONG_BITS);
const uint64_t nbytes = nwords * (uint64_t)sizeof (unsigned long);
const size_t words = (size_t)nwords;
const size_t bytes = words * sizeof (unsigned long);
unsigned long *temp;
/* Already covered by the current sieve? */
if (sieve_max > max)
return;
/* We need to be able to handle max+1,
and neither bytes or words should overflow. */
if (max >= UINT64_MAX ||
(uint64_t)words != nwords || (uint64_t)bytes != nbytes) {
fprintf(stderr, "sieve_upto(): %" PRIu64 " is too high a maximum.\n", max);
exit(EXIT_FAILURE);
}
/* Do we already have all the bytes we need? */
if (words == sieve_words) {
sieve_max = max;
return;
}
/* Allocate/reallocate. Note that realloc(NULL, size)
is equivalent to malloc(size), so this is safe
even if 'sieve' is NULL. */
temp = realloc(sieve, bytes);
if (!temp) {
fprintf(stderr, "Not enough memory for a sieve up to %" PRIu64 ".\n", max);
exit(EXIT_FAILURE);
}
sieve = temp;
/* Clear the new words to all zeros. */
memset(sieve + sieve_words, 0, (words - sieve_words) * sizeof (unsigned long));
sieve_max = max;
sieve_words = words;
}
最后,我们需要一个标记数字复合(不是素数)的函数:
static inline void not_prime(const uint64_t number)
{
/* Primality is internally handled for 0, 1, 2, 3, and 4. */
if (number <= 4)
return;
/* All larger even numbers are internally handled. */
if (!(number & 1))
return;
/* Outside the sieve? */
if (number > sieve_max) {
fprintf(stderr, "not_prime(): %" PRIu64 " is outside the sieve!\n", number);
exit(EXIT_FAILURE);
}
{
const uint64_t half = number / 2;
const size_t w = half / ULONG_BITS;
const unsigned long m = 1uL << (half % ULONG_BITS);
/* Half is the bit index in sieve[].
half / ULONG_BITS is the word containing the bit
half % ULONG_BITS that needs to be set. */
sieve[w] |= m;
}
}
我要留给你们的Eratosthenes建筑的筛子。我的意图是仅说明如何使用动态内存管理来创建和管理相对有效的(即,平衡内存使用和运行时间)筛查奇数正整数。
({Verifying},一个简单的Eratosthenes筛子在我的笔记本电脑上使用一个单一的零件就发现所有少于10,000,000,000的455,052,511个质数花费不到90秒,而发现所有203,280,221个32位素数的时间少于30秒。核和上述函数。对于更大的范围,最好使用开窗式筛子。请注意,与上述is_prime()
不同,质数计数函数不考虑0和1质数。)