Filter
Exclude
Time range
-
Near
import java.math.*; public class SumOfNumbers { public static void main(String... args) { for (int pow = 1; pow < 1_000_000_000; pow *= 10) { long time = System.nanoTime(); try { System.out.printf("pow = %,d%n", pow); var sum = getSum(new BigInteger("10").pow(pow)); System.out.println(sum.bitLength()); } finally { time = System.nanoTime() - time; System.out.printf("time = %dms%n", (time / 1_000_000)); } } } private static BigInteger getSum(BigInteger upto) { return upto.multiply(upto.add(BigInteger.ONE)) .shiftRight(1); } } Can be improved with my parallelMultiply() method :-)

I would be more interested in how programmers respond to the challenge. For example, if you do use the algorithm for O(1), how do you avoid overflowing the integer for the same maximum size? And what is that maximum size of n? What about using a long? And what would the maximum size be then? And what about BigInteger? What would the complexity be then? How about calculating the sum to 1 billion? 1 trillion? With BigInteger, we no longer have O(1), but rather O(n^1.46) in Java 8 onwards. Before that it was O(n^2). And yes, this has little to do with programming, but I personally would expect any programmer to at least know that the sum of integers is n * (n 1) / 2. I figured that out by myself when I was 10.
2
12
3,792
25 Jan 2024
Replying to @Redacious_
me tryna find sumofnumbers() anywhere in his answer

ALT Ratatouille Meme GIF

1
10
502
if i add two num in python a = 2, b = 5 print(a b) <-- print sum in two lines of code but in Java public class SumOfNumbers 1 { public static void main(String args[]){ int n1 = 2, n2 = 5, sum; sum = n1 n2; System.out.println(sum); } } <-- this is why Python is love for me πŸ˜‚
4
67
πŸ’‘ Javascript Tip πŸ‘‡ Calculate Sum of numbers using reduce method #javascripttips #sumofnumbers #reduce
2
From what I see in the loop, everything is done right. Except the numbers array, it is correct as well but just that its empty. So the value of "sumofnumbers" will be 0 because the array you want to loop through to provide a sum is empty.
2
Small things can make a huge difference. How do you find sum of numbers from A to B in #Python? Check out our #video on #Youtube Link: youtu.be/ctwdzGCpBMk Don’t forget to #subscribe. #BeingPythonic #YouTube #AlwaysBlue #CodeNewbies #cleanCoding #sumOfNumbers #Python3

Replying to @WellPaidGeek
let arrayOfNumbers = [2, 4, 6, 8, 10, 12]; let sumOfNumbers = 0; for (i = 0; i < arrayOfNumbers.length; i ) { sumOfNumbers = arrayOfNumbers[i]; } const avgNumber = () => { return sumOfNumbers / arrayOfNumbers.length; } console.log(avgNumber());
3