Question

4. 3323 A prime number can be divided, without a remainder, only by itself and by...

4.
3323

A prime number can be divided, without a remainder, only by itself and by 1.

Write a code segment to determine if a defined positive integer N is prime.

Create a list of integers from 2 to N-1. Use a loop to determine the remainder of N when dividing by each integer in the list. Set the variable result to the number of instances the remainder equals zero. If there are none, set result=0 (the number is prime).

Use the built-in MATLAB function rem to determine the remainder after division. (the remainder of N divided by P is rem(N,P))

For example

N=5
% list= [2,3,4]
% remainder of 5/2 is 1
% remainder of 5/3 is 2
% remainder of 5/4 is 1
result = 0 % inputted number is prime 
N=6
% list=[2,3,4,5]
% remainder of 6/2 is 0
% remainder of 6/3 is 0
% remainder of 6/4 is 2
% remainder of 6/5 is 1
result = 2 % two integers in the list have remainder=0

Homework Answers

Answer #1

Code to copy:

clc;
clear all;
close all;
N=input('Enter an integer:');
li=2:N-1;
result=0;
for i=li
if rem(N,i)==0
result=result+1;
end
end
result
if result==0
disp('Inputted number is prime');
else
fprintf('%d integers in the list have remainder=0\n',result);
end

Matlab code:

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
4. Prove that if p is a prime number greater than 3, then p is of...
4. Prove that if p is a prime number greater than 3, then p is of the form 3k + 1 or 3k + 2. 5. Prove that if p is a prime number, then n √p is irrational for every integer n ≥ 2. 6. Prove or disprove that 3 is the only prime number of the form n2 −1. 7. Prove that if a is a positive integer of the form 3n+2, then at least one prime divisor...
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. ( Java programing...
Show that there exists a prime number p such that p+4 and p+6 are also prime....
Show that there exists a prime number p such that p+4 and p+6 are also prime. [Hint: Primes are 2, 3, 5, 7, 11, 13, 17, 19, 23, ...]
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Write a python program that defines a function isPrime (number) with the following header: def isPrime (number): that checks whether a number is prime or not. Use that function in your main program to count the number of prime numbers that are less than 5000....
Prove that Z32 with the operations of [+] and [*] as defined below is not an...
Prove that Z32 with the operations of [+] and [*] as defined below is not an integral domain. The set of integers mod m is denoted Zm. The elements of Zm are denoted [x]m where x is an integer from 0 to m-1. Each element [x]m is an equivalence class of integers that have the same integer remainder as x when divided by m. For example, Z7 = {[0]7, [1]7, [2]7, [3]7, [4]7, [5]7, [6]7}. The element [5]7 represents the...
See four problems attached. These will ask you to think about GCDs and prime factorizations, and...
See four problems attached. These will ask you to think about GCDs and prime factorizations, and also look at the related topic of Least Common Multiples (LCMs). The prime factorization of numbers can be used to find the GCD. If we write the prime factorization of a and b as a = p a1 1 p a2 2 · p an n b = p b1 1 p b2 2 · p bn n (using all the primes pi needed...
Provided below is a simple data set for you to practice finding descriptive measures. For the...
Provided below is a simple data set for you to practice finding descriptive measures. For the data​ set, complete parts​ (a) through​ (c). 0, 2, 4, 5, 6, 0, 2, 4, 5, 6      a. Obtain the quartiles. Q1 = Q2 = Q3 = ​(Type integers or decimals. Do not​ round.) b. Determine the interquartile range.The interquartile range is "?" ​ (Type an integer or a decimal. Do not​ round.) c. Find the​ five-number summary. ​ ​(Type integers or decimals....
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are....
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are. Write a program that, for any given sequence of unique natural numbers, will compute the 'distance' between that original ordering and the same set of numbers sorted in ascending order. The distance should be computed by calculating how far displaced each number is in the original ordering from its correct location in the sorted list and summing those results. For instance, given the list...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in the sequence, the next number in the sequence is determined by two simple rules: If the current number n is odd, the next number in the sequence is equal to 3 * n + 1 If the current number n is even instead, the next number in the sequence is equal to one half of n (i.e., n divided by 2) We repeat this...