Given an integer array nums, return the greatest common divisor of the smallest number and largest number in nums.
The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.
Example 1:
Input: nums = [2,5,6,9,10] Output: 2 Explanation: The smallest number in nums is 2. The largest number in nums is 10. The greatest common divisor of 2 and 10 is 2.
Example 2:
Input: nums = [7,5,6,8,3] Output: 1 Explanation: The smallest number in nums is 3. The largest number in nums is 8. The greatest common divisor of 3 and 8 is 1.
Example 3:
Input: nums = [3,3] Output: 3 Explanation: The smallest number in nums is 3. The largest number in nums is 3. The greatest common divisor of 3 and 3 is 3.
Constraints:
2 <= nums.length <= 10001 <= nums[i] <= 1000program gcd_of_smallest_and_largest
implicit none
integer :: i, n, nums(1000), smallest, largest, gcd
do while (get_line(n) > 0)
read (*, *) nums(1:n)
smallest = minval(nums(1:n))
largest = maxval(nums(1:n))
gcd = gcd_of_two(smallest, largest)
write (*, '(I0)') gcd
end do
contains
function gcd_of_two(a, b) result(gcd)
implicit none
integer, intent(in) :: a, b
integer :: gcd
if (b == 0) then
gcd = a
else
gcd = gcd_of_two(b, mod(a, b))
end if
end function gcd_of_two
end program gcd_of_smallest_and_largest
temp.f95:28:18:
28 | gcd = gcd_of_two(b, mod(a, b))
| 1
Error: Function ‘gcd_of_two’ at (1) cannot be called recursively, as it is not RECURSIVE
temp.f95:6:14:
6 | do while (get_line(n) > 0)
| 1
Error: Function ‘get_line’ at (1) has no IMPLICIT type
module greatest_common_divisor
implicit none
private
public :: gcd
contains
integer function gcd(nums) result(gcd_val)
implicit none
integer, intent(in) :: nums(:)
integer :: smallest, largest
smallest = minval(nums)
largest = maxval(nums)
gcd_val = gcd_helper(smallest, largest)
end function gcd
integer function gcd_helper(a, b) result(gcd_val)
implicit none
integer, intent(in) :: a, b
integer :: remainder
if (b == 0) then
gcd_val = a
else
remainder = mod(a, b)
gcd_val = gcd_helper(b, remainder)
end if
end function gcd_helper
end module greatest_common_divisor
program test_greatest_common_divisor
use greatest_common_divisor
implicit none
integer, parameter :: nums(4) = [2, 5, 6, 9, 10]
integer :: gcd_val
gcd_val = gcd(nums)
write (*,*) 'The greatest common divisor of the smallest and largest number in nums is:', gcd_val
gcd_val = gcd([7, 5, 6, 8, 3])
write (*,*) 'The greatest common divisor of the smallest and largest number in nums is:', gcd_val
gcd_val = gcd([3, 3])
write (*,*) 'The greatest common divisor of the smallest and largest number in nums is:', gcd_val
end program test_greatest_common_divisor
temp.f95:26:22:
26 | gcd_val = gcd_helper(b, remainder)
| 1
Error: Function ‘gcd_helper’ at (1) cannot be called recursively, as it is not RECURSIVE
temp.f95:32:9:
32 | use greatest_common_divisor
| 1
Fatal Error: Cannot open module file ‘greatest_common_divisor.mod’ for reading at (1): No such file or directory
compilation terminated.
def findGCD(nums):
min_val, max_val = min(nums), max(nums)
while max_val % min_val != 0:
min_val, max_val = max_val % min_val, min_val
return min_val
nums array.min_val and max_val with the values of the minimum and maximum elements, respectively.min_val and max_val using the Euclidean algorithm. Keep performing the modulo operation and swapping them until max_val % min_val is equal to zero.min_val as the GCD of the two numbers.int findGCD(vector<int>& nums) {
auto min_max = minmax_element(nums.begin(), nums.end());
int min_val = *min_max.first;
int max_val = *min_max.second;
while(max_val % min_val != 0) {
int temp = min_val;
min_val = max_val % min_val;
max_val = temp;
}
return min_val;
}
nums array.min_val and max_val with the values of the minimum and maximum elements, respectively.min_val and max_val using the Euclidean algorithm. Keep performing the modulo operation and swapping them until max_val % min_val is equal to zero.min_val as the GCD of the two numbers.