How to resolve the algorithm Semiprime step by step in the C# programming language
How to resolve the algorithm Semiprime step by step in the C# programming language
Table of Contents
Problem Statement
Semiprime numbers are natural numbers that are products of exactly two (possibly equal) prime numbers.
Semiprimes are also known as:
(This particular number was chosen as the length of the Arecibo message).
Write a function determining whether a given number is semiprime.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Semiprime step by step in the C# programming language
The code snippet is a C# program that tests if a number is semiprime. A semiprime number is a natural number that is the product of two prime numbers. The program tests numbers from 0 to 49 and prints the result to the console.
The isSemiPrime
function takes an integer as input and returns true if the input is semiprime, and false otherwise.
The function works by iterating over all the numbers from 2 to the square root of the input, and checking if the input is divisible by any of these numbers.
If the input is divisible by a number, then the function divides the input by that number and increments a counter.
If the counter reaches 2, then the input is semiprime and the function returns true.
Otherwise, the function returns false.
The main function of the program simply calls the isSemiPrime
function for each number from 0 to 49 and prints the result to the console.
Source code in the csharp programming language
static void Main(string[] args)
{
//test some numbers
for (int i = 0; i < 50; i++)
{
Console.WriteLine("{0}\t{1} ", i,isSemiPrime(i));
}
Console.ReadLine();
}
//returns true or false depending if input was considered semiprime
private static bool isSemiPrime(int c)
{
int a = 2, b = 0;
while (b < 3 && c != 1)
{
if ((c % a) == 0)
{
c /= a;
b++;
}
else
{
a++;
};
}
return b == 2;
}
You may also check:How to resolve the algorithm Loops/Downward for step by step in the FALSE programming language
You may also check:How to resolve the algorithm N-smooth numbers step by step in the REXX programming language
You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Environment variables step by step in the C++ programming language
You may also check:How to resolve the algorithm Queue/Usage step by step in the ALGOL 68 programming language