Aliquot Sequence | C # implementation of Optimized approach to generate Aliquot Sequence ; Function to calculate sum of all proper divisors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; else Otherwise take both ; calculate sum of all proper divisors only ; Function to print Aliquot Sequence for an input n . ; Print the first term ; Calculate next term from previous term ; Print next term ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int getSum ( int n ) { for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum - n ; } static void printAliquot ( int n ) { Console . Write ( n + " ▁ " ) ; HashSet < int > s = new HashSet < int > ( ) ; s . Add ( n ) ; while ( n > 0 ) { n = getSum ( n ) ; if ( s . Contains ( n ) ) { Console . Write ( " Repeats with " break ; } Console . Write ( n + " ▁ " ) ; s . Add ( n ) ; } } public static void Main ( String [ ] args ) { printAliquot ( 12 ) ; } }