@@ -5,7 +5,7 @@ module stdlib_math
5
5
6
6
implicit none
7
7
private
8
- public :: clip, linspace, logspace
8
+ public :: clip, gcd, linspace, logspace
9
9
public :: EULERS_NUMBER_SP, EULERS_NUMBER_DP, EULERS_NUMBER_QP
10
10
public :: DEFAULT_LINSPACE_LENGTH, DEFAULT_LOGSPACE_BASE, DEFAULT_LOGSPACE_LENGTH
11
11
public :: arange
@@ -29,6 +29,17 @@ module stdlib_math
29
29
module procedure clip_qp
30
30
end interface clip
31
31
32
+ ! > Returns the greatest common divisor of two integers
33
+ ! > ([Specification](../page/specs/stdlib_math.html#gcd))
34
+ ! >
35
+ ! > Version: experimental
36
+ interface gcd
37
+ module procedure gcd_int8
38
+ module procedure gcd_int16
39
+ module procedure gcd_int32
40
+ module procedure gcd_int64
41
+ end interface gcd
42
+
32
43
interface linspace
33
44
! ! Version: Experimental
34
45
! !
@@ -648,4 +659,77 @@ elemental function clip_qp(x, xmin, xmax) result(res)
648
659
res = max (min (x, xmax), xmin)
649
660
end function clip_qp
650
661
662
+
663
+ ! > Returns the greatest common divisor of two integers of kind int8
664
+ ! > using the Euclidean algorithm.
665
+ elemental function gcd_int8 (a , b ) result(res)
666
+ integer (int8), intent (in ) :: a
667
+ integer (int8), intent (in ) :: b
668
+ integer (int8) :: res
669
+
670
+ integer (int8) :: rem, tmp
671
+
672
+ rem = min (abs (a), abs (b))
673
+ res = max (abs (a), abs (b))
674
+ do while (rem /= 0_int8 )
675
+ tmp = rem
676
+ rem = mod (res, rem)
677
+ res = tmp
678
+ end do
679
+ end function gcd_int8
680
+
681
+ ! > Returns the greatest common divisor of two integers of kind int16
682
+ ! > using the Euclidean algorithm.
683
+ elemental function gcd_int16 (a , b ) result(res)
684
+ integer (int16), intent (in ) :: a
685
+ integer (int16), intent (in ) :: b
686
+ integer (int16) :: res
687
+
688
+ integer (int16) :: rem, tmp
689
+
690
+ rem = min (abs (a), abs (b))
691
+ res = max (abs (a), abs (b))
692
+ do while (rem /= 0_int16 )
693
+ tmp = rem
694
+ rem = mod (res, rem)
695
+ res = tmp
696
+ end do
697
+ end function gcd_int16
698
+
699
+ ! > Returns the greatest common divisor of two integers of kind int32
700
+ ! > using the Euclidean algorithm.
701
+ elemental function gcd_int32 (a , b ) result(res)
702
+ integer (int32), intent (in ) :: a
703
+ integer (int32), intent (in ) :: b
704
+ integer (int32) :: res
705
+
706
+ integer (int32) :: rem, tmp
707
+
708
+ rem = min (abs (a), abs (b))
709
+ res = max (abs (a), abs (b))
710
+ do while (rem /= 0_int32 )
711
+ tmp = rem
712
+ rem = mod (res, rem)
713
+ res = tmp
714
+ end do
715
+ end function gcd_int32
716
+
717
+ ! > Returns the greatest common divisor of two integers of kind int64
718
+ ! > using the Euclidean algorithm.
719
+ elemental function gcd_int64 (a , b ) result(res)
720
+ integer (int64), intent (in ) :: a
721
+ integer (int64), intent (in ) :: b
722
+ integer (int64) :: res
723
+
724
+ integer (int64) :: rem, tmp
725
+
726
+ rem = min (abs (a), abs (b))
727
+ res = max (abs (a), abs (b))
728
+ do while (rem /= 0_int64 )
729
+ tmp = rem
730
+ rem = mod (res, rem)
731
+ res = tmp
732
+ end do
733
+ end function gcd_int64
734
+
651
735
end module stdlib_math
0 commit comments