Recursively Finding Primes in js: A Slogging Thread

Written by jare | Published 2021/01/27
Tech Story Tags: slogging | javascript | programming | recursion | node-vs-js | go-vs-js | finding-primes-regex | finding-primes-js | web-monetization

TLDRvia the TL;DR App

This Slack discussion by , Oliver N and Arvin Kushwaha occurred in slogging's official #programming-challenges channel, and has been edited for readability.
Jan 27, 2021, 1:42 AM
In the least amount of lines in your preferred language, build a recursive function that calculates the prime numbers. Bonus points mostly to number lines and also to a system where recursion doesn't fault.
Jan 27, 2021, 1:44 AM
Dane Lyons
Oliver NJan 27, 2021, 1:52 AM
my one line js 😄

const f = (n, x) => x === undefined ? f(n, n - 1) : x > 1 ? n % x !== 0 && f(n, x - 1) : true;
Oliver NJan 27, 2021, 1:53 AM
console.log(f(2))
console.log(f(3))
console.log(f(4))
console.log(f(5))
console.log(f(6))
console.log(f(10))
console.log(f(13))
Jan 27, 2021, 1:54 AM
Hilariously man I was gonna say no js trickery
Jan 27, 2021, 1:54 AM
Thot that was anti my ethos hah
Oliver NJan 27, 2021, 1:54 AM
haha, why 😄
Jan 27, 2021, 1:54 AM
That was quick! Are you looking for work and what other languages you work in
Oliver NJan 27, 2021, 1:55 AM
I'm working mostly in Go and JS
Jan 27, 2021, 1:55 AM
I love node. Never figured out go.
Jan 27, 2021, 1:55 AM
Go is compiled tho ya? Fast?
Oliver NJan 27, 2021, 1:56 AM
yes, compiled, and fast
Jan 27, 2021, 1:56 AM
Yeah we looked at that or rust to get our cycles down. Both confuse me
Oliver NJan 27, 2021, 1:56 AM
ppl even use Go to compile JS to make it fast
https://github.com/evanw/esbuild
Jan 27, 2021, 1:59 AM
Didn't answer q tho do you have much free time? My company is ostensibly giving me my offer in the next 1-2 weeks after we finalize the fund in caymans. I offered to cut my salary if we could give it to what we've been affectionately calling a 'real dev'. I'm great at POC and shipping but am entirely self taught, untested, procedural and.. well.
Jan 27, 2021, 2:00 AM
Https://coindexlabs.com
Jan 27, 2021, 2:00 AM
Yes, we know the color scheme sucks
Jan 27, 2021, 2:01 AM
I mixed up ostensibly and allegedly. Ha!
Oliver NJan 27, 2021, 2:01 AM
how about part-time?
Jan 27, 2021, 2:02 AM
Would probably do the trick yeah. You just did in a few minutes what would have taken me hours. Does that one liner fault ever?
Jan 27, 2021, 2:02 AM
And do you have much experience with trading systems? Crypto exchange apis?
Oliver NJan 27, 2021, 2:03 AM
yeah, maybe, I haven't test it with any input other than number, also maybe it will fault with JS infinity
Jan 27, 2021, 2:03 AM
We have some other candidates but yeah do send your resume maybe or linkedin here or pm? Forward to ceo
Jan 27, 2021, 2:03 AM
Oh yeah no no worries on bad input, I meant as increasing
Jan 27, 2021, 2:11 AM
Utsav Jaiswal David Smooke looks like your new product may have done what months of searching including your new partners on jobs.hackernoon didn't ;)
Arvin KushwahaJan 27, 2021, 2:35 AM
When you say recursive function that calculates the prime numbers, do you mean returns a sequence of prime numbers or checks if the number is prime?
Jan 27, 2021, 2:29 PM
Very good point, my original idea was indeed to return the sequence of primes. When the original answer was posted I chaulked up the disconnect to miscommunication and a failure in directions :)
Arvin KushwahaJan 27, 2021, 3:41 PM
Well, in that case, here's my answer in Python

g = lambda x: (print(x) if not any([x%i == 0 for i in (range(2, x) if x < 10 else range(2, int(x**0.5)))]) and x >= 2 else False) or g(x+1)

g(0) # Or any number you wish to start fromOf course, it does have the limitation of recursive depth...
Jan 27, 2021, 4:04 PM
Is also the reason I made a bonus point about faulting. Any idea when the recursive depth would fail? Does it vary?
Jan 27, 2021, 7:31 PM
Arvin if you don't mind and you'd wanted to be considered can you send linkedin github or whatever via pm?

Written by jare | https://linktr.ee/STACCart
Published by HackerNoon on 2021/01/27