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)
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?
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.
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
Related
I am trying to use recursion in Swift to print out the Fibonacci sequence for a number "n" iterations. However, I keep getting the same error.
I have already tried doing it without recursion and was able to do it. However, I am now trying to do in a more complex and "computer scientisty" way by using recursion.
func fibonacciSequence (n: Int) -> [Int] {
// Consumes a number "n", which is the number of iterations to go through with the Fibonacci formula and prints such sequence.
var fibonacciArray = [Int]()
for n in 0 ... n {
if n == 0 {
fibonacciArray.append(0)
}
else if n == 1 {
fibonacciArray.append(1)
}
else {
fibonacciArray.append (fibonacciSequence(n: (n - 1)) +
fibonacciSequence(n: (n-2)))
}
}
return fibonacciArray
I expect to call the function with a number n and for the function to print out the Fibonacci sequence. Example: if n = 5, I expect the console to print 0, 1, 1, 2, 3, 5. The error I get is this: (Cannot convert value of type '[Int]' to expected argument type 'Int').
As pointed out above, the return value is causing an error when summed. A possible way (but not recursive) of fixing the code would be to simply change the else statement:
func fibonacciSequence (n: Int) -> [Int] {
// Consumes a number "n", which is the number of iterations to go through with the Fibonacci formula and prints such sequence.
var fibonacciArray = [Int]()
for n in 0 ... n {
if n == 0 {
fibonacciArray.append(0)
}
else if n == 1 {
fibonacciArray.append(1)
}
else {
fibonacciArray.append (fibonacciArray[n-1] + fibonacciArray[n-2] )
}
}
return fibonacciArray
}
A recursive solution would be the following:
func fibonacciSequence (n: Int, sumOne: Int, sumTwo: Int, counter: Int, start: Bool) {
if start {
print(0)
print(1)
}
if counter == -1 {
print(1)
}
if (counter == n - 2) {
return
}
let sum = sumOne + sumTwo
print(sum)
fibonacciSequence(n: n, sumOne: sumTwo , sumTwo: sum, counter: counter + 1, start: false)
}
fibonacciSequence(n: 8, sumOne: 0, sumTwo: 1, counter: 0, start: true)
There is probably a "nicer" way, but I hope it helps. Cheers.
These is my solution for fabonacci series in swift 5 playground
func fibonacci(n: Int) {
var num1 = 0
var num2 = 1
var nextNum = Int()
let i = 1
var array = [Int]()
array.append(num1)
array.append(num2)
for _ in i...n {
nextNum = num1 + num2
num1 = num2
num2 = nextNum
array.append(num2)
print(array)
}
print("result = \(num2)")
}
print(fibonacci(n: 5))
let fibonacci = sequence(state: (0, 1)) {(state: inout (Int, Int)) -> Int? in
defer { state = (state.1, state.0 + state.1) }
return state.0
}
//limit 10
for number in fibonacci.prefix(10) {
print(number)
}
// MARK: - Function
func fibonacciSeries(_ num1 : Int,_ num2 : Int,_ term : Int,_ termCount : Int) -> Void{
if termCount != term{
print(num1)
fibonacciSeries(num2, num2+num1, term, termCount + 1)
}
}
// MARK: - Calling Of Function fibonacciSeries(0, 1, 5, 0)
// MARK: - out Put 0 1 1 2 3
Note Need to Change only No Of term for fibonacci Series.
func fibonacci(n: Int) {
var seq: [Int] = n == 0 ? [0] : [0, 1]
var curNum = 2
while curNum < n{
seq.append(seq[curNum - 1] + seq[curNum - 2])
curNum += 1 }
print(seq) }
Recursive way of fabonacci -> Solutions
func fibo( n: Int) -> Int {
guard n > 1 else { return n }
return fibo(n: n-1) + fibo(n: n-2)
}
I want to say that if some 2d array contains the "point" format [Int,Int], then regenerate the random numbers, not counting the iteration.
for _ in 0..<33{
let j = Int(arc4random_uniform(10))
let k = Int(arc4random_uniform(10))
while live.contains(//The point j,k){
live.append(Array(arrayLiteral: j,k))
cells[j][k] = true
}
}
From what I understood your question, you want to generate an array of 2D points excluding repetition, you can use CGPoint or define your own Point
struct Point: Equatable {
let x: Int
let y: Int
}
func == (lhs: Point, rhs: Point) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
var live: [Point] = []
for _ in 0..<10{
var candidate = Point(x: Int(arc4random_uniform(10)), y: Int(arc4random_uniform(10)))
while live.contains(candidate) {
candidate = Point(x: Int(arc4random_uniform(10)), y: Int(arc4random_uniform(10)))
}
live.append(candidate)
}
or you can use tuple like so
var live: [(Int, Int)] = []
for _ in 0..<10{
var j = Int(arc4random_uniform(10))
var k = Int(arc4random_uniform(10))
while live.contains({$0 == (j, k)}) {
j = Int(arc4random_uniform(10))
k = Int(arc4random_uniform(10))
}
live.append((j,k))
}
Depending on your problem size, it might be more optimal to build an array of all possible values, and then shuffle and take first X elements every time you need new set of random points. You can optimize it further, but the code'd look similar to:
var possibleValues: [Point] = []
for x in 0..<5 {
for y in 0..<5 {
possibleValues.append(Point(x: x, y: y))
}
}
func randomPoints(numberOfPoints: Int) -> [Point] {
// shuffle original array without changing it
let shuffled = possibleValues.sorted { _ in arc4random_uniform(10) > 5 }
// take first X elements
return Array(shuffled[0..<numberOfPoints])
}
randomPoints(numberOfPoints: 10)
You can optimize this solution even further but that'd require to know more about your data set. Hope this helps
I'm trying to learn functional Swift and started doing some exercises from Project Euler.
Even Fibonacci numbers
Problem 2
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Implemented a memoized Fibonacci function, as per WWDC advanced Swift videos:
func memoize<T:Hashable, U>( body: ((T)->U,T) -> U) -> (T)->U {
var memo = [T:U]()
var result: ((T)->U)!
result = { x in
if let q = memo[x] { return q }
let r = body(result,x)
memo[x] = r
return r
}
return result
}
let fibonacci = memoize { (fibonacci:Int->Double,n:Int) in n < 2 ? Double(n) : fibonacci(n-1) + fibonacci(n-2) }
and implemented a class that conforms to the Sequence protocol
class FibonacciSequence: SequenceType {
func generate() -> GeneratorOf<Double> {
var n = 0
return GeneratorOf<Double> { fibonacci(n++) }
}
subscript(n: Int) -> Double {
return fibonacci(n)
}
}
The first (non-functional) solution of the problem:
var fib = FibonacciSequence().generate()
var n:Double = 0
var sum:Double = 0
while n < Double(4_000_000) {
if n % 2 == 0 {
sum += n
}
n = fib.next()!
}
println(sum)
The second, more functional solution, using ExSwift for it's takeWhile function
let f = FibonacciSequence()
println((1...40).map { f[$0] }
.filter { $0 % 2 == 0 }
.takeWhile { $0 < 4_000_000 }
.reduce(0, combine: +))
I'd like to improve on this solution, because of the 1...40 range at the begging that's calculating too many terms for no reason. Ideally I'd like to be able to have some sort of infinite range, but at the same time only calculate the required terms that satisfy the condition in the takeWhile
Any suggestions ?
Here I generate the sequence that already stops once max value is reached.
Then you just need to reduce without filtering, just sum 0 when n is odd.
func fibonacciTo(max: Int) -> SequenceOf<Int> {
return SequenceOf { _ -> GeneratorOf<Int> in
var (a, b) = (1, 0)
return GeneratorOf {
(b, a) = (a, b + a)
if b > max { return nil }
return b
}
}
}
let sum = reduce(fibonacciTo(4_000_000), 0) {a, n in (n % 2 == 0) ? a + n : a }
As an alternative, if you wish to keep fibonacci a more general function you could extend SequenceOf with takeWhile and reduce1 obtaining something that resembles function composition:
extension SequenceOf {
func takeWhile(p: (T) -> Bool) -> SequenceOf<T> {
return SequenceOf { _ -> GeneratorOf<T> in
var generator = self.generate()
return GeneratorOf {
if let next = generator.next() {
return p(next) ? next : nil
}
return nil
}
}
}
// Reduce1 since name collision is not resolved
func reduce1<U>(initial: U, combine: (U, T) -> U) -> U {
return reduce(self, initial, combine)
}
}
func fibonacci() -> SequenceOf<Int> {
return SequenceOf { _ -> GeneratorOf<Int> in
var (a, b) = (1, 0)
return GeneratorOf {
(b, a) = (a, b + a)
return b
}
}
}
let sum2 = fibonacci()
.takeWhile({ $0 < 4_000_000 })
.reduce1(0) { a, n in (n % 2 == 0) ? a + n : a}
Hope this helps
There is a filter() function which takes a sequence as an argument:
func filter<S : SequenceType>(source: S, includeElement: (S.Generator.Element) -> Bool) -> [S.Generator.Element]
but since the return value is an array, this is not suited if you want
to work with an "infinite" sequence. But with
lazy(FibonacciSequence()).filter ( { $0 % 2 == 0 })
you get an "infinite" sequence of the even Fibonacci numbers. You cannot
call the .takeWhile() method of ExSwift on that sequence because
.takeWhile() is only defined for struct SequenceOf and not for
general sequences. But
TakeWhileSequence(
lazy(FibonacciSequence()).filter ( { $0 % 2 == 0 }),
{ $0 < 4_000_000 }
)
works and gives the sequence of all even Fibonacci numbers less than
4,000,000. Then
let sum = reduce(TakeWhileSequence(
lazy(FibonacciSequence()).filter ( { $0 % 2 == 0 }),
{ $0 < 4_000_000 }), 0, +)
gives the intended result and computes only the "necessary"
Fibonacci numbers.
Note that there is no actual need to memoize the Fibonacci numbers
here because they are accessed sequentially. Also (as #Matteo
already noticed), all Fibonacci numbers are integers. So you could
define the sequence more simply as
struct FibonacciSequence : SequenceType {
func generate() -> GeneratorOf<Int> {
var current = 1
var next = 1
return GeneratorOf<Int>() {
let result = current
current = next
next += result
return result
};
}
}
and the above computation does still work.
You can get quite close to what you want by using Swift's lazy sequences. If you take your generator of fibonacci numbers (here's the one I'm using:)
var (a, b) = (1, 0)
var fibs = GeneratorOf<Int> {
(b, a) = (a, b + a)
return b
}
You can wrap it in lazy():
var (a, b) = (1, 0)
var fibs = lazy(
GeneratorOf<Int> {
(b, a) = (a, b + a)
return b
}
)
Which exposes it to filter() as a lazy function. This filter() returns:
LazySequence<FilterSequenceView<GeneratorOf<Int>>>
Now, to get your takeWhile() function, you'd need to extend LazySequence:
extension LazySequence {
func takeWhile(condition: S.Generator.Element -> Bool)
-> LazySequence<GeneratorOf<S.Generator.Element>> {
var gen = self.generate()
return lazy( GeneratorOf{ gen.next().flatMap{ condition($0) ? $0 : nil }})
}
}
So that it returns nil (stops the generator) if either the underlying sequence ends, or the condition isn't satisfied.
With all of that, your fibonacci sequence under a given number looks a lot like what you wanted:
fibs
.filter {$0 % 2 == 0}
.takeWhile {$0 < 100}
//2, 8, 34
But, because reduce isn't a method on LazySequence, you have to convert to an array:
fibs
.filter {$0 % 2 == 0}
.takeWhile {$0 < 100}.array
.reduce(0, combine: +)
//44
You could do a quick and dirty extension to LazySequence to get reduce():
extension LazySequence {
func reduce<U>(initial: U, combine: (U, S.Generator.Element) -> U) -> U {
var accu = initial
for element in self { accu = combine(accu, element) }
return accu
}
}
And you can write the final thing like this:
fibs
.filter {$0 % 2 == 0}
.takeWhile {$0 < 100}
.reduce(0, combine: +)
//44
All of those sequences get to persist in their laziness - fibs is infinite, so they wouldn't really work otherwise. In fact, nothing is calculated until reduce: it's all thunks until then.
In Swift 3.1, here's an iterator that generates Fibonacci numbers forever, and an infinite sequence derived from it:
class FibIterator : IteratorProtocol {
var (a, b) = (0, 1)
func next() -> Int? {
(a, b) = (b, a + b)
return a
}
}
let fibs = AnySequence{FibIterator()}
You can get the sum of the even-numbered terms under four million like this:
fibs.prefix{$0 < 4000000}.filter{$0 % 2 == 0}.reduce(0){$0 + $1}
Be warned that filter and map are strict by default, and will run forever on an infinite Sequence. In the example above, this doesn't matter since prefix returns only a finite number of values. You can call .lazy to get a lazy Sequence where filter and map will behave non-strictly. For example, here are the first 5 even Fibonacci numbers:
> print( Array(fibs.lazy.filter{$0 % 2 == 0}.prefix(5)) )
[2, 8, 34, 144, 610]
Below is my swift function to calculate the sum of multiples of a given set of real numbers in a range from 0 to 'upto'. The algorithm loops through from 1 to the upper limit and maps the array of multiples to an array of modulos for the specified multipliers and then reduces that array using multiplication such that if a given value of i is evenly divisible by any of the given multipliers the result of the reduce will be 0.
func sumOfMultiples(mults: [Int], upto: Int) -> {
var acc: Int = 0
for i in 1...upto {
if mults.map({i % $0}).reduce(1,*) == 0 {
acc += i
}
}
return acc
}
The problem is I'm getting a compile error
error: 'Int' is not a subtype of '()'
if mults.map({i % 0}).reduce(1,*) == 0 {
// Methinks you're missing this: ~~~~~~~~vvv
func sumOfMultiples(mults: [Int], upto: Int) -> Int {
func sumOfMultiples(mults: [Int], upto: Int) -> {
var acc: Int = 0
for i in 1...upto {
if mults.map( { (x) in i % x } ).reduce(1,*) == 0 {
acc += i
}
}
return acc
}
Here's a very simple recursive function:
func lap (n: Int) -> Int {
if n == 0 { return 0 }
return lap (n - 1)
}
If I want to convert it as closure:
let lap = {
(n: Int) -> Int in
if n == 0 { return 0 }
return lap (n - 1)
}
I got a compiler error: "Variable used within its own initial value"
you can workaround it with two step assignment
var lap : (Int) -> Int!
lap = {
(n: Int) -> Int in
if n == 0 { return 0 }
return lap(n - 1)
}
or you can use Y combinator
func Y<T, R>( f: (T -> R) -> (T -> R) ) -> (T -> R) {
return { t in f(Y(f))(t) }
}
let lap = Y {
(f : Int -> Int) -> (Int -> Int) in
return { (n : Int) -> Int in return n == 0 ? 0 : f(n - 1) }
}
// with type inference
let lap2 = Y {
f in { n in n == 0 ? 0 : f(n - 1) }
}
This is a workaround of the memory leak problem that #zneak found (It doesn't have memory leak but captured the wrong value)
func f(n: Int) {
var f = Foo()
var lap: #objc_block (Int)->Int = { $0 }
var obj: NSObject = reinterpretCast(lap)
lap = {
[weak obj] (n: Int) -> Int in // unowned will cause crush
if n == 0 { return 0 }
println(f)
var lap2 : #objc_block (Int)->Int = reinterpretCast(obj)
return lap2 (n - 1)
}
lap(n)
}
for i in 0..<5 {
f(i)
}
class Foo {
init() {
println("init");
}
deinit {
println("deinit")
}
}
EDIT This has been resolved with Swift 2 using nested functions. Apple suggests this code:
func f(n: Int) {
func lap(n: Int) -> Int {
if n == 0 { return 0 }
print(n)
return lap(n - 1)
}
lap(n)
}
for i in 0..<1000000 { f(i) }
Although this is not obvious from the current example, so-called local functions capture the locals of the enclosing scope.
Using a location function does not leak, whereas a closure would. However, clearly, lap can't be reassigned in this case.
I received an email from Apple's Joe Groff stating that they still plan on making it possible to capture closures as weak and mutable variables at a later point. This does confirm, however, that there's no way to do it right now except with a local function.
Your current solution has a memory leak in it: lap's closure has a strong reference to itself, meaning that it cannot ever be released. This can easily be verified by launching the following program with the Leaks instrument attached:
import Foundation
func f(n: Int) {
var lap: (Int)->Int = { $0 }
lap = {
(n: Int) -> Int in
if n == 0 { return 0 }
println(n)
return lap (n - 1)
}
lap(n)
}
for i in 0..<1000000 {
f(i)
}
Unfortunately, as the explicit capture syntax cannot be applied to closure types (you get an error that says "'unowned' cannot be applied to non-class type '(Int) -> Int'"), there appears to be no easy way to achieve this without leaking. I filed a bug report about it.
Here's a response to my own question:
var lap: (Int)->Int = { $0 }
lap = {
(n: Int) -> Int in
if n == 0 { return 0 }
println(n)
return lap (n - 1)
}
What about this:
let lap = {(Void) -> ((Int) -> Int) in
func f(n: Int) -> Int {
print(n)
return n == 0 ? 0 : f(n - 1)
}
return f
}()
It's quite simple, I've just defined a recursive local function inside a closure which returns the function.
However, I have to say that the answer from #Bryan Chen about the Y combinator is awesome.
I had the same problem and was not statisfied with anything that was out there, so I created a library and made it available on GitHub.
Using this library (with Swift 3.0) your code would look like this:
let lap = Recursion<Int, Int> { (n, f) in
n == 0 ? 0 : f(n-1)
}.closure