A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Prime Number in Dart page! Here, you'll find the source code for this program as well as a description of how the program works.
// Issue 4970
import 'dart:math';
void main(List<String> args){
const String error_message= "Usage: please input a non-negative integer";
if (args.isEmpty){
print(error_message);
return;
}
try{
int num_needed = int.parse(args[0]);
if (num_needed.isNegative){
print(error_message);
return;
}
// If the number is even number other than 2 OR if number is either 0 or 1, Print Composite
if( (num_needed == 0)|| (num_needed == 1) || (num_needed %2 ==0 && num_needed != 2) ){
print("Composite");
return;
}
bool is_prime = true;
int max_divisor = sqrt(num_needed).toInt();
for (int i_index = 3; i_index <= max_divisor; i_index +=2){
if(num_needed % i_index == 0){
is_prime = false;
break;
}
}
print(is_prime ? "Prime" : "Composite");
}
catch(e){
print(error_message);
}
}
Prime Number in Dart was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.