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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node
= (Node*) malloc (
sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int sumOfFactors( int n)
{
int sum = 0;
for ( int f = 1; f <= n / 2; f++) {
if (n % f == 0) {
sum += f;
}
}
return sum;
}
int getSum(Node* head_1)
{
int sum = 0;
Node* ptr = head_1;
while (ptr != NULL) {
if (ptr->data > 0
&& ptr->data
== sumOfFactors(ptr->data)) {
sum += ptr->data;
}
ptr = ptr->next;
}
return sum;
}
int main()
{
Node* head1 = NULL;
push(&head1, 17);
push(&head1, 6);
push(&head1, 10);
push(&head1, 6);
push(&head1, 4);
int k = getSum(head1);
cout << k;
return 0;
}
|
Java
class GFG{
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node= new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int sumOfFactors( int n)
{
int sum = 0 ;
for ( int f = 1 ; f <= n / 2 ; f++)
{
if (n % f == 0 )
{
sum += f;
}
}
return sum;
}
static int getSum(Node head_1)
{
int sum = 0 ;
Node ptr = head_1;
while (ptr != null )
{
if (ptr.data > 0 && ptr.data ==
sumOfFactors(ptr.data))
{
sum += ptr.data;
}
ptr = ptr.next;
}
return sum;
}
public static void main(String[] args)
{
Node head = new Node();
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);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def getSum( self ):
Sum = 0
ptr = self .head
while ( True ):
if (ptr.data > 0 and ptr.data = =
sumOfFactors(ptr.data)):
Sum + = ptr.data
if (ptr. next = = None ):
break
ptr = ptr. next
return Sum
def sumOfFactors(n):
Sum = 0
for f in range ( 1 , (n / / 2 ) + 1 ):
if (n % f = = 0 ):
Sum + = f
return Sum
if __name__ = = '__main__' :
head1 = LinkedList()
head1.push( 17 )
head1.push( 6 )
head1.push( 10 )
head1.push( 6 )
head1.push( 4 )
k = head1.getSum()
print (k)
|
C#
using System;
class GFG{
class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node= new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int sumOfFactors( int n)
{
int sum = 0;
for ( int f = 1; f <= n / 2; f++)
{
if (n % f == 0)
{
sum += f;
}
}
return sum;
}
static int getSum(Node head_1)
{
int sum = 0;
Node ptr = head_1;
while (ptr != null )
{
if (ptr.data > 0 && ptr.data ==
sumOfFactors(ptr.data))
{
sum += ptr.data;
}
ptr = ptr.next;
}
return sum;
}
public static void Main(String[] args)
{
Node head = new Node();
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);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function sumOfFactors(n) {
var sum = 0;
for ( var f = 1; f <= parseInt(n / 2); f++) {
if (n % f == 0) {
sum += f;
}
}
return sum;
}
function getSum(head_1) {
var sum = 0;
var ptr = head_1;
while (ptr != null ) {
if (ptr.data > 0 && ptr.data == sumOfFactors(ptr.data)) {
sum += ptr.data;
}
ptr = ptr.next;
}
return sum;
}
var head = new Node();
head = push(head, 17);
head = push(head, 6);
head = push(head, 10);
head = push(head, 6);
head = push(head, 4);
var k = getSum(head);
document.write(k);
</script>
|
Last Updated :
03 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...