Please write the c-code below in matlab.

2 vues (au cours des 30 derniers jours)
Attcharapan Chailert
Attcharapan Chailert le 16 Jan 2021
#include<stdio.h>
int main()
{
const int MAX = 1000;
char number[MAX+1];
int index;
int j;
for(index = 0; index <= MAX; index++)
{
number[index]=1;
}
for(index = 2; index
<= MAX; index++)
{
if(number[index]==1)
{
for(j=2*index; j<=MAX; j=j+index)
{ number[j]=0;
if(index%2==0)
number[j]=0;
else
if(index%3==0)
number[j]=0;
else
if(index%5==0)
number[j]=0;
else
if(index%7==0)
number[j]=0;
}
}
}
printf("The following numbers are prime:\n");
for(index=2;
index<=MAX; index++)
{
if(number[index]==1)
printf("%d\n",index);
}
return 0;
}
  1 commentaire
Walter Roberson
Walter Roberson le 16 Jan 2021
The code only checks factors 2, 3, 5, 7, and so produces incorrect answers for starting from 11^2 = 121.
A number of people have posted code to produce primes; why not use one of them?

Connectez-vous pour commenter.

Réponses (1)

Athul Prakash
Athul Prakash le 18 Jan 2021
Hey Attcharapan,
I presume that you're trying to do somethign similar to the Sieve of Erastosthenes alrogithm and find prime numbers in the given range. You may check out the following File Exchange submission implementing the same:https://www.mathworks.com/matlabcentral/fileexchange/73941-erathostenes
Please note that File Exchange submissions are shared by the MATLAB user community and are not official Mathworks products (they are not written or tested by Mathworks itself).

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by