$35.00
In this MP, you are going to implement a C program to print the semiprimes in a given range. A semiprime is a positive integer that is a product of two prime numbers (the prime numbers can be
identical). Here are some examples:
Number is a semiprime ?
2 No. 2 = 2 x 1
4 Yes. 4 = 2 x 2
6 Yes. 6 = 2 x 3
12 No. 12 = 4 x 3 = 2 x 6
22 Yes. 22 = 2 x 11
Details
Your program first reads two inputs a, b (assume both are positive integers and ) and then prints all the semiprimes in (including a and b).
Here are some examples:
a b Your program output
2 6 4 6
30 40 33 34 35 38 39
16 20
62 62 62
9 16 9 10 14 15
There are two functions you need to implement:
int is_prime(int number): this function checks whether the input number is a prime number or not and returns 1 if the number is a prime number, otherwise 0.
int print_semiprimes(int a, int b): this function prints all the semiprimes in and returns 0 if there is NO semiprime within the range, otherwise 1.
Below is the algorithm of the print_semiprimes function:
Here are some rules must be followed in your implementation:
Your main function MUST call print_semiprimes to print all the semiprimes within the range. Do NOT write code to directly print semiprimes in your main function.
Your print_semiprimes function MUST use is_prime function to check the factors.
Each semiprime should be printed only once and they should be printed in ascending order.
Building an Testing
To compile your program, type following command
MP4 assignment is due on Thursday, Oct 5, by 10pm in your svn repository.
a ≤ b [a, b]
[a, b]
int print_semiprimes(int a, int b){
for each number n from a to b
for each number k from 2 to n-1
if k is a prime factor of n and (n/k) is also a prime number
print n
if there exists semiprimes in [a,b]
return 1
else
return 0
}
gcc -Wall -g mp4.c -o mp4
To execute the binary, type:
To test your implementation, below are some sample outputs. Your program will be tested with
Grading Rubric
Functionality (90%)
The program outputs the correct semiprimes for all tests.
Style, Comments, Write-up (10%)
Intro Paragraph – 5%
Style, Comments – 5%
If your code does not compile you will get 0.
./mp4
0 < a, b ≤ 200
1
2
Input two numbers: 1 10
4 6 9 10
1
2
Input two numbers: 65 65
65
1
2
Input two numbers: 100 105
1
2
Input two numbers: 141 159
141 142 143 145 146 155 158 159
1
2
Input two numbers: 80 120
82 85 86 87 91 93 94 95 106 111 115 118 119
No labels
WhatsApp us