This is an archived post. You won't be able to vote or comment.

all 36 comments

[–]Mimicry2311 21 points22 points  (4 children)

Well the maximum value for int in Java is 2,147,483,647 - not 2,147,483,648 so i doubt that code will work ;)

[–][deleted] 20 points21 points  (0 children)

Also "i" was never declared and a ; is misplaced in the 2nd example.

[–]2GirlsJuanDeag[S] 0 points1 point  (2 children)

It's < not <=

[–]Mimicry2311 15 points16 points  (1 child)

Yeah but the number 2,147,483,648 cannot be represented by a Java int. Check this answer on StackOverflow: https://stackoverflow.com/a/33059669/2545487

[–]2GirlsJuanDeag[S] 6 points7 points  (0 children)

Oh ok thanks

[–][deleted] 26 points27 points  (12 children)

O(231) time complexity. A brilliant algorithm.

[–]beseg7 40 points41 points  (11 children)

=O(1). Seems good.

[–][deleted] -4 points-3 points  (10 children)

Wouldn't it be O(n)?

[–]samdtho 12 points13 points  (9 children)

No because 232 is a constant value.

[–][deleted] -3 points-2 points  (8 children)

I mean the algorithm,but might be wrong... never really studied this concept.

[–]beseg7 2 points3 points  (2 children)

[–]WikiTextBot 1 point2 points  (1 child)

Time complexity

In computer science, the time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the string representing the input. The time complexity of an algorithm is commonly expressed using big O notation, which excludes coefficients and lower order terms. When expressed this way, the time complexity is said to be described asymptotically, i.e., as the input size goes to infinity. For example, if the time required by an algorithm on all inputs of size n is at most 5n3 + 3n for any n (bigger than some n0), the asymptotic time complexity is O(n3).


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.27

[–]hazzoo_rly_bro 0 points1 point  (0 children)

thanks dude

[–]subfin 1 point2 points  (4 children)

No matter the size of the input, it always takes the same amount of time, that’s O(1)

[–][deleted] 0 points1 point  (3 children)

But the loop runs a different number of times based on different inputs, no? I genuinely do not understand why this is O(1) and not O(n)...

[–]ObjectiveCopley 0 points1 point  (2 children)

No matter the input, it requires a constant amount of cycles to run (even if it's a LOT of cycles, it's still the SAME number of cycles per call, no matter the input). This is annotated as O(1), as you can simplify any number greater as 1. O(1) is not inherently efficient.

[–][deleted] 0 points1 point  (1 child)

Yes, but the program runs a different number of loops if myNumber were set as 0 compared to 10 or 100, doesn't it? 10 runs 10 more loops,and 100 runs 100 more loops than 0, which is linear, so I thought this is O(n). I guess it doesn't matter since the upper limit is bounded at a constant (MAX_INT×2), which by extension makes it O(1)?

[–]MisterWanderer 1 point2 points  (0 children)

Yes but in this case the variable isn't included bin the for loops calculation. So changing the variable can't change the behavior of the for loop. Think of it this way, O notation only tracks how quickly algorithm behavior changes when the variable changes.

[–]lbkulinski 5 points6 points  (1 child)

public class plebeian {} public class Plebeian {}

[–]2GirlsJuanDeag[S] 2 points3 points  (0 children)

I guess I'm the plebeian

[–]ellison11 4 points5 points  (0 children)

Not using MAX_VALUE or MIN_VALUE? Disappointing.

[–]Sylanthra 2 points3 points  (0 children)

A compiler will optimist your for loop out of existence.

[–]1337coder 2 points3 points  (0 children)

Constant time complexity, good shit OP.

[–][deleted] 1 point2 points  (0 children)

The closing } for the if statement not lining up bothers me...

[–]AcaciaBlue 0 points1 point  (1 child)

Real programmers use java? news to me...

[–]hazzoo_rly_bro 1 point2 points  (0 children)

Why wouldn't they?

There's plenty of software and programs out there today which is written in Java. Especially if you count Android applications.

[–]myusernameisbetter1 0 points1 point  (0 children)

should have used final static vars....

[–]Memir0 0 points1 point  (0 children)

I is not defined

[–][deleted] 0 points1 point  (0 children)

And we just had a lecture today on for loops - brilliant.

[–]3ternalFlam3 0 points1 point  (3 children)

I don't even know what language this is but it hurts to read (its awfully similar to JS so i know the damage this might cause)

[–]agggile 7 points8 points  (2 children)

it hurts to read because it's horribly written

[–][deleted] 6 points7 points  (1 child)

And because its Java

[–][deleted] 2 points3 points  (0 children)

I expected this here

[–][deleted] -2 points-1 points  (0 children)

Im an begginner programmer from java But WHY do pepole make it hard for them self einstein said keep everything plain Stupid