[ create a new paste ] login | about

Link: http://codepad.org/rtk2BBBI    [ raw code | output | fork | 4 comments ]

C, pasted on Sep 14:
#include<stdio.h>
#include<conio.h>
#include<math.h>
// nguyên tố => true
bool KiemTraNguyenTo(int n)
{
	if (n < 2)
	{
		return false;
	}
	else if (n > 2)
	{
		if (n % 2 == 0)
		{
			return false;
		}
		for (int i = 3; i <= sqrt((float)n); i += 2)
		{
			if (n % i == 0)
			{
				return false;
			}
		}
	}
	return true;
}

void LietKeNguyenTo(int n)
{
	for(int i = 2; i < n; i++)
	{
		if(KiemTraNguyenTo(i) == true)
			printf("%4d", i);
	}
}
int main()
{
	int n;
	printf("\nNhap n: ");
	scanf("%d", &n);

	LietKeNguyenTo(n);

	getch();
	return 0;
}


Output:
1
2
3
4
5
6
7
Line 17: error: conio.h: No such file or directory
Line 5: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'KiemTraNguyenTo'
In function 'LietKeNguyenTo':
Line 30: error: 'for' loop initial declaration used outside C99 mode
Line 32: error: 'true' undeclared (first use in this function)
Line 32: error: (Each undeclared identifier is reported only once
Line 32: error: for each function it appears in.)


Create a new paste based on this one


Comments:
posted by vu on May 19

reply
posted by anhpham22042001@gmail.com on Jul 13
tại sao lại n % 2 ==0 lại loại
số 2 cũng là số nguyên tố mà

reply
posted by yonnon on Aug 2
#include<bits/stdc++.h>

using namespace std;

bool check(int n)
{
if ( n < 2 ){
return false;
}
else if( n > 2 )
{
if ( n % 2 == 0 )
return false;
for( int i = 2; i <= sqrt(n) ; ++i)
{
if( n % i == 0)
return false;
}
}
return true;
}
void lietke(int n)
{
for( int i = 0; i < n;++i)
{
if(check(i) == true)
cout <<" "<<i;
}
}
int main()
{
int n;
cout <<"Enter your sentence: ";
cin >> n;
cout <<"1";
lietke(n);
return 0;
}
reply
posted by lehoangan01 on Feb 24
// Bài 119: Viết hàm liệt kê tất cả các số nguyên tố nhỏ hơn n

#include <iostream>
#include <math.h>
using namespace std;

void SieveOfEratosthenes (int n){

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*p; i <= n; i += p)
prime [i] = false;
}
}
for ( int p = 2; p < n; p++)
if (prime[p])
cout << p << " ";
}

int main(){
int n;
cout << "nhap n: ";
cin >> n ;
SieveOfEratosthenes(n);
return 0;
}

reply