Check prime number in array - swift

Trying to write a check for Prime numbers in the array. The array is populated randomly. But when processing the array code does not work... What am I doing wrong? Thank you!
update 2
Filling the array correctly. But with the test simple number, something is wrong. Specifically what is wrong can not say, but the point is that the rules of a simple number sorting is not working.
import Foundation
func randomArray(var i:Int,var k:Int, var array: [Int]=[], var newArray: [Int]=[]) ->Int {
for i=0;i<10;i++ {
array.append(Int(arc4random_uniform(10)))
}
for i=0;i<=array.count; i++ {
for k=2; k<array[i]; k++ {
if array[i] != 0 && array[i] != 1 && array[i]%k != 0 {
newArray.append(array[i])
} else {
return 0
}
}
}
return newArray[i]
}
randomArray(0, k: 0)

It's not clear to me what you want to do.
a) If you want to generate an array of length k at random and filter for prime numbers, use the code below. Note that this can give give you an array of length between 0 to k, because they may be no prime in the randomly generated array, or every number generated was a prime:
func isPrime(num: Int) -> Bool {
if num < 2 {
return false
}
for i in 2..<num {
if num % i == 0 {
return false
}
}
return true
}
func randomArray(len: Int) -> [Int] {
var results = [Int]()
for _ in 0..<len {
results.append(Int(arc4random_uniform(10)))
}
return results.filter(isPrime)
}
b) If you want an array of k primes, use this instead:
func randomPrimeArray(len: Int) -> [Int] {
var results = [Int]()
while results.count < len {
let x = Int(arc4random_uniform(10))
if isPrime(x) {
results.append(x)
}
}
return results
}

if you want to filter prime numbers from an array use this code:
let primeNumbers = myArray.filter { number in
if number == 0 {
return false
}
return number % 2 == 0
}

Related

Prime factorization with return being a string

I am trying to solve the following coding challenge:
Given a positive number n > 1 find the prime factor decomposition of n. The result will be a string with the following form:
"(p1xxn1)(p2xxn2)...(pkxxnk)"
with the p(i) in increasing order and n(i) empty if n(i) is 1.
Example: n = 86240 should return "(2xx5)(5)(7xx2)(11)"
I believe I have figured out how to find the prime factors of a number... my problem is that I have no idea how to convert them into the form required by the question (i.e., a string where p(i) is in increasing order). I tried to convert an integer array containing the prime factors into some sort of array of tuples containing factors p and n, but I have been struggling fruitlessly for several hours.
Here is what I have so far:
func factors(_ number: Int) -> String {
var changedNumber = number
var numberArr = [Int]()
while changedNumber >= 2 {
for i in 2...changedNumber {
if changedNumber % i == 0 {
numberArr.append(i)
changedNumber /= i
break
}
}
}
}
Any insight or resources would be greatly appreciated.
func factors(_ number: Int) -> String
I think it’s a mistake to make this return the String directly. It violates the separation of responsibilities, and makes this hard to reuse.
Imagine elsewhere in a codebase that uses this function, there might be a function which tries to parse the string result of this back into an array to use it in some other way. It may sound ridiculous, but a large number of the questions we get on here are about people trying to build systems to accept silly input from other systems that they should just change instead!
Here's what I would suggest:
func primeFactorization(of value: Int) -> (factor: Int, exponent: Int) {
...
}
func format(_ primeFactors: [(factor: Int, exponent: Int)]) -> String {
return primeFactors
.map { $0.exponent == 1 ? "(\($0.factor))" : "(\($0.factor)xx\($0.exponent))" }
.joined()
}
So you can then do:
let factorization = primeFactorization(of: 86240)
// Which results in: [
// (factor: 2, exponent: 5),
// (factor: 2, exponent: 1),
// (factor: 7, exponent: 2),
// (factor: 11, exponent: 1),
// ]
// Which you can then format as this one question wants:
format(factorization) // => "(2xx5)(5)(7xx2)(11)"
For extra points, you could generify the first function into an extension on BinaryInteger, which would let you be able to write something like 86240.primeFactorization().
Just make your function group the numbers and then use each sub collection count when creating your string:
func factors(_ number: Int) -> String {
var changedNumber = number
var numberArr: [[Int]] = []
while changedNumber >= 2 {
for i in 2...changedNumber {
if changedNumber.isMultiple(of: i) {
if numberArr.last?.last == i {
numberArr[numberArr.count-1].append(i)
} else {
numberArr.append([i])
}
changedNumber /= i
break
}
}
}
return numberArr.reduce(into: "") {
if let last = $1.last {
if $1.count == 1 {
$0 += "(" + String(last) + ")"
} else {
$0 += "(" + String(last) + "xx\($1.count))"
}
}
}
}
print(factors(86240)) // (2xx5)(5)(7xx2)(11)
There's lots of ways to handle this. Here's one, off the top of my head:
Write an extension to Int that has the following functions
func isPrime() -> Bool
func nextPrime() -> Int.
First check to see if the input number n is prime. If it is, return the result as "(nxxx1)" and you're done.
Define a struct primeFactor:
struct PrimeFactor {
let value: Int
var count: Int
}
Create an array of PrimeFactors.
func primeFactorsString(of value: String) -> String {
var primeFactors = [PrimeFactor]()
var currentPrime = 1
var remainder = value
guard !value.isPrime() else { return "(\(value)xx1)" }
while remainder > 1 {
currentPrime = currentPrime.nextPrime()
if remainder % currentPrime == 0 {
let newPrimeFactor = PrimeFactor(value: currentPrime, count: 1)
remainder /= currentPrime
while remainder % currentPrime == 0 {
newPrimeFactor.count = newPrimeFactor.count + 1
remainder /= currentPrime
}
primeFactors.append(newPrimeFactor)
}
}
// Now loop through your array of primeFactors and build your output string.
return primeFactors.map { "(\($0.value)xx\($0.count))".joined()

Return value from inner closure

Can't find a solution searching for this. Classic problem - want to find if a sum exists for any pair within an Integer array such that [1,2,3,4], 7 is true
My naive solution give the error
Unexpected non-void return value in void function
I guess because I want to return from the inner forEach closure.
func pairs (_ input: [Int], _ sum: Int ) -> Bool {
input.forEach { (number) in
let secondInput = input.filter{$0 != number}
secondInput.forEach{secondNumber in
if ((secondNumber + number) == sum) {
return true
}
}
}
return false
}
How do I return?
P.S Please ignore if you're only looking to get your naive solution working.
How about this? It takes time + space complexity into consideration.
I believe this should work well for a large set or arrays
func pairs (_ input: [Int], _ sum: Int ) -> Bool {
var firstIndex = 0
var lastIndex = input.count - 1
while firstIndex != lastIndex {
let sumCalculated = input[firstIndex] + input[lastIndex]
if sumCalculated == sum {
return true
} else if sumCalculated > sum {
lastIndex = lastIndex - 1
} else if sumCalculated < sum {
firstIndex = firstIndex + 1
}
}
return false
}
forEach only iterates through the given sequence, you can't return values from a forEach closure. contains is better suited for this kind of tasks:
func pairs(_ input: [Int], _ sum: Int ) -> Bool {
return input.contains { number in
let secondInput = input.filter { $0 != number }
return secondInput.contains { secondNumber in
return secondNumber + number == sum
}
}
}
You could also try a more functional solution, that splits the problem in multiple steps:
func pairs(_ input: [Int], _ sum: Int ) -> Bool {
return input
.flatMap { input1 in input.map { input2 in (input1, input2) } } // build all combinations of numbers from the input array
.contains { input1, input2 in input1 != input2 && input1 + input2 == sum } // check if a pair made of two distinct numbers adds up to the sum
}
If you need a solution that handles any kind of inputs (e.g. only unique numbers), then the functional solution can be adapted to this:
func pairs(_ input: [Int], _ sum: Int ) -> Bool {
return input.indices
.flatMap { i1 in input.indices.map { i2 in (i1, i2) } }
.contains { i1, i2 in i1 != i2 && input[i1] + input[i2] == sum }
}

Selection sort algorithm. Swift.

There is a great resource for swift algorithms by Ray Wenderlich:
https://github.com/raywenderlich/swift-algorithm-club
One of the basic ones is "Selection Sort":
func selectionSort(_ array: [Int]) -> [Int] {
guard array.count > 1 else { return array }
var a = array
for x in 0 ..< a.count - 1 {
var lowest = x
for y in x + 1 ..< a.count {
if a[y] < a[lowest] {
lowest = y
}
}
if x != lowest {
swap(&a[x], &a[lowest])
}
}
return a
}
I was trying to understand whats happening in here and was confused with var lowest = x step.
I decided to write my own solution for the problem and didn't use that step :
func selectSortArray1(_ array: [Int]) -> [Int] {
guard array.count > 1 else {return array}
var subArray = array
for x in 0..<subArray.count - 1 {
for y in x+1..<subArray.count {
if subArray[y] < subArray[x] {
swap(&subArray[x], &subArray[y])
}
}
}
return subArray
}
Seems to work as intended.
However, I doubt myself.
Is var lowest = x required for some edge cases?

Swift return bool in method

I've made this method:
func checkScore(player: Int) -> Bool {
var checkedFields: [Int] = []
var won: Bool = false
for var i = 0; i <= 9; i += 1 {
if(winningCombinations[i] == player) {
checkedFields.append(i)
}
}
for value in winningCombinations {
var hits = 0
for n in checkedFields {
if value.contains(n){
hits += 1
}
}
if hits == 3 {
won = true
}
}
return won
}
But when I try to build it everything becomes white and the build crashes. Am I doing something wrong here? I pass the value like this:
if self.checkScore(player) {
print("Won!")
}
(I see no error message!)
Your func checkScore(player: Int) accepts player, which is of type Int.
In your code you also say : if(winningCombinations[i] == player), meaning that you expect the elements in array winningCombinations to also be of type Int
But then you say
for value in winningCombinations {
var hits = 0
for n in checkedFields {
if value.contains(n){
If value is an element in winningCombination, it means that value is an int.. how can you say value.contains(n). Int cannot perform contains operation. Arrays can.

How can I find how many useful digits are in any given a number N

A digit in the number is useful if the number is divisible by that digit.
I have been working on this for 2 days now.
Here is what I have:
func selfDivide(integer: Int) -> Int {
var numString = String(integer)
for character in numString.characters {
if character % numString == 0 {
return character
}
}
}
I'm thinking I have to find a way to use % between that string and character.
The error that I get is:
Binary operator '%' cannot be applied to characters of type 'Character' and 'String'
Here is more Swifty way using extension (Swift 4+):
public extension Int {
/// returns number of digits in Int number
public var digitCount: Int {
get {
return numberOfDigits(in: self)
}
}
/// returns number of useful digits in Int number
public var usefulDigitCount: Int {
get {
var count = 0
for digitOrder in 0..<self.digitCount {
/// get each order digit from self
let digit = self % (Int(truncating: pow(10, digitOrder + 1) as NSDecimalNumber))
/ Int(truncating: pow(10, digitOrder) as NSDecimalNumber)
if isUseful(digit) { count += 1 }
}
return count
}
}
// private recursive method for counting digits
private func numberOfDigits(in number: Int) -> Int {
if number < 10 && number >= 0 || number > -10 && number < 0 {
return 1
} else {
return 1 + numberOfDigits(in: number/10)
}
}
// returns true if digit is useful in respect to self
private func isUseful(_ digit: Int) -> Bool {
return (digit != 0) && (self % digit == 0)
}
}
Usage:
print(333444.digitCount)
print(333444.usefulDigitCount)
I would recommend doing all of the work with Int instead of converting to String. You can use % 10 to get the digits and / 10 to remove the last digit.
func selfDivide(number: Int) -> Int {
var num = number
var count = 0
while num != 0 {
let digit = abs(num % 10)
if digit != 0 && number % digit == 0 {
count += 1
}
num = num / 10
}
return count
}
Same answer provided as an extension to Int:
extension Int {
var usefulDigits: Int {
var num = self
var count = 0
while num != 0 {
let digit = abs(num % 10)
if digit != 0 && self % digit == 0 {
count += 1
}
num = num / 10
}
return count
}
}
Examples:
print(100.usefulDigits) // 1
print(123.usefulDigits) // 2
print(222.usefulDigits) // 3
print(299.usefulDigits) // 0
print(Int.max.usefulDigits) // 4
print(Int.min.usefulDigits) // 7
In a single iteration of the while loop, the function will calculate out the last digit of number and check whether the last digit of number if not equals to 0 as well as the number is divisible by the digit or not, if the result is true then the count is incremented by 1. The last line removes the last digit from the number. Same process is repeated until the number is greater than 0. At last the number of useful digits is returned finally.
extension Int {
func usefulNumCount() -> Int {
var count = 0
var num = abs(self)
while num > 0 {
let remainder = num % 10
if remainder != 0 && self % remainder == 0 {
count += 1
}
num = num / 10
}
return count
}
}
This only makes sense to me with unsigned integers.
Please let me know if you have further sense to knock into me; I tend to need it.
public extension UnsignedInteger {
/// The digits that make up this number.
/// - Parameter radix: The base the result will use.
func digits(radix: Self = 10) -> [Self] {
sequence(state: self) { quotient in
guard quotient > 0
else { return nil }
let division = quotient.quotientAndRemainder(dividingBy: radix)
quotient = division.quotient
return division.remainder
}
.reversed()
}
func usefulDigitCount(radix: Self = 10) -> Int {
digits(radix: radix).filter(isMultiple).count
}
}
In Swift 5
extension BinaryInteger {
var digits: [Int] {
return String(describing: self).compactMap { Int(String($0)) }
}
}
extension Int {
public var digitCount: Int {
get {
return self.digits.count
}
}
}
extension Int {
var usefulDigits: Int {
var count = 0
for digit in self.digits {
if digit != 0 && self % digit == 0 {
count += 1
}
}
return count
}
}