Soluție HackerRank pentru Little Gaurav and Sequence, subdomeniul Algebra, în Java 8. Include cerința formatată, exemple, explicația pașilor și cod sursă.

  • Problemă: Little Gaurav and Sequence
  • Domeniu: Algebra
  • Limbaj: Java 8

Challenge: Little Gaurav and Sequence

Subdomeniu: Algebra (algebra)

Scor cont: 20.0 / 20

Submission status: Accepted

Submission score: 1.0

Submission ID: 464721314

Limbaj: java8

Link challenge: https://www.hackerrank.com/challenges/little-gaurav-and-sequence/problem

Cerință

Little Gaurav is very fond of numbers and sequences. One day his teacher tells him to find a strange sequence.

S = ∑_i=0, 2^i≤ n^∞ ∑_j=0^n 2^{2^i+2j}

Since this sequence looks a bit difficult, the teacher tells him to find the last digit of S.

Little Gaurav is confused because he cannot solve the problem and leaves this problem to the worthy programmers of the world. Help little Gaurav in finding the solution.

Input Format
The first line contains T, the number of test cases.
T lines follow, each line containing an integer, N.

Output Format
For each testcase, print the last digit of S in one line.

Constraints
1 ≤ T ≤ 1000
1 ≤ N ≤ 10^15

Sample Input

3
1
2
3

Sample Output

0
6
0

Explanation
For n=1, only i=0 is valid. So S is 2^{2^0+0} + 2^{2^0+2} = 10. Hence last digit of S is 0.
For n=2, only i=0 and 1 are valid. So S is
S1(for i=0) is 2^{2^0+0} + 2^{2^0+2} + 2^{2^0+4}= 42.
S2(for i=1) is 2^{2^1+0} + 2^{2^1+2} + 2^{2^1+4}= 84.
So last digit of S is 6.

Cod sursă

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int t=in.nextInt();
        for(int a0=0;a0<t;a0++){
            long n=in.nextLong();
            double x=Math.log(n)/Math.log(2);
            int y=(int)x+1;
            int s=0;
            if(y==1)
                s=2;
            else
                s=(y-1)*6;
            int a=s%10;
            int b=5;
            if(n%2==0)
                b=1;
            int c=(a*b)%10;
            System.out.println(c);
        }
        
    }
}
HackerRank Algebra – Little Gaurav and Sequence