r/badcode • • Jun 23 '21

python This is a part of the random task solution in codewars that I came across today

Post image
814 Upvotes

37 comments sorted by

18

u/just_a_dude2727 Jun 23 '21

Here is the task and the solution themselves

16

u/Kidiri90 Jun 23 '21 edited Jun 23 '21

I found that descritption very lacking. At first, I thought it was all primes up to (and including) N, but instead it's the product of the first N primes. (Also, what's the expected out put of P(1)?)

EDIT: me being dumdum

9

u/GrilledChee Jun 23 '21

Yeah. Description is ambiguous— I thought the same as you. Thank god for the THREE examples to clarify.

2

u/Mad_madman99 Jun 23 '21

My teacher ripped this for one our python assignments. Ngl I made an array like that in a separate program just to make sure my output was correct lol.

46

u/JustAnotherGamer421 shameless Jun 23 '21

How much longer does it go for? There's no closing bracket...

25

u/just_a_dude2727 Jun 23 '21

Yeah, I left a link in another comment. You can take a look

22

u/dvof Jun 23 '21

he's still typing

5

u/druepy Jun 24 '21

It's another proof of infinite prime numbers.

289

u/[deleted] Jun 23 '21

This is obviously for performance, as the original coder deemed the calculations to be too resource intensive

/s

121

u/throwit7896454 Jun 23 '21

New performance metric: Setting up the code is O(m), where m is the total number of primes to be added to the code

72

u/[deleted] Jun 23 '21

Just create a program which calculates all of the prime numbers, then concatenate them into an array format. Declare this new array as a constant in your production code. You now have O(1) for your production method!

28

u/throwit7896454 Jun 23 '21

Well, you somehow have to get the array into your second program, so probably via JSON import or what not. BAM, O(m) appears again!

Or you use MPI for that sweet inter-process communication and BAM, you now have 2482 other problems!

14

u/Achrus Jun 23 '21

Save each prime number into an individual file? Or in one plaintext file and run seek though varying prime lengths may become an issue…

3

u/razor330 Jun 24 '21

Ask the backend dev to create an api….all we do is make the call….your program is O(1) who cares what the backend is doing…just get me the response in less than 500ms and we’re good. #notmyproblem

8

u/[deleted] Jun 23 '21

in C++ you can calculate the fibonacci numbers at compile time

171

u/[deleted] Jun 23 '21

web browsing history: "is 2069 a prime number?"

112

u/lachlanhunt Jun 23 '21

This is not necessarily a bad solution. Calculating primes can be resource intensive. Predefining a constant for all required primes may be suitable for a given application.

For comparison, how often do you see programs calculating Pi every time that need to use it? They usually don’t. It’s a constant that’s already been calculated to sufficient precision for most applications.

The only significant difference is this is an array of constants, rather than just a single value.

33

u/undergroundmonorail Jun 23 '21

for something on the scale of a codewars kata i would assume that the primes needed would be either too small for this to be worth the effort or too big for this to be practical

there are certainly applications for lookup tables of primes though

6

u/NynaevetialMeara Jun 24 '21

The best solution for a problem like this would be to have a file like this :

https://raw.githubusercontent.com/koorukuroo/Prime-Number-List/master/primes.json

To work with it.

Then you could manipulate it many ways. Probably the easiest and most efficient way would be to load it up in sqlite3 and work with it.

Granted this is a tiny exercise so shortcuts are understandable and necessary.

6

u/geekboy730 Jun 24 '21

Why would I take something I know at compile times (the first 100 or so primes) and offload it to:

  1. Plaintext.

  2. A parseable file that requires runtime performance.

  3. A file format that may change in the future and break my code (or at the very least introduce a dependency).

To me, this seems like the worst possible option. Unless maybe JSON can be parsed at compile time? Maybe you can correct me.

2

u/Impossible-Round-115 Jul 10 '21

You can read a Json at compile time, it takes a bit of work to do but i’m sure there is now a library for it as it was a talk at cpp con about 4 years ago

-1

u/NynaevetialMeara Jun 24 '21

If you only need a small amount, yes. But you can't always know it at compile time.

A file like this provides you a machine readable list to work with.

The easiest way to have fast code would be to load it all in a sqlite db, and have it do the indexing for you.

But you may also load it in a single array, convert it to a B-tree manually, or read sequentially from the file loading the next prime as needed.

Though if you are just going to load it into arrays may as well put it in the code

Also, you shouldn't download the file from the internet at runtime but bundle it with the executable.

1

u/theScrapBook Jul 03 '21

If you need it repeatedly but loading it in is costly, you can also cache it.

16

u/AlwaysNinjaBusiness Jun 23 '21

If it weren't for the fact that it's python, I'd say it's because they want high performance.

0

u/[deleted] Jun 23 '21

[deleted]

8

u/LetterBoxSnatch Jun 23 '21

Yes, but still not nearly as well as most alternatives. This is fun:

https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/python.html

2

u/arzen221 Jun 23 '21

Lol fasta file processing with python

6

u/deftware Jun 23 '21

Table of primes, no biggie. It takes less time to collect a list of prime numbers from a website that lists them, copy-pasta, and format for code than it does to write out a prime number generator.

Though I would imagine a language like Python already has provisions for calculating a list of primes or something.

5

u/sdkrystian Jun 23 '21

I mean, he's on the right track. The result can't be represented by a 64 bit integer after N = 15, and even if you return say, a double as your result, you're only looking at around ~168 elements in a lookup table that stores the actual result.

2

u/[deleted] Jun 24 '21

Amazing They got O(1)

1

u/[deleted] Jun 24 '21

This is the definition of trading time for memory except not really.

1

u/Farpafraf Jun 24 '21

well if the task is only passing tests this does the job

1

u/v_maria Jun 24 '21

based and optimized-pilled

1

u/Ender2K89 wicked Jun 27 '21

oh my

0

u/Sejiko Jun 23 '21

Can you search my solution up it was a 8kyu even or odd challenge in CPP.

Let's say I had fun with some inline assembly :)