I'm thinking of a program that uses prime numbers to perform prime factorization of integers.

Asked 2 years ago, Updated 2 years ago, 30 views

I'm thinking of a program that uses prime numbers to perform prime factorization of integers.

The function p_flag is a program for an integer I(2<=i<=n) that substitutes 1 for the array element flag[i] when i is prime and 0 for otherwise.If it is not a prime number, sift it off as an integer multiple of a lower prime number.

In the Main function,
Read the variable m with an integer greater than or equal to 2 and divide the values by the smaller prime numbers in order until the quotient is 1.

If you do this, the keyboard will ask you for the value of m, and the program will not proceed.Help > <

#include<stdio.h>

void p_flag(int flag[], intn){
    inti,j;
    for(i=2;i<=n;i++){
        flag[i] = 1;
        printf("~~%d~~",i); 
        if(i<=3){
            for(intk=2;k<=i-1;k++){
                if(flag[k]!=0){
                    int check = i%k;
                    printf("%d",k); 
                    if(check==0){
                        flag[i] = 0;    
                    }
                }
            }
        }
    }

}

int main(void){
    /* void p_flag();*/
    int flag [10000], m, md, i;
    printf("integer2 or above:\n";
    scanf("%d", & m);

    p_flag(flag,m);

    md = m;
    For(i=2;i<m/*flag[i]Number of elements */;i++){
        if(flag[i]!=0){
            while(m/flag[i]!=1){
                printf("%d", flag[i]);
                md = m/flag[i];
            }
        }
    }
}

c

2022-09-29 21:21

1 Answers

The i does not increase at this point, so the loop does not appear to come out.

while(m/flag[i]!=1){
        printf("%d", flag[i]);
        md = m/flag[i];
    }

Also, if flag[i] is 0, it will end abnormally with "0 division", so I think you need to review the logic.


2022-09-29 21:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.