How to find all numbers divisible by another number in swift? - swift

How do I find all the numbers divisible by another number in swift that have a remainder of 0? This is a Fizzbuzz related question.
Lets say that...
let number = 150
And I want to do something like...
print("Fizz") // for all the numbers where the remainder of number % 3 == 0.
So if number was 15, it would print "Fizz" 5 times.

This will work
let number = 150
for num in 1...number {
if num % 3 == 0 {
print("Fizz :\(num)")
}
}

you can just loop through the number and check with your desired divisible number if the remainder is 0 then print fizz
let number = 15
for i in 0..<number {
if i % 3 == 0 {
print("\(i) Fizz")
}
}
It will print Fizz 5 times with the i value, that which number is Fizz.

Simply try this code: (You can simply replace num with any Int number and divider that is also an Int value which is used to divide all numbers till num. )
override func viewDidLoad() {
let num:Int = 15
let divider:Int = 3
var counter:Int = divider
while counter <= num {
print("Fizz")
counter += divider
}
}

func fizzbuzz(number: Int) -> String {
if number % 3 == 0 && number % 5 == 0 {
return "Fizz Buzz"
} else if number % 3 == 0 {
return "Fizz"
} else if number % 5 == 0 {
return "Buzz"
} else {
return String(number)
}
}
https://www.hackingwithswift.com/guide/ios-classic/1/3/challenge

Related

Prime numbers print from range 2...100

I have been assigned with a task to print prime numbers from a range 2...100. I've managed to get most of the prime numbers but can't figure out how to get rid of 9 and 15, basically multiples of 3 and 5. Please give me your suggestion on how can I fix this.
for n in 2...20 {
if n % 2 == 0 && n < 3{
print(n)
} else if n % 2 == 1 {
print(n)
} else if n % 3 == 0 && n > 6 {
}
}
This what it prints so far:
2
3
5
7
9
11
13
15
17
19
One of effective algorithms to find prime numbers is Sieve of Eratosthenes. It is based on idea that you have sorted array of all numbers in given range and you go from the beginning and you remove all numbers after current number divisible by this number which is prime number. You repeat this until you check last element in the array.
There is my algorithm which should do what I described above:
func primes(upTo rangeEndNumber: Int) -> [Int] {
let firstPrime = 2
guard rangeEndNumber >= firstPrime else {
fatalError("End of range has to be greater than or equal to \(firstPrime)!")
}
var numbers = Array(firstPrime...rangeEndNumber)
// Index of current prime in numbers array, at the beginning it is 0 so number is 2
var currentPrimeIndex = 0
// Check if there is any number left which could be prime
while currentPrimeIndex < numbers.count {
// Number at currentPrimeIndex is next prime
let currentPrime = numbers[currentPrimeIndex]
// Create array with numbers after current prime and remove all that are divisible by this prime
var numbersAfterPrime = numbers.suffix(from: currentPrimeIndex + 1)
numbersAfterPrime.removeAll(where: { $0 % currentPrime == 0 })
// Set numbers as current numbers up to current prime + numbers after prime without numbers divisible by current prime
numbers = numbers.prefix(currentPrimeIndex + 1) + Array(numbersAfterPrime)
// Increase index for current prime
currentPrimeIndex += 1
}
return numbers
}
print(primes(upTo: 100)) // [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]
print(primes(upTo: 2)) // [2]
print(primes(upTo: 1)) // Fatal error: End of range has to be greater than or equal to 2!
what is the Prime num : Prime numbers are the positive integers having only two factors, 1 and the integer itself,
//Funtion Call
findPrimeNumberlist(fromNumber: 1, toNumber: 100)
//You can print any range Prime number using this fucntion.
func findPrimeNumberlist(fromNumber:Int, toNumber: Int)
{
for i in fromNumber...toNumber
{
var isPrime = true
if i <= 1 { // number must be positive integer
isPrime = false
}
else if i <= 3 {
isPrime = true
}
else {
for j in 2...i/2 // here i am using loop from 2 to i/2 because it will reduces the iteration.
{
if i%j == 0 { // number must have only 1 factor except 1. so use break: no need to check further
isPrime = false
break
}
}
}
if isPrime {
print(i)
}
}
}
func getPrimeNumbers(rangeOfNum: Int) -> [Int]{
var numArr = [Int]()
var primeNumArr = [Int]()
var currentNum = 0
for i in 0...rangeOfNum{
currentNum = i
var counter = 0
if currentNum > 1{
numArr.append(currentNum)
for j in numArr{
if currentNum % j == 0{
counter += 1
}
}
if counter == 1{
primeNumArr.append(currentNum)
}
}
}
print(primeNumArr)
print(primeNumArr.count)
return primeNumArr
}
Then just call the function with the max limit using this
getPrimeNumbers(rangeOfNum: 100)
What is happening in above code:
The numArr is created to keep track of what numbers have been used
Any number that is prime number is added/appended to primeNumArr
Current number shows the number that is being used at the moment
We start from 0 ... upto our range where we need prime numbers upto (with little modification it can be changed if the range starts from other number beside 0)
Remember, for a number to be Prime it should have 2 divisor means should be only completely divisible by 2 numbers. First is 1 and second is itself. (Completely divisible means having remainder 0)
The counter variable is used to keep count of how many numbers divide the current number being worked on.
Since 1 is only has 1 Divisor itself hence its not a Prime number so we start from number > 1.
First as soon as we get in, we add the current number being checked into the number array to keep track of numbers being used
We run for loop to on number array and check if the Current Number (which in our case will always be New and Greater then previous ones) when divided by numbers in numArr leaves a remainder of 0.
If Remainder is 0, we add 1 to the counter.
Since we are already ignoring 1, the max number of counter for a prime number should be 1 which means only divisible by itself (only because we are ignoring it being divisible by 1)
Hence if counter is equal to 1, it confirms that the number is prime and we add it to the primeNumArr
And that's it. This will give you all prime numbers within your range.
PS: This code is written on current version of swift
Optimised with less number of loops
Considered below conditions
Even Number can not be prime number expect 2 so started top loop form 3 adding 2
Any prime number can not multiplier of even number expect 2 so started inner loop form 3 adding 2
Maximum multiplier of any number if half that number
var primeNumbers:[Int] = [2]
for index in stride(from: 3, to: 100, by: 2) {
var count = 0
for indexJ in stride(from: 3, to: index/2, by: 2) {
if index % indexJ == 0 {
count += 1
}
if count == 1 {
break
}
}
if count == 0 {
primeNumbers.append(index)
}
}
print("primeNumbers ===", primeNumbers)
I finally figured it out lol, It might be not pretty but it works haha, Thanks for everyone's answer. I'll post what I came up with if maybe it will help anyone else.
for n in 2...100 {
if n % 2 == 0 && n < 3{
print(n)
} else if n % 3 == 0 && n > 6 {
} else if n % 5 == 0 && n > 5 {
} else if n % 7 == 0 && n > 7{
} else if n % 2 == 1 {
print(n)
}
}

display the even numbers from 1 to 500 using a while loop and the break keyword in swift

My question is as on the title. I'm trying to print even numbers from 1 to 500 suing a while loop and break keyword. Below is my best possible answer I can think of, but this only print number 2. I've been spending hours but I wasn't able to solve it.
var number = 0
while true{
number += 2
print(number)
if number % 2 == 0 && number <= 500 {
break
}
}
You can use Stride
for evenNumber in stride(from: 0, through: 500, by: 2) {
print(evenNumber)
}
To specifically do this with while and break:
var i = 0
while true {
print(i)
i += 2
if i > 500 {
break
}
}
for i in 0...500 {
if i % 2 == 0 {
print(i)
}
}
Use below code
var numbers = 0...500
for number in numbers {
if number % 2 == 0 {
print(number)
}
}
I think it's easier to use build-in stride
let arr = Array(stride(from: 0, to: 502, by: 2))
print(arr)
//
For manually
var counter = 0
var arr = [Int]()
while counter <= 500 {
if counter % 2 == 0 {
print(counter)
arr.append(counter)
}
counter += 1
}
var number = 0
while true {
number += 2
print(number)
// ↓ Your code goes here ↓
if number > 499 {
break
}
}

Identify prime number

For some reason, it always gave me the wrong result. It's always isItPrime = true no matter what number was assigned to the "number" variable.
This is my code:
let number = 6
var i = 1
var isItPrime: Bool?
while i < number {
if number % i == 0 {
isItPrime = false
} else {
isItPrime = true
}
i += 1
}
print(isItPrime)
Can somebody explain to me what's wrong with my code and why the isItPrime bool outputs always true ?
Problem 1
The last iteration of your while loop
while i < number {
if number % i == 0 {
isItPrime = false
} else {
isItPrime = true
}
i += 1
}
does overwrite the result.
So you always end up with the following result
if number % (number-1) == 0 {
isItPrime = false
} else {
isItPrime = true
}
Problem 2
Finally every number can be divided by 1, so you should start i from 2.
So
let number = 6
var i = 2
var isItPrime = true
while i < number {
if number % i == 0 {
isItPrime = false
break
}
}
print(isItPrime)
Refactoring
You can write a similar logic using Functional Programming
let number = 5
let upperLimit = Int(Double(number).squareRoot())
let isPrime = !(2...upperLimit).contains { number % $0 == 0 }
Because isItPrime is overwritten in subsequent iterations, the last number which is checked, which is number - 1 will always set isItPrime to true, because number and number - 1 are coprime.
Instead of saving the value to a boolean, just end the loop when you found out that the number is not a prime:
let number = 6
var isItPrime: Bool = true
for i in 2 ..< number {
if number % i == 0 {
isItPrime = false
break // end the loop, as we know that the number is not a prime.
}
}
print(isItPrime)
When dealing with problems like this, don't be afraid to take out a piece of paper and manually see what is going on in your loop.
Your loop will go from i = 1 to number = 5 (because of the < operator.
With that in mind, we perform each iteration manually.
for i = 1, number = 6
6 mod 1 = 0, isItPrime = false
for i = 2, number = 6
6 mod 2 = 0, isItPrime = false
for i = 3, number = 6
6 mod 3 = 0, isItPrime = false
for i = 4, number = 6
6 mod 4 = 2, isItPrime = true
Last iteration of the loop, for i = 5, number = 6
6 mod 5 = 1, isItPrime = true
There we can see that the problem is that the last iteration will always have a module of 1, therefore resulting in in your else clause getting executed.
It is always returning true because your while loop isn't working the way you want. Currently, it loops until i is 1 less than number. During that final run through the loop, number % i == 0 is false, so your code sets isItPrime to true.
To fix this problem, try this code:
let number = 6
var i = 2
var isItPrime: Bool?
while (i < number || isItPrime == false) {
if number % i == 0 {
isItPrime = false
} else {
isItPrime = true
}
i += 1
}
print(isItPrime)
You may have noticed I set i to 2, because any number modulo (%) 1 is 0
I think it's worth pointing out, however, that:
You should probably make this a method
If you initially set isItPrime to true, you can dispense with the else part of your if-else statement
Hope this helps!
// MARK: - Function
func primeNo(_ num:Int,_ divisor :Int = 2){
if divisor == num{
print("Num is prime")
}else{
if num%divisor != 0{
primeNo(num, divisor + 1)
}else{
print("num is not prime")
}
}
}
// MARK: - Use
primeNo(6)
out Put
num is not prime

Sum of Printed For Loop in Swift

For a project, I'm trying to find the sum of the multiples of both 3 and 5 under 10,000 using Swift. Insert NoobJokes.
Printing the multiples of both 3 and 5 was fairly easy using a ForLoop, but I'm wondering how I can..."sum" all of the items that I printed.
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0 {
print(i)
}
}
(468 individual numbers printed; how can they be summed?)
Just a little walk through about the process. First you will need a variable which can hold the value of your sum, whenever loop will get execute. You can define an optional variable of type Int or initialize it with a default value same as I have done in the first line. Every time the loop will execute, i which is either multiple of 3 or 5 will be added to the totalSum and after last iteration you ll get your result.
var totalSum = 0
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0
{
print(i)
totalSum = totalSum + i
}
}
print (totalSum)
In Swift you can do it without a repeat loop:
let numberOfDivisiblesBy3And5 = (0...10000).filter{ $0 % 3 == 0 || $0 % 5 == 0 }.count
Or to get the sum of the items:
let sumOfDivisiblesBy3And5 = (0...10000).filter{ $0 % 3 == 0 || $0 % 5 == 0 }.reduce(0, {$0 + $1})
range : to specify the range of numbers for operation to act on.
here we are using filter method to filter out numbers that are multiple of 3 and 5 and then sum the filtered values.
(reduce(0,+) does the job)
let sum = (3...n).filter({($0 % 3) * ($0 % 5) == 0}).reduce(0,+)
You just need to sum the resulting i like below
var sum = 0
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0 {
sum = sum + i
print(i)
}
}
Now sum contains the Sum of the values
Try this:
var sum = 0
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0 {
sum = sum + i
print(i)
}
}
print(sum)
In the Bottom line, this should to be working.
var sum = 0
for i in 0...10000 {
if i % 3 == 0 || i % 5 == 0 {
sum += i
print(i)
}
}
print(sum)

Prime number determination program

What's wrong with my logic? How can 9 come out of this loop both as a prime and not a prime number?
This works as expected for 0, 1, 2, 3, 4, 5, 6, 7, 8 but gets hung up on 9...
var userInput = 9
if userInput == 0 {
print("0 is not a prime number")
} else if userInput == 1 {
print("1 is not a prime number")
} else if userInput == 2 {
print("2 is a prime number")
} else {
for var i = 2; i < userInput; i = i + 1 {
if userInput % i == 0 {
print("\(userInput) is not a prime number")
break
} else {
print("\(userInput) is a prime number")
break
}
}
}
Remove one of the break statements. The loop needs to run completely until it finds a condition where it isn't a prime number. If it does not find said condition, it is a prime number.
var userInput = 9
if userInput == 0 {
print("0 is not a prime number")
} else if userInput == 1 {
print("1 is not a prime number")
} else if userInput == 2 {
print("2 is a prime number")
} else {
for var i = 2; i < userInput; i = i + 1 {
if userInput % i == 0 {
print("\(userInput) is not a prime number")
break
} else {
print("\(userInput) is a prime number")
// no break here
}
}
}
Or a bit more useful :
The logic is embedded in a function so you can use return for the control flow instead of break. Because there is a return true at the end you only have to look for false conditions. If it is not a prime you use return false to escape from the function and the return true at the end will never be called.
extension Int {
func isPrimeNumber() -> Bool {
switch self {
case 0 : return false
case 1 : return false
default :
for i in 2..<self {
if (self % i) == 0 {
return false
}
}
}
return true
}
}
userInput.isPrimeNumber()
The function is placed in an extension to Int so you can just call the function from userInput.
This logic is flawed:
for var i = 2; i < userInput; i = i + 1 {
if userInput % i == 0 {
print("\(userInput) is not a prime number")
break
} else {
print("\(userInput) is a prime number")
break
}
}
The if-else is wrong. Rather, you need to cycle through the entire if part of the for-loop first, testing whether each number is a factor (if userInput % i == 0), over and over; then and only then, if you have finished the loop and still have not discovered a factor, can you declare that this must be a prime.
However, you'll find it hard to succeed in writing that logic if you set everything up at a flat top level as you have done. The problem is that you have no way to do a true exit when you're at top level. Your logic thus requires that you put everything inside a function, from which you can do a forced early exit by saying return.
In this rewrite, I've done that, plus I've used a switch which is clearer (and Swiftier) than your if...else if:
func testForPrime(userInput:Int) {
switch userInput {
case 0: print("0 is not a prime number")
case 1: print("1 is not a prime number")
case 2: print("2 is a prime number")
default:
for i in 2..<userInput {
if userInput % i == 0 {
print("\(userInput) is not a prime number")
return
}
}
print("\(userInput) is a prime number")
}
}
And here is how to test it:
for i in 0...20 {testForPrime(i)}
Output:
0 is not a prime number
1 is not a prime number
2 is a prime number
3 is a prime number
4 is not a prime number
5 is a prime number
6 is not a prime number
7 is a prime number
8 is not a prime number
9 is not a prime number
10 is not a prime number
11 is a prime number
12 is not a prime number
13 is a prime number
14 is not a prime number
15 is not a prime number
16 is not a prime number
17 is a prime number
18 is not a prime number
19 is a prime number
20 is not a prime number
(Also please note that I've used a Swift-style for-loop instead of your C-style for-loop. You should get used to the Swift style, because the C-style for-loop will be deleted from the language soon.)
My solutions look terrible in the comment box. I've re-pasted them here. Thank you again for all of your help!
var uI = 9
var isPrime = true
if uI == 0 || uI == 1 {
isPrime = false
}
for var i = 2; i < uI; i++ {
if uI % i == 0 {
isPrime = false
}
}
if isPrime {
print("\(uI) is prime!")
} else {
print("\(uI) is not prime”)
}
var uI = 11
var isPrime = true
if uI == 0 || uI == 1 {
isPrime = false
}
var i = 2
while i < uI {
if uI % i == 0 {
isPrime = false
}
i++
}
if isPrime {
print("\(uI) is prime!”)
} else {
print("\(uI) is not prime”)
}