vendredi 13 décembre 2019

Difficulty to satisfy test cases of first euler project

I recently got started with the Euler project. This is my first project which is not getting implemented for test cases 2,3,4. Please have look at my code and tell me what test cases have been missed.

The problem can be found on this webpage: https://www.hackerrank.com/contests/projecteuler/challenges/euler001/problem

My code :

import java.io.*;
public class Solution {
public static void main(String[] args)throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in) );
    int testCases = Integer.parseInt(in.readLine());
    int arr[] = new int[testCases];
    if(testCases <= 1 || testCases >= 100000)
    {System.out.print("exit 1");
        System.exit(0);}
    for(int i = 0; i < testCases; i++)
    {

        arr[i] = Integer.parseInt(in.readLine());  
        if(arr[i] <= 1 || i >= 1000000000 )
        {System.out.print("exit 2"); 
            System.exit(0);}        }
    for(int i = 0; i < testCases; i++)
    {
        System.out.println(FactorSum( arr[i] ) );   
    }
}

public static int FactorSum(int input)
{
    int sum = 0;
    for(int i = 0 ; i < input ; i++)
    {
        if(i%5 == 0 || i%3 == 0)
        {
            sum+=i;
        }
    }
    return sum;
}
}

Please help !

Aucun commentaire:

Enregistrer un commentaire