- #26

jbriggs444

Science Advisor

Homework Helper

2019 Award

- 8,920

- 3,653

Try that code using 4 as an input.Code:`1-Input a nonnegative integer N 3-If N=1 go to step 11 3-If N=2 go to step 12 4-If N=3 go to step 12 5-If N>3 go to step 6 6-Let M=2 7-M=M+1 8-Calculate N/M 9-Check N/M=1 If yes go to step 12 , otherwise go to step 10 10-If remaining of N/M is 0 go to step 11 , otherwise go to step 7 11-N is not a prime number 12-N is a prime number`

Edit: I actually turned this into machine-readable code. [Somewhat cringe-worthy, but it runs]

Perl:

```
#!/usr/bin/perl
use strict;
my $n; my $m; my $quotient;
LINE1: print "Enter a number: "; $n=<> or die "Invalid input"; chomp $n;
LINE2: if ( $n == 1 ) { goto LINE11 };
LINE3: if ( $n == 2 ) { goto LINE12 };
LINE4: if ( $n == 3 ) { goto LINE12 };
LINE5: if ( $n > 3 ) { goto LINE6 };
LINE6: $m = 2;
LINE7: $m = $m + 1;
LINE8: $quotient = int ( $n / $m );
LINE9: if ( $quotient == 1 ) { goto LINE12 } else { goto LINE10 };
LINE10: if ( $n - ( $quotient*$m ) == 0 ) { goto LINE11 } else { goto LINE7 };
LINE11: print "That number is not prime\n"; exit;
LINE12: print "That number is prime\n"; exit;
```

Enter a number: 1

That number is not prime

C:\Users\John\Documents>perl prime.pl

Enter a number: 2

That number is prime

C:\Users\John\Documents>perl prime.pl

Enter a number: 3

That number is prime

C:\Users\John\Documents>perl prime.pl

**Enter a number: 4**

That number is prime

That number is prime

C:\Users\John\Documents>perl prime.pl

Enter a number: 5

That number is prime

C:\Users\John\Documents>perl prime.pl

Enter a number: 6

That number is not prime

Last edited: