Question

CODE MUST BE WRITTEN IN SWIFT programming language Write a function that takes in two positive...

CODE MUST BE WRITTEN IN SWIFT programming language

Write a function that takes in two positive integers and prints every prime number between (and including) them

Sample Input:

prime(from: 0, to: 100)

Sample Output:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code in Swift for printing every prime number between two integers is given below:

func prime(from: Int, to: Int)

{

var new_from = from

if(from < 3)

{

new_from = 3

print(2, terminator:", ")

}

for i in new_from...to

{

var flag = 0

for j in 2..<i

{

if i%j == 0

{

flag = 1

break

}

}

if flag==0

{

print(i, terminator:", ")

}

}

}

prime(from: 0, to: 100)

The function "prime" takes two integer arguments "to" and "from" and returns nothing. Since in Swift, we cannot change the value of the function parameters, we take a new variable named "new_from" initialized to the parameter "from". If the value of "from" is less than 3, then we initialize the "new_from" variable to 3 and print 2, since 2 is the only prime number less than 3.

Now, a for loop is used from the variable "new_from" to the variable "to. It takes every integer in between the two values (including the two values) and checks whether the integer is prime or not. If it is prime, it is being printed on the screen, else the loop continues to the next value.

The variable "flag" is used as an indicator to tell whether the control entered the "if" block inside the second "for" loop. If the control entered the "if" block, it means that the integer is divisible by some other integer (other than 1 of course) and the value of flag changes to 1. Now, if the value of flag is 0, it means that the control never entered the loop and hence the number is prime and we need to print it.

This is how the above code works.

The screenshot of the code is:

The screenshot of the output is:

Hope it helped. If you have any doubts or queries, please feel free to ask in the comments section. If it helped in any way, please consider giving a thumbs up.

Add a comment
Know the answer?
Add Answer to:
CODE MUST BE WRITTEN IN SWIFT programming language Write a function that takes in two positive...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and...

    USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and returns a single Boolean value representing whether the given argument is prime (True) or not (False). After writing the function, test it by using a loop to print out all the prime numbers from 1-100. To check your results, the prime numbers from 1-100 are: 2, 3, 5, 7, 11. 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,...

  • Using PHP: Write a function that: Given a numerical parameter in input, computes and prints all...

    Using PHP: Write a function that: Given a numerical parameter in input, computes and prints all the prime numbers up to that value. Example: If input parameter is 10, output is "2, 3, 5, 7" If input parameter is 100, output is "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97" Write a "tester function" that: Given a specific subset of inputs, tests the...

  • 4. Write a logical function perfect Square that receives a positive integer number and checks if ...

    write the code in C please 4. Write a logical function perfect Square that receives a positive integer number and checks if it is a perfect square or not. Note: perfect square numbers are 4, 9,16,25,36 etc.... Write a main function that makes use of the perfect Square function to find and print all perfect squares between nl and n2. nl and n2 are end values of a range introduced by the user. ■ (inactive CAT EXE) Enter end values...

  • Source: 2014 Nielsen 72 Prime Numbers A prime number is a number that is evenly divisible...

    Source: 2014 Nielsen 72 Prime Numbers A prime number is a number that is evenly divisible only by 1 and itself. The prime numbers less than 100 are listed below. 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 Choose one of these numbers at random. Find the probability that a. The number is odd b. The sum of the digits is odd c....

  • Consider the below matrixA, which you can copy and paste directly into Matlab.

    Problem #1: Consider the below matrix A, which you can copy and paste directly into Matlab. The matrix contains 3 columns. The first column consists of Test #1 marks, the second column is Test # 2 marks, and the third column is final exam marks for a large linear algebra course. Each row represents a particular student.A = [36 45 75 81 59 73 77 73 73 65 72 78 65 55 83 73 57 78 84 31 60 83...

  • this is c++ and please let the code works on vs2017. the output should be same...

    this is c++ and please let the code works on vs2017. the output should be same as the question. mes A. Hotz Programming Challenge: Prime qumber Array Values from Canvas: ICE12A Please name your source file: PC7101.CPP A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5 The number 6, however, is not prime because it can...

  • The task involves writing a C++ program that determines the prime numbers between 1 and 100....

    The task involves writing a C++ program that determines the prime numbers between 1 and 100. The steps you should follow to identify the prime numbers are the following. 1. The number 1 is not a prime number, so it should be scratched. 2. Starting from the first prime number, which is 2, you scratch all the numbers that are the multiple of 2. You should not scratch out 2 itself. 3. The next number in the sequence after the...

  • The following function is_prime() is not a very efficient prime number test: #include <stdio.h> int is_prime(int...

    The following function is_prime() is not a very efficient prime number test: #include <stdio.h> int is_prime(int n) { int d; for (d = 2; d < n; d++) { if (!(n % d)) return 0; } return 1; } int main() { if (is_prime(7)) printf("Seven is Prime!\n");    return 0; } It is unnecessary to divide n by all numbers between 2 and n - 1 to determine if n is prime. Only divisors up to and including  need to be...

  • Write a python nested for loop that prints out the following pattern 100 99 98 97...

    Write a python nested for loop that prints out the following pattern 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33...

  • 1. Forecast demand for Year 4. a. Explain what technique you utilized to forecast your demand....

    1. Forecast demand for Year 4. a. Explain what technique you utilized to forecast your demand. b. Explain why you chose this technique over others. Year 3 Year 1 Year 2 Actual Actual Actual Forecast Forecast Forecast Demand Demand Demand Week 1 52 57 63 55 66 77 Week 2 49 58 68 69 75 65 Week 3 47 50 58 65 80 74 Week 4 60 53 58 55 78 67 57 Week 5 49 57 64 76 77...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT