⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perfect.txt

📁 MPI并行程序设计: 对于输入的无符号长整型数
💻 TXT
字号:
#include "mpi.h"
#include "stdio.h"

int isPerfect ( unsigned long );

int main ( int argc, char * argv [] )
{
        unsigned long n, my_Count, all_Count;
        int i, myid, numprocs, namelen;
        double startwtime = 0.0, endwtime;
        char processor_name [MPI_MAX_PROCESSOR_NAME];

        MPI_Init ( &argc, &argv );
        MPI_Comm_size ( MPI_COMM_WORLD, &numprocs );
        MPI_Comm_rank ( MPI_COMM_WORLD, &myid );
        MPI_Get_processor_name ( processor_name, &namelen );

        fprintf ( stdout, "Process %d of %d on %s\n",
                  myid, numprocs, processor_name );

        n = 0;
        if ( myid == 0 )
        {
                printf ( "Please give N = " );
                scanf ( "%d", &n );
                startwtime = MPI_Wtime ( );
        }
        MPI_Bcast ( &n, 1, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD );
        my_Count = 0;
        for ( i = myid + 1; i <= n; i += numprocs )
        {
                if ( isPerfect ( i ) == 1 )
                {
                       my_Count ++;
                        printf ( "%d\n", i);
                }
        }
        MPI_Reduce ( &my_Count, &all_Count, 1, MPI_UNSIGNED_LONG,
                     MPI_SUM, 0, MPI_COMM_WORLD );
        if ( myid == 0 )
        {
                printf ( "Count of Perfect Numbers : %d\n", all_Count);
                endwtime = MPI_Wtime ( );
                printf ( "Wall Clock Time = %f\n", endwtime - startwtime );
                fflush ( stdout );
        }
        MPI_Finalize ( );
        return 0;
}

int isPerfect ( unsigned long i )
{
        if ( i == 1 )
                return 0;

                unsigned long sum = 1;
        int j;
        for ( j =2; j < i; j++ )
                if ( i % j == 0 )
                        sum += j;
        if ( i == sum )
                {
                        /*      printf ( "%d\n",i);*/
                return 1;
                }
        else
                return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -