// Sample Code // given n, define a boolean function that tells you whether a number is a // factor of n and use that function to filter the array of numbers 1 to n def factors(n : Int) = { def isFactor(m : Int) = n % m == 0 (1 to n).toArray.filter(isFactor) } // By definition, a prime is a number greater than 2 whose only factors are 1 // and itself def prime(n : Int) = factors(n).length == 2 /////////////////////////////////////////////////////////////////////////////// // Problem #1 // write a method named product that takes an array of integers // (Array[Int]) as a parameter and returns the product of all // the integers in the array. You may assume the array contains // at least 1 number. // def product(ints : Array[Int]) = /////////////////////////////////////////////////////////////////////////////// // Problem #2 // write a method named largestPrime that takes an array of integers // (Array[Int]) as a parameter and returns the value of the largest // prime in the array. You may assume the array contains at least // 1 prime number. // HINT: the prime method is helpful // def largestPrime(ints : Array[Int]) = /////////////////////////////////////////////////////////////////////////////// // Problem #3 // write a method named retainMultiples that takes an array of integers // (Array[Int]) as a parameter and returns an array identical to the // parameter, except this array will contain only elements which are // multiples of another element in the array. For this problem, a number // is not a multiple of itself (so, 4 is the smallest multiple of 2). // For example, if given an array containing 3, 4, 2, 5, and 8, then the // result would be an array containing 4 and 8 (4 is a multiple of 2, // and 8 is a multiple of both 2 and 4). The 3, 2, and 5 are removed // because they aren't multiples of any element in the array // def retainMultiples(ints : Array[Int]) = /////////////////////////////////////////////////////////////////////////////// // uncomment these lines of code to test your methods var array = Array(3, 4, 2, 5, 8) // var productResult = product(array) // correct: 960 // var largestPrimeResult = largestPrime(array) // correct: 5 // var retainMultiplesResult = retainMultiples(array) // correct: Array(4, 8)