Project Euler #1: Multiples of 3 and 5
Project Euler #1: Multiples of 3 and 5
+ 0 comments why I got error (/tmp/submission/20230321/07/35/hackerrank-9a64492898cab0ea015fbc82490e172f/code/Solution.cs(51,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/submission/20230321/07/35/hackerrank-9a64492898cab0ea015fbc82490e172f/code/Solution.csproj])
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution {
static void Main(String[] args) {
// read the number of test cases from the user input int t = Convert.ToInt32(Console.ReadLine());
// initialize two variables to store the sum of multiples of 3 and 5 respectively int sum0 = 0; // for multiples of 3 or 5 less than t int sum1 = 0; // for each test case
// loop through all the natural numbers less than t for (int a0 = 0; a0 < t; a0++) { // check if the current number i is a multiple of 3 or 5 if (a0 % 3 == 0 || a0 % 5 == 0) { // if so, add it to sum0 sum0 += a0; } } // read the value of n for the current test case from the user input int n = Convert.ToInt32(Console.ReadLine()); // loop through all the natural numbers less than n for (int a1 = 1; a1 < n; a1++) { // check if the current number j is a multiple of 3 or 5 if (a1 % 3 == 0 || a1 % 5 == 0) { // if so, add it to sum1 sum1 += a1; } } // output the final values of sum0 and sum1 Console.WriteLine(sum0); Console.WriteLine(sum1); // wait for the user to press a key before closing the console window Console.ReadLine(); } }
}
+ 0 comments for python,
import sys t = int(input().strip()) for a0 in range(t): sum = 0 n = int(input().strip()) x, y, z = (n-1)//3, (n-1)//5, (n-1)//15 sum = 3*x*(x+1) + 5*y*(y+1) - 15*z*(z+1) print(sum//2)
+ 0 comments Could someone guide me on why my Julia code fails test cases #2 and #3?
function sumToNDigits(x) return x*(x+1)/2 end t = parse(UInt128, strip(readline(stdin))) for t_itr = 1:t n = parse(UInt128, strip(readline(stdin))) d3 = div(n-1, 3) d5 = div(n-1, 5) d15 = div(n-1, 15) sum3 = 3*sumToNDigits(d3) sum5 = 5*sumToNDigits(d5) sum15 = 15*sumToNDigits(d15) sum = sum3 + sum5 - sum15 println(sum) # println(UInt128(sum)) end exit()
+ 0 comments long a=(n-1)/3; long b=(n-1)/15; long c=((n-1)/5); System.out.println(3*(((a+1)*a)/2) +5*(((c+1)*c)/2)-15*(((b+1)*b)/2));
+ 0 comments Hi,
below is my code, which is failing even for t = 100.
I calculated manually for t = 100, but my answer(my answer 2633 vs system answer) seems correct.
can someone tell me whats wrong?
//Return sum of multiples of 3 and 5 below limitNum(n) int sumOfMultiples(int& limitNum) { int sum = 0; int num = 3;
for(int i =0; i<2; i++) { int tempLimitNum = limitNum -1; int reminder = 1; while(reminder != 0) { reminder = tempLimitNum % num; if(reminder != 0) { tempLimitNum--; } } sum =sum + (((tempLimitNum - num)/num)+1)*(0.5)*(num+num*(((tempLimitNum - num)/num)+1)); num = 5; } return sum;
}
Sort 1142 Discussions, By:
Please Login in order to post a comment