TubSon(int n, int *mycount) { bool prime[n + 1]; memset


Download 28.33 Kb.
Sana03.02.2023
Hajmi28.33 Kb.
#1156963
Bog'liq
3-lab, 010-20Qoʻldoshev Elbek


7-variant
Eratosfen elagi usuli bo’yicha N o’lchamli massiv tashkil qilinsin. Keyin foydalanuvchi
kiritgan tub sonni tub sonlar ichida nechanchi o’rinda ekanligi aniqlansin. Chiziqli qidirish
algoritmidan foydalanilsin.
#include
using namespace std;

int *tubSon(int n, int *mycount)


{
bool prime[n + 1];
memset(prime, true, sizeof(prime));


for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{


for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}

int count = 0;


for (int p = 2; p <= n; p++)
if (prime[p])
{
count++;
}

*mycount = count;


int *tub = new int[count];


int a = 0;
for (int p = 2; p <= n; p++)
if (prime[p])
{
tub[a] = p;
a++;
}
return tub;
}

int main()


{
int N, a;
cout << "N: ";
cin >> N;

int array[N];


int step = 100;


int *mycount = new int();
int *arr = tubSon(step, mycount);
while (N > *mycount)
{
step = 2 * step;
arr = tubSon(step, mycount);
}
for (int i = 0; i < N; i++)
{
array[i] = arr[i];
cout << array[i] << " ";
}
cout << "\n qidirilayotgan sonni kiriting = ";
cin >> a;


for (int i = 0; i < N; i++)
{


if (a == array[i])
{
cout << a << " sonining o'rni : " << i << endl;
}
}


return 0;
}


Download 28.33 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling