看板 NTUEE107HW 關於我們 聯絡資訊
如題 An integer is said to be prime if it is divisible only by two distinct factors 1 and itself. For example, 2, 3, 5, and 7 are prime, but 4, 6, 8 and 9 are not. [Note: The number 1 is not a prime number.] Write a function that determines if a number is prime. The following is a main program that calls the function to determine and print all the prime numbers between 1 and 10,000. // prime.cpp #include <iostream> using std::cout; using std::endl; #include <iomanip> using std::setw; bool prime( int ); /* prototype for function prime */ int main() { int count = 0; cout << "The prime numbers from 1 to 10000 are:\n"; for ( int loop = 2; loop <= 10000; ++loop ) if ( prime(loop) ) { /* make call to function prime */ ++count; cout << setw( 6 ) << loop; if ( count % 10 == 0 ) cout << '\n'; } // end if cout << '\n' << "There were " << count << " prime numbers\n"; return 0; } // end main -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.239.237