- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n in C++
In this problem, we are given a value n. Our task is to find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n.
Let's take an example to understand the problem,
Input : n= 5 Output : 0
Explanation −
(51 + 52 + 53 + 54) mod 5 = (5 + 25 + 125 + 625) mod 5 = (780) mode 5 = 0
Solution Approach
A simple solution to the problem is by directly finding the value of the equation for the given value of N and then calculating its modulus with 5.
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; int findMod5Val(int n){ int val = (n + (n*n) + (n*n*n) + (n*n*n*n)); return val%5; } int main(){ int n = 12; cout<<"For N = "<<n<<", the value of (n^1 + n^2 + n^3 + n^4)\%5 is "<<findMod5Val(n); return 0; }
Output
For N = 12, the value of (n^1 + n^2 + n^3 + n^4)%5 is 0
Another solution to the problem is by using mathematical formulation and generalisation of the function.
$\mathrm{f(n)\:=\:(n\:+\:n^2\:+\:n^3\:+\:n^4)}$
$\mathrm{f(n)\:=\:n^*(1\:+\:n\:+\:n^2\:+\:n^3)}$
$\mathrm{f(n)\:=\:n^*(1^*(1+n)+n^{2*}(1+n))}$
$\mathrm{f(n)\:=\:n^*((1+n^2)^*(1+n))}$
$\mathrm{f(n)\:=\:n^*(n+1)^*(n^2+1)}$
For this equation we can derive that the value of f(n) % 5 can be either 0 or 4 based on the value of n.
if(n%5 == 1), f(n)%5 = 4 Else, f(n)%5 = 0
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; int findMod5Val(int n){ if(n % 4 == 1) return 4; return 0; } int main(){ int n = 65; cout<<"For N = "<<n<<", the value of (n^1 + n^2 + n^3 + n^4)\%5 is "<<findMod5Val(n); return 0; }
Output
For N = 65, the value of (n^1 + n^2 + n^3 + n^4)%5 is 4
- Related Articles
- Find (1^n + 2^n + 3^n + 4^n) mod 5 in C++
- If $frac{n}{4}-5=frac{n}{6}+frac{1}{2}$, find the value of $n$.
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- Evaluate: $frac{a^{2 n+1} times a^{(2 n+1)(2 n-1)}}{a^{n(4 n-1)}times(a^{2})^{2 n+3}}$.
- Find the sum:( 4-frac{1}{n}+4-frac{2}{n}+4-frac{3}{n}+ldots ) upto ( n ) terms
- Solve: $frac{n-1}{2}-frac{n-2}{3}=frac{n-4}{7}$.
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!
- C++ program to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- Find the value of ( n ) when ( 5^{2 n} times 5^{3}=5^{9} ).
- If $1+2+3+........+n=78$, then find the value of $n$.
- Simplify the following:( frac{5 times 25^{n+1}-25 times 5^{2 n}}{5 times 5^{2 n+3}-(25)^{n+1}} )
- Simplify the following:( frac{5^{n+3}-6 times 5^{n+1}}{9 times 5^{n}-2^{2} times 5^{n}} )
- Prove that:( frac{2^{n}+2^{n-1}}{2^{n+1}-2^{n}}=frac{3}{2} )
- Python Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
