Print all the prime numbers from 1 to N Prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
func prime(n:Int) {
for index in 1...n
{
if(!isNotPrime(index, n: n)){
print(index)
}
}
}
func isNotPrime(x:Int,n:Int)->Bool {
for index in 2...n^2
{
if((x%index == 0)&&(index != x)){
return true}
}
return false;
}
prime(100)
Create power function x in the power of y without using the func pow
func power(x:Double,y:Double)->Double
{
if(y==0){
return 1.0
}
var temp:Double = 1.0
for _ in 1...Int(y){
temp = temp * x
}
print (temp)
if(y < 0){
temp = Double(1) / Double(temp)
}
return temp<br>
}
power(3.0,y: 4.0)
use recursion
func power2(x:Int,y:Int)->Int
{
if(y==0){
return 1
}
return x * power2(x,y:y-1)
}
power2(3,y:4)