Filtering prime numbers

Report a typo

Erick wants to create the NumberFilter interface that should implement the Process() method to filter the prime numbers from a slice with many integer numbers.

He has already created the PrimeNumbers type, the Process() method, and the isPrime() function to verify if the number is prime or not. Now you need to help Erick with the following tasks:

  1. Create the NumberFilter interface that implements the Process() method. Note thatProcess() takes a slice of integers []int as an argument;
  2. Declare the numFilter variable, and assign to it PrimeNumbers with the numbers slice as an argument;
  3. Call the Process() method on the numFilter variable and then output the filteredNumbers.

This problem uses the math/rand package to generate a slice with random numbers via the hidden generateRandomNumbers() function. However, don't be scared! You don't need to know how the math/rand package works to solve this problem.

Sample Input 1:

42

Sample Output 1:

before filtering:
 [5 87 68 50 23 45 57 76 28 43 29 67 15 28 52 53 52 1 35 4 82 34 44 75 44 61 2 19 7 79 82 2 15 10 29 98 74 67 72 7 35 87]
after filtering prime numbers:
 [5 23 43 29 67 53 61 2 19 7 79 2 29 67 7]

Sample Input 2:

30

Sample Output 2:

before filtering:
 [38 16 91 78 87 31 97 17 9 58 40 89 39 84 4 7 15 95 80 95 66 42 35 75 83 37 78 62 84 26]
after filtering prime numbers:
 [31 97 17 89 7 83 37]
Write a program in Go
package main

import (
"fmt"
"math/rand"
)

type PrimeNumbers []int

func (p PrimeNumbers) Process() []int {
var filteredNumbers []int
for _, number := range p {
if isPrime(number) {
filteredNumbers = append(filteredNumbers, number)
}
}
return filteredNumbers
}

// Create the `NumberFilter` interface that implements the `Process()` method below:
type ? interface {
?
}

func main() {
// DO NOT delete the code block below; it is used to generate a slice with random numbers:
var randNumberAmount int64
fmt.Scanln(&randNumberAmount)
numbers := generateRandomNumbers(randNumberAmount)

fmt.Println("before filtering:\n", numbers)
// Declare the `numFilter` variable and assign to it `PrimeNumbers`
// with the `numbers` slice as an argument:
numFilter := ?(?)

// Call the `Process()` method of the `numFilter` variable below:
___

Create a free account to access the full topic