public static long gcd ( List < Long > l ) { if ( l . isEmpty ( ) ) { throw new IllegalArgumentException ( "List must contain at least one element" ) ; } BigInteger gcd = BigInteger . valueOf ( l . get ( 0 ) ) ; for ( Long num : l . subList ( 1 , l . size ( ) ) ) { gcd = gcd . gcd ( BigInteger . valueOf ( num ) ) ; } return gcd . longValue ( ) ; }
Finds the greatest common divisor of all numbers in the list .