Sum of factorials of prime numbers in linked list

Given a linked list which is contains integer values. And our goal is to detect all prime nodes of factorial in this linked list. For example.

```Example 1

Linked List :  1  5  4
Prime node  : {5}
Factorial   : {5*4*3*2*1}
Result      : 120

Example 2

Linked List :  1  4  11 3
Prime node  : {11,3}
Factorial   : {(11*10*9*8*7*6*5*4*3*2*1)+(3*2*1)}
{(39916800) + (6)}
Result      : 39916806

Example 3

Linked List :   5  6  3  9  1  11
Prime node  : {5,3,11}
Factorial   : {...} //factorial of 3,5,11
Result      : {39916926}
```

Assuming that calculate result are under the max number. Here given code implementation process.

``````// C Program
// Sum of factorials of prime numbers in linked list
#include <stdio.h>
//for malloc function
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
};
//Create a node of linked list
struct Node *create_node(int data)
{
//Create dynamic node
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
//Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
void insert(struct Node **head, int data)
{
struct Node *node = create_node(data);
{
}
else
{
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
}
}
{
{
return;
}
while (temp != NULL)
{
printf("  %d", temp->data);
temp = temp->next;
}
}
//Check that whether given number is prime or not
int is_prime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
return 1;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return 0;
}
int i = 11;
while ((i *i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return 0;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return 0;
}
i = i + 6;
}
return 1;
}
int factorials(int num)
{
int i = 1;
int result = 1;
while (i <= num)
{
result *= i;
i++;
}
return result;
}
//Calculate the sum of  all prime factorial in given linked list
{
{
return;
}
//Define resultant variable
int result = 0;
//Get first node of linked list
while (temp != NULL)
{
if (is_prime(temp->data))
{
//Add the factorial of prime number
result += factorials(temp->data);
}
//Visit to next node
temp = temp->next;
}
//Display of calculated result
printf("\n Sum Of Prime Factorials : %d\n", result);
}
int main()
{
return 0;
}``````

Output

`````` Linked List :   5  6  3  9  1  11
Sum Of Prime Factorials : 39916926``````
``````// Java Program
// Sum of factorials of prime numbers in linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
System.out.print("  " + temp.data);
temp = temp.next;
}
}
//Check that whether given number is prime or not
public boolean is_prime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
int i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
public int factorials(int num)
{
int i = 1;
int result = 1;
while (i <= num)
{
result *= i;
i++;
}
return result;
}
//Calculate the sum of  all prime factorial in given linked list
public void prime_factorial_sum()
{
{
return;
}
//Define resultant variable
int result = 0;
//Get first node of linked list
while (temp != null)
{
if (is_prime(temp.data) == true)
{
//Add the factorial of prime number
result += factorials(temp.data);
}
//Visit to next node
temp = temp.next;
}
//Display of calculated result
System.out.print("\n Sum Of Prime Factorials : " + result + "\n");
}
public static void main(String[] args)
{
obj.insert(5);
obj.insert(6);
obj.insert(3);
obj.insert(9);
obj.insert(1);
obj.insert(11);
obj.display();

obj.prime_factorial_sum();
}
}``````

Output

`````` Linked List :   5  6  3  9  1  11
Sum Of Prime Factorials : 39916926``````
``````//Include header file
#include <iostream>
using namespace std;

// C++ Program
// Sum of factorials of prime numbers in linked list

class Node
{
public: int data;
Node * next;
Node(int data)
{
//Set node value
this->data = data;
this->next = NULL;
}
};
{
Node * tail;
//Class constructors
{
this->tail = NULL;
}
//insert node at last of linke list
void insert(int data)
{
//Create a node
Node * node = new Node(data);
{
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
void display()
{
{
return;
}
cout << "\n Linked List : ";
while (temp != NULL)
{
cout << "  " << temp->data;
temp = temp->next;
}
}
//Check that whether given number is prime or not
bool is_prime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
int i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
int factorials(int num)
{
int i = 1;
int result = 1;
while (i <= num)
{
result *= i;
i++;
}
return result;
}
//Calculate the sum of  all prime factorial in given linked list
void prime_factorial_sum()
{
{
cout << "\n Empty Linked List \n";
return;
}
//Define resultant variable
int result = 0;
//Get first node of linked list
while (temp != NULL)
{
if (this->is_prime(temp->data) == true)
{
//Add the factorial of prime number
result += this->factorials(temp->data);
}
//Visit to next node
temp = temp->next;
}
//Display of calculated result
cout << "\n Sum Of Prime Factorials : " << result << "\n";
}
};
int main()
{
obj.insert(5);
obj.insert(6);
obj.insert(3);
obj.insert(9);
obj.insert(1);
obj.insert(11);
obj.display();
obj.prime_factorial_sum();
return 0;
}``````

Output

`````` Linked List :   5  6  3  9  1  11
Sum Of Prime Factorials : 39916926``````
``````//Include namespace system
using System;

// C# Program
// Sum of factorials of prime numbers in linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
Console.Write("  " + temp.data);
temp = temp.next;
}
}
//Check that whether given number is prime or not
public Boolean is_prime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
int i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
public int factorials(int num)
{
int i = 1;
int result = 1;
while (i <= num)
{
result *= i;
i++;
}
return result;
}
//Calculate the sum of  all prime factorial in given linked list
public void prime_factorial_sum()
{
{
return;
}
//Define resultant variable
int result = 0;
//Get first node of linked list
while (temp != null)
{
if (is_prime(temp.data) == true)
{
//Add the factorial of prime number
result += factorials(temp.data);
}
//Visit to next node
temp = temp.next;
}
//Display of calculated result
Console.Write("\n Sum Of Prime Factorials : " + result + "\n");
}
public static void Main(String[] args)
{
obj.insert(5);
obj.insert(6);
obj.insert(3);
obj.insert(9);
obj.insert(1);
obj.insert(11);
obj.display();
obj.prime_factorial_sum();
}
}``````

Output

`````` Linked List :   5  6  3  9  1  11
Sum Of Prime Factorials : 39916926``````
``````<?php
// Php Program
// Sum of factorials of prime numbers in linked list

class Node
{
public \$data;
public \$next;

function __construct(\$data)
{
//Set node value
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructors
function __construct()
{
\$this->tail = null;
}
//insert node at last of linke list
public	function insert(\$data)
{
//Create a node
\$node = new Node(\$data);
{
\$this->tail = \$node;
}
else
{
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
public	function display()
{
{
return;
}
echo "\n Linked List : ";
while (\$temp != null)
{
echo "  ". \$temp->data;
\$temp = \$temp->next;
}
}
//Check that whether given number is prime or not
public	function is_prime(\$num)
{
if (\$num == 2 || \$num == 3 || \$num == 5)
{
return true;
}
if (\$num <= 1 || (\$num % 2 == 0) || (\$num % 3 == 0) || (\$num % 5 == 0))
{
return false;
}
\$i = 11;
while ((\$i * \$i) <= \$num)
{
if (\$num % \$i == 0)
{
//When number is divisible of current i value
return false;
}
else if (\$num % (\$i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
\$i = \$i + 6;
}
return true;
}
public	function factorials(\$num)
{
\$i = 1;
\$result = 1;
while (\$i <= \$num)
{
\$result *= \$i;
\$i++;
}
return \$result;
}
//Calculate the sum of  all prime factorial in given linked list
public	function prime_factorial_sum()
{
{
echo "\n Empty Linked List \n";
return;
}
//Define resultant variable
\$result = 0;
//Get first node of linked list
while (\$temp != null)
{
if (\$this->is_prime(\$temp->data) == true)
{
//Add the factorial of prime number
\$result += \$this->factorials(\$temp->data);
}
//Visit to next node
\$temp = \$temp->next;
}
//Display of calculated result
echo "\n Sum Of Prime Factorials : ". \$result ."\n";
}
}

function main()
{
\$obj->insert(5);
\$obj->insert(6);
\$obj->insert(3);
\$obj->insert(9);
\$obj->insert(1);
\$obj->insert(11);
\$obj->display();
\$obj->prime_factorial_sum();
}
main();``````

Output

`````` Linked List :   5  6  3  9  1  11
Sum Of Prime Factorials : 39916926``````
``````// Node Js Program
// Sum of factorials of prime numbers in linked list

class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
//Class constructors
constructor()
{
this.tail = null;
}
//insert node at last of linke list
insert(data)
{
//Create a node
var node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
display()
{
{
return;
}
while (temp != null)
{
process.stdout.write("  " + temp.data);
temp = temp.next;
}
}
//Check that whether given number is prime or not
is_prime(num)
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
var i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
factorials(num)
{
var i = 1;
var result = 1;
while (i <= num)
{
result *= i;
i++;
}
return result;
}
//Calculate the sum of  all prime factorial in given linked list
prime_factorial_sum()
{
{
return;
}
//Define resultant variable
var result = 0;
//Get first node of linked list
while (temp != null)
{
if (this.is_prime(temp.data) == true)
{
//Add the factorial of prime number
result += this.factorials(temp.data);
}
//Visit to next node
temp = temp.next;
}
//Display of calculated result
process.stdout.write("\n Sum Of Prime Factorials : " + result + "\n");
}
}

function main()
{
obj.insert(5);
obj.insert(6);
obj.insert(3);
obj.insert(9);
obj.insert(1);
obj.insert(11);
obj.display();
obj.prime_factorial_sum();
}
main();``````

Output

`````` Linked List :   5  6  3  9  1  11
Sum Of Prime Factorials : 39916926``````
``````#  Python 3 Program
#  Sum of factorials of prime numbers in linked list

class Node :

def __init__(self, data) :
# Set node value
self.data = data
self.next = None

# Class constructors
def __init__(self) :
self.tail = None

# insert node at last of linke list
def insert(self, data) :
# Create a node
node = Node(data)
self.tail = node
else :
self.tail.next = node
self.tail = node

def display(self) :
print("\nEmpty linked list\n", end = "")
return

print("\n Linked List : ", end = "")
while (temp != None) :
print("  ", temp.data, end = "")
temp = temp.next

# Check that whether given number is prime or not
def is_prime(self, num) :
if (num == 2 or num == 3 or num == 5) :
return True

if (num <= 1 or(num % 2 == 0) or(num % 3 == 0) or(num % 5 == 0)) :
return False

i = 11
while ((i * i) <= num) :
if (num % i == 0) :
# When number is divisible of current i value
return False

elif(num % (i + 2) == 0) :
# When number is divisible of current i + 2 value
return False

i = i + 6

return True

def factorials(self, num) :
i = 1
result = 1
while (i <= num) :
result *= i
i += 1

return result

# Calculate the sum of  all prime factorial in given linked list
def prime_factorial_sum(self) :
print("\n Empty Linked List \n", end = "")
return

# Define resultant variable
result = 0
# Get first node of linked list
while (temp != None) :
if (self.is_prime(temp.data) == True) :
# Add the factorial of prime number
result += self.factorials(temp.data)

# Visit to next node
temp = temp.next

# Display of calculated result
print("\n Sum Of Prime Factorials : ", result ,"\n", end = "")

def main() :
obj.insert(5)
obj.insert(6)
obj.insert(3)
obj.insert(9)
obj.insert(1)
obj.insert(11)
obj.display()
obj.prime_factorial_sum()

if __name__ == "__main__": main()``````

Output

`````` Linked List :    5   6   3   9   1   11
Sum Of Prime Factorials :  39916926``````
``````#  Ruby Program
#  Sum of factorials of prime numbers in linked list

class Node

# Define the accessor and reader of class Node
attr_accessor :data, :next

def initialize(data)

# Set node value
self.data = data
self.next = nil
end
end

# Class constructors
def initialize()

self.tail = nil
end
# insert node at last of linke list
def insert(data)

# Create a node
node = Node.new(data)

self.tail = node
else

self.tail.next = node
self.tail = node
end
end
def display()

return
end
while (temp != nil)

print("  ", temp.data)
temp = temp.next
end
end
# Check that whether given number is prime or not
def is_prime(num)

if (num == 2 || num == 3 || num == 5)

return true
end
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))

return false
end
i = 11
while ((i * i) <= num)

if (num % i == 0)

# When number is divisible of current i value
return false
elsif(num % (i + 2) == 0)

# When number is divisible of current i + 2 value
return false
end
i = i + 6
end
return true
end
def factorials(num)

i = 1
result = 1
while (i <= num)

result *= i
i += 1
end
return result
end
# Calculate the sum of  all prime factorial in given linked list
def prime_factorial_sum()

return
end
# Define resultant variable
result = 0
# Get first node of linked list
while (temp != nil)

if (self.is_prime(temp.data) == true)

# Add the factorial of prime number
result += self.factorials(temp.data)
end
# Visit to next node
temp = temp.next
end
# Display of calculated result
print("\n Sum Of Prime Factorials : ", result ,"\n")
end
end
def main()

obj.insert(5)
obj.insert(6)
obj.insert(3)
obj.insert(9)
obj.insert(1)
obj.insert(11)
obj.display()
obj.prime_factorial_sum()
end
main()``````

Output

`````` Linked List :   5  6  3  9  1  11
Sum Of Prime Factorials : 39916926
``````
``````// Scala Program
// Sum of factorials of prime numbers in linked list

class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
var tail: Node)
{
//Class constructors
def this()
{
this(null, null);
}
//insert node at last of linke list
def insert(data: Int): Unit = {
//Create a node
var node: Node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
print("  " + temp.data);
temp = temp.next;
}
}
//Check that whether given number is prime or not
def is_prime(num: Int): Boolean = {
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
var i: Int = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
def factorials(num: Int): Int = {
var i: Int = 1;
var result: Int = 1;
while (i <= num)
{
result *= i;
i += 1;
}
return result;
}
//Calculate the sum of  all prime factorial in given linked list
def prime_factorial_sum(): Unit = {
{
return;
}
//Define resultant variable
var result: Int = 0;
//Get first node of linked list
while (temp != null)
{
if (is_prime(temp.data) == true)
{
//Add the factorial of prime number
result += factorials(temp.data);
}
//Visit to next node
temp = temp.next;
}
//Display of calculated result
print("\n Sum Of Prime Factorials : " + result + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
obj.insert(5);
obj.insert(6);
obj.insert(3);
obj.insert(9);
obj.insert(1);
obj.insert(11);
obj.display();
obj.prime_factorial_sum();
}
}``````

Output

`````` Linked List :   5  6  3  9  1  11
Sum Of Prime Factorials : 39916926``````
``````// Swift Program
// Sum of factorials of prime numbers in linked list

class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//Set node value
self.data = data;
self.next = nil;
}
}
{
var tail: Node? ;
//Class constructors
init()
{
self.tail = nil;
}
//insert node at last of linke list
func insert(_ data: Int)
{
//Create a node
let node: Node? = Node(data);
{
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
func display()
{
{
return;
}
print("\n Linked List : ", terminator: "");
while (temp != nil)
{
print("  ", temp!.data, terminator: "");
temp = temp!.next;
}
}
//Check that whether given number is prime or not
func is_prime(_ num: Int) -> Bool
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
var i: Int = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
func factorials(_ num: Int) -> Int
{
var i: Int = 1;
var result: Int = 1;
while (i <= num)
{
result *= i;
i += 1;
}
return result;
}
//Calculate the sum of  all prime factorial in given linked list
func prime_factorial_sum()
{
{
print("\n Empty Linked List \n", terminator: "");
return;
}
//Define resultant variable
var result: Int = 0;
//Get first node of linked list
while (temp != nil)
{
if (self.is_prime(temp!.data) == true)
{
//Add the factorial of prime number
result += self.factorials(temp!.data);
}
//Visit to next node
temp = temp!.next;
}
//Display of calculated result
print("\n Sum Of Prime Factorials : ", result ,"\n", terminator: "");
}
}
func main()
{
obj.insert(5);
obj.insert(6);
obj.insert(3);
obj.insert(9);
obj.insert(1);
obj.insert(11);
obj.display();
obj.prime_factorial_sum();
}
main();``````

Output

`````` Linked List :    5   6   3   9   1   11
Sum Of Prime Factorials :  39916926``````

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.