Last active
August 29, 2015 14:20
-
-
Save joanmolinas/6fd71ba761bcd2b7d5ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Melchoriforme Array is an Array V[1..N] with N >= 0 if some of elements is "rubio". | |
"Rubio" number is an number if your value it's equals to the sum of previous numbers. | |
Example: [1,2,5,8] -> 8 is Rubio because 1 + 2 + 5 = 8. | |
*/ | |
/* | |
This is the predicate. Pass 2 parameters: | |
v -> Array of all elements. | |
num -> value for check if "rubio". | |
@Discussion: for break when i == num, because we want check previous number until i(num). | |
Return true when sum == num. | |
*/ | |
func rubio(v : [Int], num : Int) -> Bool { | |
var sum = 0 | |
for i in v { | |
if(i == num) { break } | |
sum += i | |
} | |
return num == sum ? true : false | |
} | |
/* | |
In this function pass vector and return if vector contain an rubio number. If contain, Return true. | |
*/ | |
func melchoriforme(v : [Int]) -> Bool { | |
if v.count < 1 { return false } | |
for i in v { | |
if(rubio(v, i)) { return true } | |
} | |
return false | |
} | |
/* IN MY CASE, THIS IS MY ALGORITHM */ | |
func melchoriforme(v : [Int]) -> Bool { | |
if v.count < 1 { return false } | |
func rubio(v : [Int], num : Int) -> Bool { | |
var sum = 0 | |
for i in v { | |
if(i == num) { break } | |
sum += i | |
} | |
return num == sum ? true : false | |
} | |
for i in v { | |
if(rubio(v, i)) { return true } | |
} | |
return false | |
} | |
/* EXAMPLE */ | |
var numbers = [1,2,5,8,15,150] | |
melchoriforme(numbers) //OUTPUT -> True --> 8 is rubio. | |
var numbers1 = [1,2,5,15,150] | |
melchoriforme(numbers) //OUTPUT -> False | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment