+ 4

What is time limit and why is it not working here?

My code works well on my laptop for high values like 100000 but here it does not work saying time limit reached. You can check on ur device and than here. So what is this time limit. Please check and answer and upvote if you liked my code. https://code.sololearn.com/cxn3Hz1a0cjz/?ref=app

28th Aug 2017, 12:26 AM
Hassan Sajjad
Hassan Sajjad - avatar
20 Answers
+ 8
//This is a little tweak that I have done. I'm not sure if it works. Please try it. #include <iostream> using namespace std; int main() { int y = 3, x; cin >> x; bool isPrime = x % 2; while (y<x/2 && isPrime) { if (x%y == 0) isPrime = false; else y+=2; } cout << boolalpha << x << " is prime : " << isPrime ; return 0; }
28th Aug 2017, 5:08 AM
Hatsy Rei
Hatsy Rei - avatar
28th Aug 2017, 3:32 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
With the thousands of codes in Code Playground about prime numbers, your statement is hardly justified. I'm not saying that your claims are false, but it's not reflecting on my side.
28th Aug 2017, 4:48 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Thanks a lot. I was seriously frustrated because of this. Check my this programme. This is my first programme about which I think that it was tough to write. Now I think I will get some upvotes. At the end I will like to thank again and I have just checked that y=y++ works on visual studio https://code.sololearn.com/c4xkf0b94cS6/?ref=app
28th Aug 2017, 5:17 AM
Hassan Sajjad
Hassan Sajjad - avatar
+ 2
I wish you people would stop asking for upvotes, it makes me wanna downvote.
28th Aug 2017, 5:25 AM
lion
lion - avatar
+ 2
anyway it seems that we have gone beyond the topic lol. lets hope the developers would discover and fix this issue someday what we could do now is applying good practice in coding to avoid unwanted errors
28th Aug 2017, 8:11 AM
Hanz
Hanz - avatar
+ 2
I have posted that in Q & Ans discussion in order to inform developers
28th Aug 2017, 8:12 AM
Hassan Sajjad
Hassan Sajjad - avatar
+ 1
I believe it is apps fault. How can be an infinite loop if it is working good on laptop and is copy pasted.It did not work for vary small values like 3 and 5 w hile on laptop it worked correctly for highest values.
28th Aug 2017, 4:16 AM
Hassan Sajjad
Hassan Sajjad - avatar
+ 1
adding one more thing here u can not write programme for prime numbers. Time limit will exceed. Haha https://www.sololearn.com/discuss/664565/?ref=app
28th Aug 2017, 4:44 AM
Hassan Sajjad
Hassan Sajjad - avatar
+ 1
Mention me those. Please tell if mine one has fault. It did not work for 7 https://code.sololearn.com/cD3MZaCr85VC/?ref=app
28th Aug 2017, 4:56 AM
Hassan Sajjad
Hassan Sajjad - avatar
+ 1
hahaha so true tell me why y=y++ worked on visual studio
28th Aug 2017, 5:27 AM
Hassan Sajjad
Hassan Sajjad - avatar
+ 1
on visual studio x=x++statement adds 1 in variable while here the value of variable remain same. Can anyone explain that. I checked that
28th Aug 2017, 5:59 AM
Hassan Sajjad
Hassan Sajjad - avatar
+ 1
I m talking about x=x++ And I checked it
28th Aug 2017, 6:08 AM
Hassan Sajjad
Hassan Sajjad - avatar
+ 1
agree with lion. since x++ has the same function as x=x+1, there is no need to use x=x++ which should be the same as x=x=x+1.
28th Aug 2017, 7:37 AM
Hanz
Hanz - avatar
+ 1
There is no need . I agree too. But question is if c++ permits you to write in that manner than compiler is not doing good job.
28th Aug 2017, 7:40 AM
Hassan Sajjad
Hassan Sajjad - avatar
0
it should work anywhere, for the same reason x=y++ works. It's equivalent to y=y; // why would you even do that? y++;
28th Aug 2017, 5:36 AM
lion
lion - avatar
0
it can't be, x=x+1 adds 1 to x no matter what. . Ok, you now edited your comment from x=x+1 to x=x++ When I said y=y++ works I meant it is not syntactically wrong (it doesn't give compile error). Its outcome though seems to depend on compiler implementation.
28th Aug 2017, 6:04 AM
lion
lion - avatar
0
you're right, I just checked too :) It seems this newest compiler they use here goes out of its way to punish you for writing things like that :) Joking. Anyway, you shouldn't write something like that even if it would work (if it would work, it would be redundant to do x=x). By the C++ specifications as I learned them years ago I expected it to work like you said it does in Visual studio. Although it does kinda make sense to not work; lets say you have: x=2; y=x++ + x++; // NEVER write something like this! what would you expect y to be: y=2+2, or y=2+3? Well, confusion aside, if instead of y you put x, it seems x will have the value that y would have had after executing that. So yeah, better not write things like that, that may depend on the compiler implementation.
28th Aug 2017, 7:01 AM
lion
lion - avatar
0
I agree, it should give a warning at least that the code is redundant or implementation dependent. @Low Kai Han it's even worse than redundant: its outcome is dependent on the compiler version/implementation. Say you have: x=2; y=x++; Here you expect y to be 2, right? But if instead of y you have x, like in x=x++, it seems after executing that, x will have the value you expected y to have. Which is confusing.
28th Aug 2017, 7:48 AM
lion
lion - avatar
0
Exactly, good practice is recomended. It's not an error on the compiler part though @Hassan. It IS said that post increment x++ makes a copy of the object x to work with before executing everything, that's why, performance wise, it is better to use pre-increment ++x whenever possible to avoid the overhead of making that copy. I thought that's only for complex objects and not basic types like int, but it seems they try to make the behavior consistent for all data types.
28th Aug 2017, 8:25 AM
lion
lion - avatar