Given a positive integer num, return true
if num
is a perfect square or false
otherwise.
A perfect square is an integer that is the square of an integer. In other words, it is the product of some integer with itself.
You must not use any built-in library function, such as sqrt
.
Example 1:
Input: num = 16 Output: true Explanation: We return true because 4 * 4 = 16 and 4 is an integer.
Example 2:
Input: num = 14 Output: false Explanation: We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
Constraints:
1 <= num <= 231 - 1
program main
implicit none
integer :: num
logical :: is_perfect_square
! Example 1
num = 16
write(*,*) 'Example 1'
write(*,*) 'Input: ', num
write(*,*) 'Output: ', is_perfect_square(num)
! Example 2
num = 14
write(*,*) 'Example 2'
write(*,*) 'Input: ', num
write(*,*) 'Output: ', is_perfect_square(num)
contains
function is_perfect_square(num) result(res)
implicit none
integer, intent(in) :: num
logical :: res
integer :: i
do i = 1, int(sqrt(real(num)))
if (i * i == num) then
res = .true.
exit
end if
end do
end function is_perfect_square
end program main
temp.f95:21:31: 21 | function is_perfect_square(num) result(res) | 1 Error: Procedure βis_perfect_squareβ at (1) has an explicit interface from a previous declaration temp.f95:22:21: 22 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:23:34: 23 | integer, intent(in) :: num | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:24:22: 24 | logical :: res | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:25:20: 25 | integer :: i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:27:38: 27 | do i = 1, int(sqrt(real(num))) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:28:34: 28 | if (i * i == num) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:29:28: 29 | res = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:30:20: 30 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:31:15: 31 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:32:11: 32 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:33:7: 33 | end function is_perfect_square | 1 Error: Expecting END PROGRAM statement at (1)
program perfect_square
implicit none
integer :: num, i, j
logical :: is_perfect_square
! Read the input number
read(*,*) num
! Initialize the result to false
is_perfect_square = .false.
! Check if the number is a perfect square
do i = 1, num
j = i * i
if (j == num) then
is_perfect_square = .true.
exit
end if
end do
! Print the result
if (is_perfect_square) then
print *, "True"
else
print *, "False"
end if
end program perfect_square
This script defines a variable `num` to store the input number, and a variable `is_perfect_square` to store the result. The `read` statement is used to read the input number from the standard input. The `implicit none` statement is used to prevent the use of any implicit variables.
The `do` loop is used to check if the number is a perfect square. The loop variable `i` is initialized to 1 and the loop condition is `i <= num`. The loop body is executed for each value of `i` from 1 to `num`. The variable `j` is initialized to `i * i`, which is the square of `i`. The loop exits when `j == num`, which means that `num` is a perfect square.
The `if` statement is used to set the value of `is_perfect_square` based on the result of the loop. If `j == num`, then `is_perfect_square` is set to `.true.`. Otherwise, `is_perfect_square` is set to `.false.`.
The `print` statement is used to print the result to the standard output. If `is_perfect_square` is `.true.`, then the string "True" is printed. Otherwise, the string "False" is printed.
This script runs with all provided examples and outputs to stdout.
Note that this script does not use any built-in library function, such as `sqrt`, to solve the problem. Instead, it uses a simple loop to check if the number is a perfect square.
temp.f95:31:132: 31 | This script defines a variable `num` to store the input number, and a variable `is_perfect_square` to store the result. The `read` statement is used to read the input number from the standard input. The `implicit none` statement is used to prevent the use of any implicit variables. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:33:132: 33 | The `do` loop is used to check if the number is a perfect square. The loop variable `i` is initialized to 1 and the loop condition is `i <= num`. The loop body is executed for each value of `i` from 1 to `num`. The variable `j` is initialized to `i * i`, which is the square of `i`. The loop exits when `j == num`, which means that `num` is a perfect square. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:35:132: 35 | The `if` statement is used to set the value of `is_perfect_square` based on the result of the loop. If `j == num`, then `is_perfect_square` is set to `.true.`. Otherwise, `is_perfect_square` is set to `.false.`. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:37:132: 37 | The `print` statement is used to print the result to the standard output. If `is_perfect_square` is `.true.`, then the string "True" is printed. Otherwise, the string "False" is printed. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:41:132: 41 | Note that this script does not use any built-in library function, such as `sqrt`, to solve the problem. Instead, it uses a simple loop to check if the number is a perfect square. | 1 Error: Line truncated at (1) [-Werror=line-truncation] f951: some warnings being treated as errors
def is_perfect_square(num: int) -> bool:
if num < 2:
return True
left, right = 2, num // 2
while left <= right:
mid = left + (right - left) // 2
square = mid * mid
if square == num:
return True
if square > num:
right = mid - 1
else:
left = mid + 1
return False
The algorithm uses a binary search approach to find the square root of the given number. It starts with left at 2 and right at half of the input number. It calculates the middle value and squares it. If the squared middle value is equal to the input number, the function returns True; the number is a perfect square. If the squared middle value is greater than the input number, it updates the right to mid - 1; otherwise, it updates the left to mid + 1. The function repeats until the left value is greater than the right value. In that case, the function returns False, indicating that the input number is not a perfect square.
bool isPerfectSquare(int num) {
if (num < 2) return true;
long long left = 2, right = num / 2, mid, square;
while (left <= right) {
mid = left + (right - left) / 2;
square = mid * mid;
if (square == num) return true;
if (square > num) right = mid - 1;
else left = mid + 1;
}
return false;
}
The algorithm uses a binary search approach to find the square root of the given number. It starts with left at 2 and right at half of the input number. It calculates the middle value and squares it. If the squared middle value is equal to the input number, the function returns True; the number is a perfect square. If the squared middle value is greater than the input number, it updates the right to mid - 1; otherwise, it updates the left to mid + 1. The function repeats until the left value is greater than the right value. In that case, the function returns False, indicating that the input number is not a perfect square.