Given a Linked list of N integers, the task is to find the sum of factorials of each prime element in the list.
Input: L1 = 4 -> 6 -> 2 -> 12 -> 3
Prime numbers are 2 and 3, hence 2! + 3! = 2 + 6 = 8.
Input: L1 = 7 -> 4 -> 5
Prime numbers are 7 and 5, hence 7! + 5! = 5160.
Approach: To solve the problem mentioned above follow the steps given below:
- Implement a function factorial(n) that finds the factorial of N .
- Initialize a variable sum = 0. Now, traverse the given list and for each node check whether node is prime or not.
- If node is prime then update sum = sum + factorial(node) otherwise else move the node to next.
- Print the calculated sum in the end.
Below is the implementation of the above approach:
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.
- Minimum and Maximum Prime Numbers of a Singly Linked List
- Count of Prime Nodes of a Singly Linked List
- Delete all Non-Prime Nodes from a Singly Linked List
- Sum and Product of all Prime Nodes of a Singly Linked List
- Product of all prime nodes in a Doubly Linked List
- Delete all Prime Nodes from a Doubly Linked List
- Delete all Prime Nodes from a Singly Linked List
- Delete all Prime Nodes from a Circular Singly Linked List
- GCD of factorials of two numbers
- Numbers whose factorials end with n zeros
- Sum of all Palindrome Numbers present in a Linked list
- Sum of all perfect numbers present in an Linked list
- Multiply two numbers represented as linked lists into a third list
- Count natural numbers whose factorials are divisible by x but not y
- Remove all nodes from a Doubly Linked List containing Fibonacci numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Create new linked list from two given linked list with greater element at each node
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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 : amit143katiyar