标题:本人编写的打印质数的程序,请多指教!
只看楼主
PALDOS
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2023-2-24
结帖率:100%
已结贴  问题点数:20 回复次数:10 
本人编写的打印质数的程序,请多指教!
   
#include<stdio.h>
#include<math.h>
void main()
{
    int siz,i,flage;
    unsigned long int var=0,ifzs=3,mul=2;
    siz=sizeof(unsigned long int);
    printf("unsigned width is %d\n",siz);
    i=siz*8;
    printf("i=%d\n",i);
/*    do
    {
        if(i>1)
            
            printf("%lu:",var);
        else

            printf("%lu:",var-1);
        printf("%d\t",i);
        var*=2;
        --i;
   
    }while(i>0);
*/
    printf("\n2^64=%lu\n",var-1);

    do
            {
        mul=2;
        flage=1;   
   
        do{
              if(ifzs%mul==0)
              { flage=0;
                  break;}
              else
              { ++mul;}
        }while(mul<=sqrt(ifzs));
        if(flage)
        {printf("%lu\t",ifzs);
                ++ifzs;}
        else
        {    ++ifzs;
        }
        }while(ifzs<100000);
            printf("\n");
            }


搜索更多相关主题的帖子: int 编写 printf mul var 
2023-02-24 16:22
forever74
Rank: 12Rank: 12Rank: 12
来 自:CC
等 级:贵宾
威 望:49
帖 子:1636
专家分:3940
注 册:2007-12-27
得分:5 
恭喜楼主在写代码的征途上一步一个脚印奋勇前进!

对宇宙最严谨的描述应该就是宇宙其实是不严谨的
2023-02-24 18:33
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:5 
建议你先换一个正经的C编译器,这非常重要

void main() ------ 详见 https://en. ,虽然允许实现定义,但也不可能改变返回类型
siz=sizeof(unsigned long int); ------ sizoef 的类型是 size_t,不是 int
i=siz*8; ------ char 或者说字节,未必是8bits,应该用 CHAR_BIT
printf("\n2^64=%lu\n",var-1); ------ 这我就完全看不懂了,2^64 应该是 18446744073709551616,怎么可能是 0ul - 1 ?

我将你的代码改了一下
程序代码:
#include <stdio.h>
#include <limits.h>

int main( void )
{
    printf( "unsigned: %zuBytes\n", sizeof(unsigned) );
    printf( "unsigned: %zuBits\n", sizeof(unsigned)*CHAR_BIT );

    for( unsigned i=2; i!=100000; ++i )
    {
        unsigned factor = 2;
        for( ; factor<=i/factor && i%factor!=0; ++factor );
        if( factor > i/factor )
            printf( "%u\t", i );
    }
    putchar( '\n' );
}
2023-02-24 20:52
forever74
Rank: 12Rank: 12Rank: 12
来 自:CC
等 级:贵宾
威 望:49
帖 子:1636
专家分:3940
注 册:2007-12-27
得分:5 
补充一下,
试除法应该用在判断少量几个数是否素数上面;
而对于生成素数表也就是大量素数的应用,筛法的效率会高很多。
当然,还没涉及数组的话,不急。

对宇宙最严谨的描述应该就是宇宙其实是不严谨的
2023-02-24 21:23
PALDOS
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2023-2-24
得分:0 
回复 3楼 rjsp
首先感谢版主的耐心指导,令本人不甚至荣幸。关于printf("\n2^64=%lu\n",var-1); ------ 这我就完全看不懂了,2^64 应该是 18446744073709551616,怎么可能是 0ul - 1 ?
因为无符号长整型变量的数值范围为:0——2^64-1,所以当计算2^64的时候,变量var为变成0,只好用0-1来表示了(这可能涉及到补码)。上面部分的代码确实有问题,更正如下:


#include<stdio.h>
#include<math.h>
void main()
{
    int i;
    size_t siz;
    unsigned long int var=2;
    siz=sizeof(unsigned long int);
    printf("unsigned width is %d\n",siz);
    i=siz*8; /*计算无符号长整型变量所占用的位数,如为4Byte,则字节数为4*8=32Bits。
    printf("i=%d\n",i);
    do
    {
        if(i>1)
            
            printf("%lu:",var);
        else

            printf("%lu:",0-1);
        printf("%d\t",i);
        var*=2;
        --i;
   
    }while(i>0);

    printf("\n2^64=%lu\n",0-1);

            }


[此贴子已经被作者于2023-2-26 09:13编辑过]

2023-02-26 08:56
PALDOS
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2023-2-24
得分:0 
回复 4楼 forever74
感谢鼓励和指点!
2023-02-26 09:08
PALDOS
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2023-2-24
得分:0 
又修改了一下,这样显示的结果格式似乎更好看一些:(以下代码在DEV-C++5.11下编译通过)
#include<stdio.h>
#include<math.h>
void main()
{
    int i,flage=1;
    size_t siz;
    unsigned long int var=2;
    siz=sizeof(unsigned long int);
    printf("unsigned width is %d\n",siz);
    i=siz*8;
    printf("siz width is %d\n",i);
    do
    {
        if(flage>(i-1))
            
            printf("2^%d=%lu\t",flage,var-1);
        else

            {printf("2^%d=%lu\t",flage,var);}
        var*=2;
        ++flage;
   
    }while(flage<=i);

    printf("\n2^32=%lu\n",var-1);


}


[此贴子已经被作者于2023-2-26 09:37编辑过]

2023-02-26 09:35
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:5 
回复 7楼 PALDOS
程序代码:
#include <stdio.h>
#include <limits.h>

int main( void )
{
    size_t nBytes = sizeof(unsigned long int);
    printf( "The \"unsigned long\" type has %zu bytes\n", nBytes );

    size_t nBits = nBytes * CHAR_BIT;
    printf( "The \"unsigned long\" type has %zu bits\n", nBits );

    for( size_t i=0; i!=nBits; ++i )
        printf( "2^%zu = %lu\n", i, 1ul<<i );
    printf( "2^%zu - 1 = %lu\n", nBits, ~0ul );
}


一种「可能的」输出
The "unsigned long" type has 4 bytes
The "unsigned long" type has 32 bits
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
2^9 = 512
2^10 = 1024
2^11 = 2048
2^12 = 4096
2^13 = 8192
2^14 = 16384
2^15 = 32768
2^16 = 65536
2^17 = 131072
2^18 = 262144
2^19 = 524288
2^20 = 1048576
2^21 = 2097152
2^22 = 4194304
2^23 = 8388608
2^24 = 16777216
2^25 = 33554432
2^26 = 67108864
2^27 = 134217728
2^28 = 268435456
2^29 = 536870912
2^30 = 1073741824
2^31 = 2147483648
2^32 - 1 = 4294967295

2023-02-28 10:38
PALDOS
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2023-2-24
得分:0 
#include<stdio.h>
#include<math.h>
void main()
{
    int flage;
    unsigned long int ifzs=2,mul=2;
    printf("%-10lu",ifzs);
    do
           {
          mul=2;
          flage=1;   
   
        do{
              if(ifzs%mul==0)
                   {
                flage=0;
                    break;
               }
              else
                   ++mul;
          } while(mul<=sqrt(ifzs));
        
        if(flage)
            {
             printf("%-10lu",ifzs);
                 ++ifzs;
                }
        else
            {   
                ++ifzs;
            }
        }while(ifzs<10000000);
            printf("\n");
            
   }




[此贴子已经被作者于2023-3-1 08:10编辑过]

2023-03-01 00:26
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
回复 9楼 PALDOS
程序代码:
#include <stdio.h>
#include <math.h>

int main( void )
{
    for( unsigned long ifzs=2; ifzs!=10000000; ++ifzs )
    {
        unsigned long mul = 2;
        const unsigned long sr = (unsigned long)sqrt(ifzs);
        for( ; mul<=sr && ifzs%mul!=0; ++mul );
        
        if( mul > sr )
            printf( "%-10lu", ifzs );
    }
}
2023-03-01 08:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-511384-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.227873 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved