Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $primeSum = 0;
- $nonPrimeSum = 0;
- while (1){
- $input = readline();
- if ($input == 'stop'){
- break;
- }
- if ($input < 0) {
- echo 'Number is negative.'. PHP_EOL;
- continue;
- }
- if ($input == 1) {
- $nonPrimeSum++;
- continue;
- }
- $isPrime = true;
- for($i = 2; $i < $input; $i++){
- if($input % $i == 0){
- $isPrime = false;
- break;
- }
- }
- if ($isPrime){
- $primeSum+= $input;
- }else {
- $nonPrimeSum += $input;
- }
- }
- echo "Sum of all prime numbers is: $primeSum\n";
- echo "Sum of all non prime numbers is: $nonPrimeSum\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement