0
func step(_ g: Int, _ m: Int, _ n: Int) -> (Int, Int)? {
    var z = [m]
    var x = m
    var y = n
    while x < y {
        x += 1
        z += [x]
    }
    for i in z {
        var k = 2
        while k < n {

        if i % k != 0 && i != k {

            }
            k += 1
        }

    }
    print(z)
    return (0, 0)
}
print (step(2, 100, 130))

so it currently returns the set of numbers 100-130 in the form of an array. the overall function will do more than what i am asking about but for now i just want to create an array that takes the numbers 100-130, or more specifically the numbers x- y and returns an array of prime. the if i%k part need the help. yes i know it is redundant and elongated but im new at this. that being said try to only use the simple shortcuts. that being said i would also be ok with examples of ways to make it more efficient but im going to need explanations on some of it because.. well im new. for context assume if only been doing this for 20-30 days (coding in general)

3 Answers3

0

you can do this:

let a = 102 
let b = 576 // two numbers you want  to check within

/**** This function returns your array of primes ****/
func someFunc(x: Int, y: Int) -> [Int] {
    var array = Array(x...y) // This is a quick way to map and create array from a range . /// Array(1...5) . ---> [1,2,3,4,5]

    for element in array {
        if !isPrime(n: element) { // check if numberis prime in a for loop
            array.remove(at: array.index(of: element)!) // remove if it isnt
        }
    }

    return array
}

someFunc(x: a, y: b) //this is how you call this func.  someFunc(x: 4, y: 8) ---> [5, 7]


// THis is a supporting function to find a prime number .. pretty straight forward, explanation in source link below.
func isPrime(n: Int) -> Bool {
    if n <= 1 {
        return false
    }
    if n <= 3 {
        return true
    }
    var i = 2
    while i*i <= n {
        if n % i == 0 {
            return false
        }
        i = i + 1
    }
    return true
}

Source: Check if a number is prime?

Akshansh Thakur
  • 5,163
  • 2
  • 21
  • 38
0

Firstly, it's a good idea to separate out logic into functions where possible. E.g. Here's a generic function for calculating if a number is prime (adapted from this answer):

func isPrime<T>(_ n: T) -> Bool where T: BinaryInteger {
    guard n > 1 else {
        return false
    }
    guard n > 3 else {
        return true
    }

    var i = T(2)
    while (i * i) <= n {
        if n % i == 0 {
            return false
        }
        i += 1
    }
    return true
}

To get the numbers by step, Swift provides the stride function. So your function can simplify to:

func step(_ g: Int, _ m: Int, _ n: Int) -> (Int, Int)? {
    let z = stride(from: m, to: n, by: g).filter { isPrime($0) }
    print(z)
    return (0, 0)
}

To explain, stride will return a Sequence of the numbers that you want to step through, which you can then filter to get only those that return true when passed to the function isPrime.

By the way, your example of print(step(2, 100, 130)) should print nothing, because you'll be checking all the even numbers from 100 to 130, which will obviously be non-prime.

I'd also recommend that you don't use single-letter variable names. g, m, n and z aren't descriptive. You want clarity over brevity so that others can understand your code.

Guy Kogus
  • 7,251
  • 1
  • 27
  • 32
0

This returns an array of primes between 2 numbers:

extension Int {
  func isPrime() -> Bool {
    if self <= 3 { return self == 2 || self == 3 }
    for i in 2...self/2 {
      if self % i == 0 {
        return false
      }
    }
    return true
  }
}

func getPrimes(from start: Int, to end: Int) -> [Int] {
  var primes = [Int]()
  let range = start > end ? end...start : start...end

  for number in range {
    if number.isPrime() { primes.append(number) }
  }

  return primes
}

In the extension you basically loop through every number in between 2 and selected number/2 to check if its divisible or not and return false if it is, else it will return true.

The getPrimes() basically takes in 2 numbers, if the start number is higher than the end number they switch places (a failsafe). Then you just check if the number is prime or not with help of the extension and append the value to the array if it is prime.

func step(_ steps: Int, _ start: Int, _ end: Int) {
            var primes = [Int]()
            var number = start
            repeat {
                if number.isPrime() { primes.append(number) }
                number+=steps
            } while number <= end
        }

Here is another function if you want to take steps in the difference higher than 1

Vollan
  • 1,887
  • 11
  • 26