Find Perfect Squares in a List
Easy
You are given a list of integers called numbers.
Your task is to return a new list containing only the numbers that are perfect squares.
A perfect square is a non-negative integer that can be represented as:
n = x * x where x is an integer.
For example:
- 4 = 2 × 2
- 9 = 3 × 3
- 16 = 4 × 4
- 25 = 5 × 5
Important Restriction
- You must not use sqrt() or any built-in square-root function to determine whether a number is a perfect square.
- Instead, determine whether a number is a perfect square by generating possible integer values and checking whether any value multiplied by itself equals the original number.
- Negative numbers are not considered perfect squares.
- 0 and 1 are considered perfect squares.
Input
ri!numbers
A list of integers.
Output
Return a list containing only the perfect-square numbers from the input.
The original order of the numbers must be preserved.
Example
Input:
{4, 7, 9, 15, 16, 23, 25, 30}
Output:
{4, 9, 16, 25}
Constraints
- The input can contain positive integers, negative integers, 0, and 1.
- Negative numbers must be ignored.
- 0 and 1 are perfect squares.
- Duplicate perfect squares must be preserved.
- The output must preserve the original order.
- Do not use sqrt() or any other built-in square-root function.
Restrictions
Solve this without using sqrt().
ri! Rule Inputs
ri!numbersList of Number (Integer){
4,
7,
9,
15,
16,
23,
25,
30
}Example 1 — Case 1
Input: ri!numbers
{
4,
7,
9,
15,
16,
23,
25,
30
}Output:
{
4,
9,
16,
25
}Example 2 — Case 2
Input: ri!numbers
{
0,
1,
2,
3,
4
}Output:
{
0,
1,
4
}Example 3 — Case 3
Input: ri!numbers
{
-9,
-4,
5,
16,
20,
36
}Output:
{
16,
36
}Appian Expression Tips
- • Use
ri!variableNamefor rule inputs - • Use
a!localVariables(local!x: …, body)for locals - • Use
a!forEach(items: …, expression: fv!item)to iterate - • Use
where()/reject(fn!isnull, list)to filter