Question

In: Computer Science

A positive integer n is said to be prime (or, "a prime") if and only if...

A positive integer n is said to be prime (or, "a prime") if and only if n is greater than 1 and is divisible only by 1 and n . For example, the integers 17 and 29 are prime, but 4, 21 and 38 are not prime. Write a function named "is_prime" that takes a positive integer argument and returns as its value the integer 1 if the argument is prime and returns the integer 0 otherwise.

Can you make it in vba excel and in the form of functions because its hard for me

Solutions

Expert Solution

SOLUTION

The following function has been created as part of the VBA module for an Excel Workbook.

PROGRAM

Function is_prime(number As Integer) As Integer
    'Declare the variables
    Dim divisor As Integer
    Dim i As Long
    Dim prime As Integer
    'Initialize divisor to zero
    divisor = 0
    'Start a for loop
    For i = 1 To number
        'Check for perfect divisors
        If number Mod i = 0 Then
            'Increment the divisor by 1
            divisor = divisor + 1
        End If
    'Close loop
    Next i
    'If divisor is 2 then the number is prime and so return 1
    If divisor = 2 Then
        is_prime = 1
    Else
    'Is not prime and hence return 0
        is_prime = 0
    End If
End Function

Note:

Notice that the return variable is the same as that of the function name(is_prime)

Kindly refer to the screenshot for indentation related queries.

SCREENSHOT

PROGRAM

The VBA developer window is shown along with the corresponding Excel Workbook.

SAMPLE OUTPUT 1

Here you can see a list of numbers from A2:A13 whose values have to be determined whether they are prime or not.

In the cell B2 call the function "is_prime" by entering the formula

=is_prime(A2)

Here you are passing on the value of Cell A2 as a parameter. Click Enter. The value of either '0' or '1' is returned based on the prime property of the parameter.

Drag the fill handle till cell B13 to calculate the same for the range.

Here the function is called for a single cell output

=is_prime(27)

The result is returned to the same cell.

Hope this helps...


Related Solutions

(Prime Numbers) An integer is said to be prime if it is divisible by only 1...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write pseudocode and function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and print all the prime numbers between 1 and 1000. Display 10 numbers per line. Twin primes...
Write following program using Python: A positive integer greater than 1 is said to be prime...
Write following program using Python: A positive integer greater than 1 is said to be prime if it has no divisors other than 1 and itself. A positive integer greater than 1 is composite if it is not prime. Write a program that asks the user to enter a integer greater than 1, then displays all of the prime numbers that are less than or equal to the number entered. Last, create two files. One file will hold all of...
Suppose a is a positive integer and p is a prime/ Prove that p|a if and...
Suppose a is a positive integer and p is a prime/ Prove that p|a if and only if the prime factorization of a contains p. Can someone please show a full proof to this, thank you.
show that an integer n > 4, is prime iff it is not a divisor of...
show that an integer n > 4, is prime iff it is not a divisor of (n-1)!
Let n be a positive integer. Prove that two numbers n2+3n+6 and n2+2n+7 cannot be prime...
Let n be a positive integer. Prove that two numbers n2+3n+6 and n2+2n+7 cannot be prime at the same time.
Let n be a positive integer. Prove that if n is composite, then n has a...
Let n be a positive integer. Prove that if n is composite, then n has a prime factor less than or equal to sqrt(n) . (Hint: first show that n has a factor less than or equal to sqrt(n) )
ATestforPrimalityisthefollowing: Given an integer n > 1, to test whether n is prime check to see...
ATestforPrimalityisthefollowing: Given an integer n > 1, to test whether n is prime check to see if it is divisible by a prime number less than or equal to it’s square root. If it is not divisible by an of these numbers then it is prime. We will show that this is a valid test. prove ∀n, r, s ∈ N+, r s ≤ n → (r ≤ √n ∨ s ≤ √n) b) Prove ∀ n ∈ N+ ,...
Prove or disprove that 3|(n 3 − n) for every positive integer n.
Prove or disprove that 3|(n 3 − n) for every positive integer n.
Prove that τ(n) < 2 n for any positive integer n. This is a question in...
Prove that τ(n) < 2 n for any positive integer n. This is a question in Number theory
Please prove 1. Every positive integer is a product of prime numbers. 2. If a and...
Please prove 1. Every positive integer is a product of prime numbers. 2. If a and b are relatively prime, and a|bc, then a|c. 3. The division algorithm for F[x]. Just the existence part only, not the uniqueness part
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT