Sum of all perfect numbers present in an Linked list

Given an Linked list containing N positive integer, the task is to find the sum of all the perfect numbers from the list.

A number is perfect if is equal to the sum of its proper divisors i.e. the sum of its positive divisors excluding the number itself.

Examples:

Input: L1 = 3 -> 6 -> 9
Output:6
Proper divisor sum of 3 = 1
ans=0
Proper divisor sum of 6 = 1 + 2 + 3 = 6
ans=6;
Proper divisor sum of 9 = 1 + 3 = 4
ans=6;

Input: L1 = 17 -> 6 -> 10 -> 6 -> 4
Output: 12



Approach: Initialise sum = 0 and for every node of the list, find the sum of its proper divisors say sumFactors. If cur_node = sumFactors then update the resultant sum as sum = sum + cur_node. Print the sum in the end.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Node of the singly linked list
struct Node {
    int data;
    Node* next;
};
  
// Function to insert a node
// at the beginning of
// the singly Linked List
void push(Node** head_ref, int new_data)
{
    // allocate node
    Node* new_node
        = (Node*)malloc(
            sizeof(struct Node));
  
    // put in the data
    new_node->data = new_data;
  
    // link the old list off the new node
    new_node->next = (*head_ref);
  
    // move the head to point
    // to the new node
    (*head_ref) = new_node;
}
// Function to return the sum of
// all the proper factors of n
int sumOfFactors(int n)
{
    int sum = 0;
    for (int f = 1; f <= n / 2; f++) {
  
        // f is the factor of n
        if (n % f == 0) {
            sum += f;
        }
    }
    return sum;
}
  
// Function to return the required sum
int getSum(Node* head_1)
{
  
    // To store the sum
    int sum = 0;
    Node* ptr = head_1;
    while (ptr != NULL) {
  
        // If current element is non-zero
        // and equal to the sum
        // of proper factors of itself
        if (ptr->data > 0
            && ptr->data
                   == sumOfFactors(ptr->data)) {
            sum += ptr->data;
        }
        ptr = ptr->next;
    }
    return sum;
}
  
// Driver code
int main()
{
    // start with the empty list
    Node* head1 = NULL;
  
    // create the linked list
    push(&head1, 17);
    push(&head1, 6);
    push(&head1, 10);
    push(&head1, 6);
    push(&head1, 4);
    int k = getSum(head1);
    cout << k;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG{
  
// Node of the singly linked list
static class Node 
{
    int data;
    Node next;
};
  
// Function to insert a node
// at the beginning of
// the singly Linked List
static Node push(Node head_ref, 
                 int new_data)
{
      
    // Allocate node
    Node new_node= new Node();
  
    // Put in the data
    new_node.data = new_data;
  
    // Link the old list off the new node
    new_node.next = head_ref;
  
    // Move the head to point
    // to the new node
    head_ref = new_node;
      
    return head_ref;
}
  
// Function to return the sum of
// all the proper factors of n
static int sumOfFactors(int n)
{
    int sum = 0;
      
    for(int f = 1; f <= n / 2; f++)
    {
  
       // f is the factor of n
       if (n % f == 0)
       {
           sum += f;
       }
    }
    return sum;
}
  
// Function to return the required sum
static int getSum(Node head_1)
{
  
    // To store the sum
    int sum = 0;
      
    Node ptr = head_1;
      
    while (ptr != null
    {
  
        // If current element is non-zero
        // and equal to the sum of proper
        //  factors of itself
        if (ptr.data > 0 && ptr.data == 
                sumOfFactors(ptr.data))
        {
            sum += ptr.data;
        }
        ptr = ptr.next;
    }
    return sum;
}
  
// Driver code
public static void main(String[] args)
{
      
    // Start with the empty list
    Node head = new Node();
  
    // Create the linked list
    head = push(head, 17);
    head = push(head, 6);
    head = push(head, 10);
    head = push(head, 6);
    head = push(head, 4);
      
    int k = getSum(head);
      
    System.out.print(k);
}
}
  
// This code is contributed by amal kumar choubey

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
class GFG{
  
// Node of the singly linked list
class Node 
{
    public int data;
    public Node next;
};
  
// Function to insert a node
// at the beginning of
// the singly Linked List
static Node push(Node head_ref, 
                 int new_data)
{
      
    // Allocate node
    Node new_node= new Node();
  
    // Put in the data
    new_node.data = new_data;
  
    // Link the old list off the new node
    new_node.next = head_ref;
  
    // Move the head to point
    // to the new node
    head_ref = new_node;
      
    return head_ref;
}
  
// Function to return the sum of
// all the proper factors of n
static int sumOfFactors(int n)
{
    int sum = 0;
      
    for(int f = 1; f <= n / 2; f++)
    {
  
        // f is the factor of n
        if (n % f == 0)
        {
            sum += f;
        }
    }
    return sum;
}
  
// Function to return the required sum
static int getSum(Node head_1)
{
  
    // To store the sum
    int sum = 0;
      
    Node ptr = head_1;
      
    while (ptr != null
    {
  
        // If current element is non-zero
        // and equal to the sum of proper
        // factors of itself
        if (ptr.data > 0 && ptr.data == 
               sumOfFactors(ptr.data))
        {
            sum += ptr.data;
        }
        ptr = ptr.next;
    }
    return sum;
}
  
// Driver code
public static void Main(String[] args)
{
      
    // Start with the empty list
    Node head = new Node();
  
    // Create the linked list
    head = push(head, 17);
    head = push(head, 6);
    head = push(head, 10);
    head = push(head, 6);
    head = push(head, 4);
      
    int k = getSum(head);
      
    Console.Write(k);
}
}
  
// This code is contributed by amal kumar choubey

chevron_right


Output:

12

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Amal Kumar Choubey