r/badcode • u/MysteriousK69420 if: else: if: else: if: else: if: else: if: else: if: else: if: • Feb 16 '22
python presenting, whatever the fk this is
418
Upvotes
r/badcode • u/MysteriousK69420 if: else: if: else: if: else: if: else: if: else: if: else: if: • Feb 16 '22
362
u/This_Growth2898 Feb 16 '22
A normal beginner's code. He's struggling with some basic concepts like bools, but nothing criminal here.
60
u/Rex_Lapis101 Feb 16 '22 edited Feb 16 '22
i like how the code starts off as normal code just to decent into madness and becomes a goddamn faliure, so much so that even i can't call myself a faliure in comparison to that code
(before anyone says anything, yes, i wrote that code, i didn't realised what i did there either)
58
u/[deleted] Feb 16 '22
I've done this. And as my friend perfectly put it " its like you were told to reach a word limit "
26
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 16 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
43
u/TotalCheeks Feb 16 '22
Don’t see a problem, obviously is just paid by the line
9
u/sneakykenny Feb 16 '22
Wait aren't you supposed to do [2, root[ to check if a number is prime ?
22
u/JNCressey Feb 16 '22
that 2nd to last line
if isPrime == True:could be replaced by simplyelse:.Sincerely, Python's "for-else"
7
u/NanthaR Feb 16 '22
If it works then it's fine. /s
3
u/Bobebobbob Feb 16 '22
I'm not used to Python, is anything wrong with this besides not going [2,root] and the having two more lines at the end?
1
u/hemophiliac_driver Feb 17 '22
In fact, this is a very optimal way to test if n is prime.
There is certain theorem that states that If n is a composite integer, then n has a prime divisor less than sqrt(n)
So, the script is calculating the maximum prime value to factorize n.
The algorithm could be better, but the idea itself make a lot of sense.
If we want to delve into the math behind this code, we can read the section of this wikipedia article
1
u/TreeTrunkzTV Feb 26 '22 edited Mar 02 '22
I'm a beginner myself and would like some feedback, is this a better way of doing this or is there an even better way?
0
u/lachlanhunt Feb 17 '22
Looks like a naive implementation of a prime sieve approach. It could be a bit more efficient because it only needs to check if it’s divisible by other primes less than the square root. Or at least only check if the number is divisible by 2 or odd numbers starting from 3.
The
range(int(root), mum)for the loop looks wrong. It should berange(3, int(root), 2), with an extra check for even numbers before that.0
u/BALLZCENTIE Feb 17 '22
So is it bad because of the weird flow of control or some supposed inefficiencies? Seems like all the comments are that it can be done more efficiently
-1
u/logicaldementia Feb 16 '22
just cuz number has no remainder its prime now?
-2
u/plastigoop Feb 17 '22
I suffered damage reading that.
-2
u/FrezoreR Feb 17 '22
A really weird and inefficient way to check if a number is a prime 😅