codeparrot/xlcost-text-to-code huggingface dataset:
Generation of n numbers with given set of factors | C # program to generate n numbers with given factors ; Generate n numbers with factors in factor [ ] ; array of k to store next multiples of given factors ; Prints n numbers int output = 0 ; Next number to print as output ; Find the next smallest multiple ; Printing minimum in each iteration print the value if output is not equal to current value ( to avoid the duplicates ) ; incrementing the current value by the respective factor ; Driver code
using System ; class GFG { static void generateNumbers ( int [ ] factor , int n , int k ) { int [ ] next = new int [ k ] ; for ( int i = 0 ; i < n ; ) { int toincrement = 0 ; for ( int j = 0 ; j < k ; j ++ ) if ( next [ j ] < next [ toincrement ] ) toincrement = j ; if ( output != next [ toincrement ] ) { output = next [ toincrement ] ; Console . Write ( next [ toincrement ] + " ▁ " ) ; i ++ ; } next [ toincrement ] += factor [ toincrement ] ; } } static public void Main ( ) { int [ ] factor = { 3 , 5 , 7 } ; int n = 10 ; int k = factor . Length ; generateNumbers ( factor , n , k ) ; } }
Reversible numbers | C # program to check if a given number is reversible or not ; Function to check reversible number ; Calculate reverse of n ; Calculate sum of number and its reverse ; Check for reverse number reach digit must be odd ; Driver function
using System ; class GFG { static void checkReversible ( int n ) { int rev = 0 , rem = 0 ; int flag = n ; while ( flag > 0 ) { rem = flag % 10 ; rev *= 10 ; rev += rem ; flag /= 10 ; } int sum = rev + n ; while ( sum > 0 && ( rem % 2 != 0 ) ) { rem = sum % 10 ; sum /= 10 ; } if ( sum == 0 ) Console . WriteLine ( " Reversible ▁ Number " ) ; else Console . WriteLine ( " Non - Reversible ▁ Number " ) ; } public static void Main ( ) { int n = 36 ; checkReversible ( n ) ; } }
Reversible numbers | C # program to find the count of reversible numbers upto a given number n ; Function to calculate the count of reversible number ; Calculate counts of reversible number of 1 to n - digits ; All four possible cases and their formula ; for i of form 2 k ; for i of form 4 k + 3 ; for i of form 4 k + 1 no solution ; Driver function ; count up - to 9 digit numbers ( 1 billion )
using System ; class GFG { static void countReversible ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { switch ( i % 4 ) { case 0 : case 2 : count += 20 * ( int ) Math . Pow ( 30 , ( i / 2 - 1 ) ) ; break ; case 3 : count += 100 * ( int ) Math . Pow ( 500 , i / 4 ) ; break ; case 1 : break ; } } Console . WriteLine ( count ) ; } public static void Main ( ) { int n = 9 ; countReversible ( n ) ; } }
Multiplicative order | C # program to implement multiplicative order ; function for GCD ; Function return smallest + ve integer that holds condition A ^ k ( mod N ) = 1 ; result store power of A that rised to the power N - 1 ; modular arithmetic ; return smallest + ve integer ; increment power ; Driver Code
using System ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static int multiplicativeOrder ( int A , int N ) { if ( GCD ( A , N ) != 1 ) return - 1 ; int result = 1 ; int K = 1 ; while ( K < N ) { result = ( result * A ) % N ; if ( result == 1 ) return K ; K ++ ; } return - 1 ; } public static void Main ( ) { int A = 4 , N = 7 ; Console . Write ( multiplicativeOrder ( A , N ) ) ; } }
Sum of product of x and y such that floor ( n / x ) = y | C # program to find sum of product of x and y such that n / x = y ( Integer Division ) ; Return the sum of natural number in a range . ; n * ( n + 1 ) / 2. ; Return the sum of product x * y . ; Iterating i from 1 to sqrt ( n ) ; Finding the upper limit . ; Finding the lower limit . ; Driver Code
using System ; class GFG { static int sumOfRange ( int a , int b ) { int i = ( a * ( a + 1 ) ) >> 1 ; int j = ( b * ( b + 1 ) ) >> 1 ; return ( i - j ) ; } static int sumofproduct ( int n ) { int sum = 0 ; int root = ( int ) Math . Sqrt ( n ) ; for ( int i = 1 ; i <= root ; i ++ ) { int up = n / i ; int low = Math . Max ( n / ( i + 1 ) , root ) ; sum += ( i * sumOfRange ( up , low ) ) ; sum += ( i * ( n / i ) ) ; } return sum ; } static public void Main ( String [ ] args ) { int n = 10 ; Console . WriteLine ( sumofproduct ( n ) ) ; } }
Primitive root of a prime number n modulo n | C # program to find primitive root of a given number n ; Returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Iterative Function to calculate ( x ^ n ) % p in O ( logy ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Utility function to store prime factors of a number ; Print the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to find smallest primitive root of n ; Check if n is prime or not ; Find value of Euler Totient function of n Since n is a prime number , the value of Euler Totient function is n - 1 as there are n - 1 relatively prime numbers . ; Find prime factors of phi and store in a set ; Check for every number from 2 to phi ; Iterate through all prime factors of phi . and check if we found a power with value 1 ; Check if r ^ ( ( phi ) / primefactors ) mod n is 1 or not ; If there was no power with value 1. ; If no primitive root found ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) { return false ; } if ( n <= 3 ) { return true ; } if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static int power ( int x , int y , int p ) { while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % p ; } x = ( x * x ) % p ; } return res ; } static void findPrimefactors ( HashSet < int > s , int n ) { while ( n % 2 == 0 ) { s . Add ( 2 ) ; n = n / 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { s . Add ( i ) ; n = n / i ; } } if ( n > 2 ) { s . Add ( n ) ; } } static int findPrimitive ( int n ) { HashSet < int > s = new HashSet < int > ( ) ; if ( isPrime ( n ) == false ) { return - 1 ; } int phi = n - 1 ; findPrimefactors ( s , phi ) ; for ( int r = 2 ; r <= phi ; r ++ ) { bool flag = false ; foreach ( int a in s ) { if ( power ( r , phi / ( a ) , n ) == 1 ) { flag = true ; break ; } } if ( flag == false ) { return r ; } } return - 1 ; } public static void Main ( String [ ] args ) { int n = 761 ; Console . WriteLine ( " ▁ Smallest ▁ primitive ▁ root ▁ of ▁ " + n + " ▁ is ▁ " + findPrimitive ( n ) ) ; } }
Minimum number of power terms with sum equal to n | C # program to calculate minimum numberof powers of x to make sum equal to n . ; Return minimum power terms of x required ; if x is 1 , return n since any power of 1 is 1 only . ; Consider n = a * x + b where a = n / x and b = n % x . ; Update count of powers for 1 's  added ; Repeat the process for reduced n ; Driver code
using System ; class GFG { static int minPower ( int n , int x ) { if ( x == 1 ) return n ; int ans = 0 ; while ( n > 0 ) { ans += ( n % x ) ; n /= x ; } return ans ; } public static void Main ( ) { int n = 5 , x = 3 ; Console . WriteLine ( minPower ( n , x ) ) ; } }
Sum of Perrin Numbers | C # program to calculate sum of Perrin Numbers ; function for sum of first n Perrin number . ; if ( n == 0 ) n = 0 ; if ( n == 1 ) n = 1 ; if ( n == 2 ) n = 2 ; calculate k = 5 sum of three previous step . ; Sum remaining numbers ; calculate next term ; Driver code
using System ; class GFG { static int calSum ( int n ) { int a = 3 , b = 0 , c = 2 ; return 3 ; return 3 ; return 5 ; int sum = 5 ; while ( n > 2 ) { int d = a + b ; sum += d ; a = b ; b = c ; c = d ; n -- ; } return sum ; } public static void Main ( ) { int n = 9 ; Console . WriteLine ( calSum ( n ) ) ; } }
Print the kth common factor of two numbers | C # program to find kth common factor of two numbers ; Returns k 'th common factor of x and y. ; Find smaller of two numbers ; Count common factors until we either reach small or count becomes k . ; If we reached small ; Driver code
using System ; class GFG { static int findKHCF ( int x , int y , int k ) { int small = Math . Min ( x , y ) ; int count = 1 ; for ( int i = 2 ; i <= small ; i ++ ) { if ( x % i == 0 && y % i == 0 ) count ++ ; if ( count == k ) return i ; } return - 1 ; } public static void Main ( ) { int x = 4 , y = 24 , k = 3 ; Console . Write ( findKHCF ( x , y , k ) ) ; } }
Find minimum number to be divided to make a number a perfect square | C # program to find minimum number which divide n to make it a perfect square . ; Return the minimum number to be divided to make n a perfect square . ; Since 2 is only even prime , compute its power separately . ; If count is odd , it must be removed by dividing n by prime number . ; If count is odd , it must be removed by dividing n by prime number . ; Driver code
using System ; class GFG { static int findMinNumber ( int n ) { int count = 0 , ans = 1 ; while ( n % 2 == 0 ) { count ++ ; n /= 2 ; } if ( count % 2 == 1 ) ans *= 2 ; for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n /= i ; } if ( count % 2 == 1 ) ans *= i ; } if ( n > 2 ) ans *= n ; return ans ; } public static void Main ( ) { int n = 72 ; Console . WriteLine ( findMinNumber ( n ) ) ; } }
Program to implement Collatz Conjecture | C # program to implement Collatz Conjecture ; Function to find if n reaches to 1 or not . ; If there is a cycle formed , we can 't reach 1. ; If n is odd then pass n = 3 n + 1 else n = n / 2 ; Wrapper over isToOneRec ( ) ; To store numbers visited using recursive calls . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static Boolean isToOneRec ( int n , HashSet < int > s ) { if ( n == 1 ) { return true ; } if ( s . Contains ( n ) ) { return false ; } return ( n % 2 == 1 ) ? isToOneRec ( 3 * n + 1 , s ) : isToOneRec ( n / 2 , s ) ; } static Boolean isToOne ( int n ) { HashSet < int > s = new HashSet < int > ( ) ; return isToOneRec ( n , s ) ; } public static void Main ( String [ ] args ) { int n = 5 ; if ( isToOne ( n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Program to implement Collatz Conjecture | C # program to implement Collatz Conjecture ; Function to find if n reaches to 1 or not . ; Return true if n is positive ; Drivers code
using System ; class GFG { static bool isToOne ( int n ) { return ( n > 0 ) ; } public static void Main ( ) { int n = 5 ; if ( isToOne ( n ) == true ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
GCD of two numbers formed by n repeating x and y times | C # program to print Greatest Common Divisor of number formed by N repeating x times and y times ; Return the Greatest common Divisor of two numbers . ; Prints Greatest Common Divisor of number formed by n repeating x times and y times . ; Finding GCD of x and y . ; Print n , g times . ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void findgcd ( int n , int x , int y ) { int g = gcd ( x , y ) ; for ( int i = 0 ; i < g ; i ++ ) Console . Write ( n ) ; } public static void Main ( ) { int n = 123 , x = 5 , y = 2 ; findgcd ( n , x , y ) ; } }
Count natural numbers whose factorials are divisible by x but not y | C # program to count natural numbers whose factorials are divisible by x but not y . ; GCD function to compute the greatest divisor among a and b ; Returns first number whose factorial is divisible by x . ; int i = 1 ; Result ; Remove common factors ; We found first i . ; Count of natural numbers whose factorials are divisible by x but not y . ; Return difference between first natural number whose factorial is divisible by y and first natural number whose factorial is divisible by x . ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( ( a % b ) == 0 ) return b ; return gcd ( b , a % b ) ; } static int firstFactorialDivisibleNumber ( int x ) { int new_x = x ; for ( i = 1 ; i < x ; i ++ ) { new_x /= gcd ( i , new_x ) ; if ( new_x == 1 ) break ; } return i ; } static int countFactorialXNotY ( int x , int y ) { return ( firstFactorialDivisibleNumber ( y ) - firstFactorialDivisibleNumber ( x ) ) ; } public static void Main ( ) { int x = 15 , y = 25 ; Console . Write ( countFactorialXNotY ( x , y ) ) ; } }
Find the first natural number whose factorial is divisible by x | A simple C # program to find first natural number whose factorial divides x ; Returns first number whose factorial divides x . ; int i = 1 ; Result ; Driver code
using System ; class GFG { static int firstFactorialDivisibleNumber ( int x ) { int fact = 1 ; for ( i = 1 ; i < x ; i ++ ) { fact = fact * i ; if ( fact % x == 0 ) break ; } return i ; } public static void Main ( ) { int x = 16 ; Console . Write ( firstFactorialDivisibleNumber ( x ) ) ; } }
Find two prime numbers with given sum | C # program to find a prime number pair whose sum is equal to given number C # program to print super primes less than or equal to n . ; Generate all prime numbers less than n . ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true bool isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Prints a prime pair with given sum ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driver code
using System ; class GFG { static bool SieveOfEratosthenes ( int n , bool [ ] isPrime ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) isPrime [ i ] = false ; } } return false ; } static void findPrimePair ( int n ) { bool [ ] isPrime = new bool [ n + 1 ] ; SieveOfEratosthenes ( n , isPrime ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime [ i ] && isPrime [ n - i ] ) { Console . Write ( i + " ▁ " + ( n - i ) ) ; return ; } } } public static void Main ( ) { int n = 74 ; findPrimePair ( n ) ; } }
Count numbers with same first and last digits | C # program to get count of numbers with same start and end digit in an interval ; Utility method to get first digit of x ; method to return count of numbers with same starting and ending digit from 1 upto x ; get ten - spans from 1 to x ; add 9 to consider all 1 digit numbers ; Find first and last digits ; If last digit is greater than first digit then decrease count by 1 ; Method to return count of numbers with same starting and ending digit between start and end ; driver code
using System ; class GFG { public static int getFirstDigit ( int x ) { while ( x >= 10 ) x /= 10 ; return x ; } public static int getCountWithSameStartAndEndFrom1 ( int x ) { if ( x < 10 ) return x ; int tens = x / 10 ; int res = tens + 9 ; int firstDigit = getFirstDigit ( x ) ; int lastDigit = x % 10 ; if ( lastDigit < firstDigit ) res -- ; return res ; } public static int getCountWithSameStartAndEnd ( int start , int end ) { return getCountWithSameStartAndEndFrom1 ( end ) - getCountWithSameStartAndEndFrom1 ( start - 1 ) ; } public static void Main ( ) { int start = 5 , end = 40 ; Console . Write ( getCountWithSameStartAndEnd ( start , end ) ) ; } }
Right | C # code to check right - truncatable prime or not ; Generate all prime numbers less than n . ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true bool isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Returns true if n is right - truncatable , else false ; Generating primes using Sieve ; Checking whether the number remains prime when last ( right ) digit is successively removed ; Driven program
using System ; class GFG { static void sieveOfEratosthenes ( int n , bool [ ] isPrime ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = false ; } } } static bool rightTruPrime ( int n ) { bool [ ] isPrime = new bool [ n + 1 ] ; sieveOfEratosthenes ( n , isPrime ) ; while ( n != 0 ) { if ( isPrime [ n ] ) n = n / 10 ; else return false ; } return true ; } public static void Main ( ) { int n = 59399 ; if ( rightTruPrime ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Mersenne Prime | C # Program to generate mersenne primes ; Generate all prime numbers less than n . ; Initialize all entries of boolean array as true . A value in prime [ i ] will finally be false if i is Not a prime , else true bool prime [ n + 1 ] ; ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to generate mersenne primes lessthan or equal to n ; Create a boolean array " prime [ 0 . . n ] " ; Generating primes using Sieve ; Generate all numbers of the form 2 ^ k - 1 and smaller than or equal to n . ; Checking whether number is prime and is one less then the power of 2 ; Driven program
using System ; class GFG { static void SieveOfEratosthenes ( int n , bool [ ] prime ) { for ( int i = 0 ; i <= n ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } } static void mersennePrimes ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; SieveOfEratosthenes ( n , prime ) ; for ( int k = 2 ; ( ( 1 << k ) - 1 ) <= n ; k ++ ) { long num = ( 1 << k ) - 1 ; if ( prime [ ( int ) ( num ) ] ) Console . Write ( num + " ▁ " ) ; } } public static void Main ( ) { int n = 31 ; Console . WriteLine ( " Mersenne ▁ prime ▁ numbers " + " ▁ smaller ▁ than ▁ or ▁ equal ▁ to ▁ " + n ) ; mersennePrimes ( n ) ; } }
Find sum of modulo K of first N natural number | C # program to find sum of modulo K of first N natural numbers . ; Return sum of modulo K of first N natural numbers . ; Iterate from 1 to N && evaluating and adding i % K . ; Driver program
using System ; class GFG { static int findSum ( int N , int K ) { int ans = 0 ; for ( int i = 1 ; i <= N ; i ++ ) ans += ( i % K ) ; return ans ; } static public void Main ( ) { int N = 10 , K = 2 ; Console . WriteLine ( findSum ( N , K ) ) ; } }
Find sum of modulo K of first N natural number | C # program to find sum of modulo K of first N natural numbers . ; Return sum of modulo K of first N natural numbers . ; Counting the number of times 1 , 2 , . . , K - 1 , 0 sequence occurs . ; Finding the number of elements left which are incomplete of sequence Leads to Case 1 type . ; adding multiplication of number of times 1 , 2 , . . , K - 1 , 0 sequence occurs and sum of first k natural number and sequence from case 1. ; Driver program
using System ; class GFG { static int findSum ( int N , int K ) { int ans = 0 ; int y = N / K ; int x = N % K ; ans = ( K * ( K - 1 ) / 2 ) * y + ( x * ( x + 1 ) ) / 2 ; return ans ; } static public void Main ( ) { int N = 10 , K = 2 ; Console . WriteLine ( findSum ( N , K ) ) ; } }
Smallest number to multiply to convert floating point to natural | C # program to find the smallest number to multiply to convert a floating point number into natural number . ; Finding GCD of two number ; Returns smallest integer k such that k * str becomes natural . str is an input floating point number ; Find size of string representing a floating point number . ; Below is used to find denominator in fraction form . ; Used to find value of count_after_dot ; To find numerator in fraction form of given number . For example , for 30.25 , numerator would be 3025. ; If there was no dot , then number is already a natural . ; Find denominator in fraction form . For example , for 30.25 , denominator is 100 ; Result is denominator divided by GCD - of - numerator - and - denominator . For example , for 30.25 , result is 100 / GCD ( 3025 , 100 ) = 100 / 25 = 4 ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int findnum ( String str ) { int n = str . Length ; int count_after_dot = 0 ; bool dot_seen = false ; int num = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] != ' . ' ) { num = num * 10 + ( str [ i ] - '0' ) ; if ( dot_seen == true ) count_after_dot ++ ; } else dot_seen = true ; } if ( dot_seen == false ) return 1 ; int dem = ( int ) Math . Pow ( 10 , count_after_dot ) ; return ( dem / gcd ( num , dem ) ) ; } public static void Main ( ) { String str = "5.125" ; Console . Write ( findnum ( str ) ) ; } }
Find the maximum number of handshakes | C # program to find maximum number of handshakes . ; Calculating the maximum number of handshake using derived formula . ; Driver code
using System ; class GFG { static int maxHandshake ( int n ) { return ( n * ( n - 1 ) ) / 2 ; } public static void Main ( ) { int n = 10 ; Console . Write ( maxHandshake ( n ) ) ; } }
Count digits in given number N which divide N | C # program to find number of digits in N that divide N . ; Utility function to check divisibility by digit ; ( N [ i ] - '0' ) gives the digit value and form the number ; Function to count digits which appears in N and divide N divide [ 10 ] -- > array which tells that particular digit divides N or not count [ 10 ] -- > counts frequency of digits which divide N ; We initialize all digits of N as not divisible by N ; 1 divides all numbers ; start checking divisibility of N by digits 2 to 9 ; if digit divides N then mark it as true ; Now traverse the number string to find and increment result whenever a digit divides N . ; Driver Code
using System ; class GFG { static bool divisible ( string N , int digit ) { int ans = 0 ; for ( int i = 0 ; i < N . Length ; i ++ ) { ans = ( ans * 10 + ( N [ i ] - '0' ) ) ; ans %= digit ; } return ( ans == 0 ) ; } static int allDigits ( string N ) { bool [ ] divide = new bool [ 10 ] ; for ( int i = 0 ; i < divide . Length ; i ++ ) { divide [ i ] = false ; } divide [ 1 ] = true ; for ( int digit = 2 ; digit <= 9 ; digit ++ ) { if ( divisible ( N , digit ) ) divide [ digit ] = true ; } int result = 0 ; for ( int i = 0 ; i < N . Length ; i ++ ) { if ( divide [ N [ i ] - '0' ] == true ) result ++ ; } return result ; } public static void Main ( ) { string N = "122324" ; Console . Write ( allDigits ( N ) ) ; } }
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 ) ; } }