Last Digit!


Submit solution

Points: 10
Time limit: 1.0s
Memory limit: 256M

Author:
Problem types

For any given number \(n\) output the last digit of \(n!\) (n factorial).

Input Specification

The first line of the input contains an integer \(t\) denoting the number of test cases. \(t\) lines follows each contains an integer (\(0 \le n \le 10^9\)).

Output Specification

For each test case output the last digit of \(n\) factorial.

Sample Input

2
1
10

Sample Output

1
0

Comments


  • 0
    SAKIF  commented on Oct. 18, 2020, 4:17 p.m.

    Je crois que j'ai une bonne réponse il ne faut même pas calculer le factoriel mais il faut juste faire une étude des cas n<4 et le cas n>=5 et donc sur mon IDE Eclipse ca marche mais quand je fait la soumission j'ai un message d'erreur WA. Qu'est ce que vous en pensez?

    I think I have a good answer you don't even have to calculate the factorial but you just have to do a study of the cases n <4 and the case n> = 5 and therefore on my Eclipse IDE it works but when I make the submission I have a WA error message. What do you think?


    • 0
      XanaX  commented on Oct. 18, 2020, 5:04 p.m.

      Oui bien sûr, mais vous avez trois cas à étudier:n>=5 et n==4 et sinon(<4)etc... et just imprimer le dernier degist..


      • 1
        SAKIF  commented on Oct. 18, 2020, 5:37 p.m.

        oui j'ai fait tous les cas et ca marche bien sur mon IDE mais le système n'a pas accepte mais quand j'ai change java par C il a accepte.


        • 0
          moncef  commented on Oct. 18, 2020, 5:43 p.m. edited

          You should not abort a submission unless you're sure it is wrong !


  • 2
    bougayez  commented on May 28, 2020, 12:16 p.m.

    Hi, This is really confusing for me. It is clear that if n <= 4 then the output will be factorial of n and if n > 4 then the output will be 0. This is the code https://ideone.com/NP1P5h but it give WA.


    • 1
      pikala  commented on May 28, 2020, 1:19 p.m.

      for n<=4 print just last digit not whole factorial


      • 0
        bougayez  commented on May 28, 2020, 4:13 p.m.

        Thanks bro. That is really weird that I did not pay attention to it.


  • 0
    aymanrs  commented on May 27, 2020, 5:25 p.m. edited

    t


  • 0
    AYBouchana  commented on May 25, 2020, 3:57 p.m.

    Je ne sais pas pourquoi j'obtiens une fausse réponse, le problème ne nécessite même pas de calculer n factorial, si n>=5 alors le chiffre des unités est automatiquement 0 car il y a un produit de 2 et 5 c'est à dire 10. Si n==0 alors 0, si n==1 alors 1, si n==2 alors 2, si n==3 alors 3 et si n==4 alors 4. Je ne sais pas pourquoi j'obtiens une fausse réponse.


    • 0
      aymanrs  commented on May 25, 2020, 4:35 p.m.

      si n==3 alors 6 et si n==0 alors 1


      • 0
        Yassineosip  commented on May 26, 2020, 4:05 a.m. edited

        bro i suffer i solved this problem but the result is TLE and output stack at 0 0 0 0 0 0... can u help please ??


        • 0
          aymanrs  commented on May 26, 2020, 6:11 p.m.

          you may have an infinite loop or a typo somewhere.


          • 0
            Yassineosip  commented on May 27, 2020, 4:39 p.m.

            no i dont think so here is the error "Case #2: IR (RecursionError)"

            the submited code :

            case = int(input())

            def facto(n): if n < 0: exit() if n == 0 or n == 1: return 1 else: return n * facto(n-1)

            for i in range(case): n = int(input()) print(str(facto(n))[-1])


            • 0
              aymanrs  commented on May 27, 2020, 5:26 p.m.

              the code is not clear, can you share it with ideone.com ?


              • 0
                Yassineosip  commented on May 27, 2020, 6:11 p.m.

                as i saw ur using c++ here is the code in c++ https://ideone.com/mrWjQa


              • 0
                Yassineosip  commented on May 27, 2020, 6:07 p.m.

                here we go bro https://ideone.com/iQuh6V


                • 4
                  pikala  commented on May 27, 2020, 6:26 p.m.

                  you should not really compute that factorial of n , because n can be up to 10^9 and the factorial will not be supported in long long type, and that solution is slow anyways. we just want the last digit. just use the fact that when n>=5 then 5 and 2 will be included in the factorial and thus there will be always 0 as a last digit(because 5*2=10), for smaller n you can just check every single case.


                  • 0
                    code06  commented on Aug. 2, 2020, 2:53 p.m.

                    Thanks, this really helped!


                  • 0
                    Yassineosip  commented on May 27, 2020, 7:15 p.m.

                    thank you mate very helpful


    • 0
      akwa_blue  commented on May 25, 2020, 4:18 p.m.

      Il y a deux cas qui sont faux dans ce que tu dis, fais une fonction qui calcule le factoriel pour toi au lieu de hard coder à la main (pour les petits n). C'est plus simple.