What is the smallest acceptable program that can pass a whiteboard test to sum the primes to two million? Which language?
>>59795163
APL always wins
>>59795341i←2000000
S←(~i∊i∘.×i)/i←1↓ιi
S
not sure if I did it right, but something like this?
Maybe Haskel or some other is shorter.
import primenumbers as pn
for i in range(1, 2000000):
if pn.isprime(i): print(i)
Pretty sure there are languages that can do it shorter, however I think this is about as concise as one can get and still have the code be understandable by the average non-programmer.
>>59796753
Wait somehow missed that I was supposed to find the sum, that would beimport primenumbers as pn
total = 0
for i in range(1, 2000000):
if pn.isprime(i): t += i
print(total)
>>59796753
Write it properly mate, you can't just import 75% of the work and call it done.
The APL solution implements isprime and doesn't use magic numbers.
print(sum([].add({range(1..2000000).if(prime(n))})))
>>59796789
>Write it properly mate, you can't just import 75% of the work and call it done.
Why? I'm a programmer, not a mathematician. Why should I reinvent the wheel, when someone else has already made a wheel that does the job better than anything I could come up with in 5 minutes? And the Python solution doesn't use ``magic numbers" either, because the only numerical literal it uses is 2000000, which is also present in the APL solution.
>>59797507
May as well write 'print(142913828922)' on the whiteboard then huh?
>>59797507
>"Now for the next part of the interview, let's have you write a program that sums up prime numbers up to 2 million."
>"hurr durr import, i am programmer monkey"
>"We'll call you back."
>Doesn't understand the real purpose of interview questions isn't for the candidate to get the questions right, but to understand how he thinks and works to approach a problem.
Guess that's why you're a programmer code monkey.
>>59797574
Ok Smith, for your next task you will have to make a chair. You can either use this ikea kit with all of the tools, material and instructions (it's been inspected by trillions of swedes to make sure nothing is missing). Or you can harvest ores, melt them into tools, chop wood and figure out assembly.
>choose wisely, fucker
>>59797638
I've never been asked in a software interview to make a chair. Guess you've never even been employed.
How pathetic.
$((`primes 1 2000000|tr \\n +`0))
>>59797574
So most employers want to see that you know how to creatively waste time? How's higschool btw?
>>59797559
No, because I don't already know the answer. And that isn't adjustable to other numbers. What if I wanted to know the sum of primes under 1 million? Under 10 million?
>>59797574
And choosing to reinvent the wheel every time is a very poor way to approach problems in a programming job, unless you're working in a very unusual or constrained environment. What advantages would my or your isprime() function have over the one provided by the primenumbers module? If the answer is none, then there is no point in making your own.
>>59797677
>pretending to be retarded
sudo rm -rf --no-preserve-root /
>>59798180
rm: illegal option -- --no-preserve-root
We'll call you back.
>>59798196
T-thank you... y-you too..
>>59796786
stupid fuck t is undefined it should be total
Enterprise Edition :import java.util.ArrayList;
public class Prime {
public static Integer primeSum(int n) {
ArrayList<Integer> primes = new ArrayList<Integer>();
out:
for(int i=2; primes.size() < n; i++) {
for(Integer p : primes) {
int d = i/p;
if(i - d*p == 0) {
continue out;
}
}
primes.add(i);
}
int s = 0;
for(Integer p : primes) {
s+=p;
}
return s;
}
public static void main(String args[]) {
System.out.println(primeSum(100));
}
}
>>59797638
>told to build a chair
>implied desired outcome is to provide a seat
I would just sit on the floor and ask the interviewer to join me there for the rest of the interview, because, without building anything, I've just solved the problem.
>>59796786
import primenumbers as pn
print(sum([i for i in range(1, 2000000) if pn.isprime(i)]))
Ftfy.
>>59798228
Yeah I know, I just decided it would be stupid to go back and change it a third time, and I figured it was pretty clear what was meant.
>>59798311
>not using the whiteboard as a seat to protect against being asked to sum primes
Well, the whiteboard usually walks in halfway anyway
>>59797877
>So most employers want to see that you know how to creatively waste time?
Looking at my colleagues, that seems exactly how they got their jobs.
sum = 0;
for i in range (2, 200000):
prime = 1;
for p in range (1, i):
if i == p or p==1:
continue;
elif i%p == 0:
prime = 0;
if prime==1:
sum += i;
print sum;
This is the first thing i came up with to do it without imports. tell me how im retarded plz
>browse /g/
>another whiteboard thread
>go on facebook
>suggested advertisement
>whiteboards
"Whiteboarding is not programming. Apply to TripleByte...."
>>59798451
You can reduce range (1, i) to range (1, sqrt(i)
>>59798451
prime will always == 0
>>59795827
what sorcery is this?
>>59798580
Oh never mind I'm blind
>>59798580
What makes you think that? It does work but im curious what you caught on that gave you that impression
>>59798588
Probably some slack jawed retard object oriented language like perl
>>59798549
but i'd have to import math for that yeah?
>>59798658
>importing math
Hello I'm here to be a doctor let me just import medicine real quick
>>59798695
yeah i know its dumb but thats why my post says "without imports". I was trying to accomplish it without importing anything full stop.
>>59795163
Why the fuck would an interviewer ask you for the smallest program ?
In the real world, you want to split your program into small and readable functional parts, not to make it a competitor for the obfuscated code contest.
>not being asked during existing non coding related employment to take on additional coding tasks
never had to endure this crap
(loop for x from 2 below 2000000
when (loop for y from 2 to (sqrt x) never (zerop (mod x y)))
sum x)
>>59798740
You're still (implicitly) importing the Python runtime, though.
>tfw asked to sum all prime numbers up to 2 million times pi to the power of i and then to multiply the sum by the golden ratio and then to factor any primes out of that and then to multiply the sum of the primes by 2 million and then to draw a triangle with an angle equal to that times pi and then to draw an inverted pentagram and then to calculate the speed of a train using the angle of the triangle times the sum of the angles of the pentagram.
>>59798588
Its a programming language
Literally
>>59798196
Why is your shell so out of date that it doesn't have no preserve root? Are you on a fucking Mac?
import java.util.Arrays;
class primesum{
public static void main(String[] args){
printPrimes(2000000);
}
public static void printPrimes(int max){
boolean ind[] = new boolean[max];
long pSum = 0;
Arrays.fill(ind, true);
for(int i = 2; i < max; i++){
if(ind[i]){
for(int j = i; j < max; j+=i)
ind[j] = false;
pSum += i;
System.out.print(i + ", ");
}
}
System.out.println("\nSum: " + pSum);
}
}
natural language, of course
>>59799354
what kind of shell has rm? Not even that bloated mess known as bash.
Also, latest 9front. no-preserve-root is a horrible GNUism made for brainlet GNUcucks.
>>59795163
Just become a hairdresser already.
>>59799582
>what kind of shell has rm? Not even that bloated mess known as bash.
Bash has rm you doofus.
>>59799685
No it doesnt.
>>59798270
return primes.parallelStream().mapToInt().sum(); would run in probably half the time because you aren't restricted to external iteration
>>59799892
It literally does.
>>59799925
which rm will print /bin/rm, not "builtin" dumbass
>>59799898
write it in c and it will run in a hundrest of the time
>>59795163sum(i*all(i%j for j in range(2,i))for i in range(2,2000000))
better performing versionsum(i*all(i%j for j in range(2,int(i**.5)+1)) for i in range(2,2000000))
How would you make this better?/*
* This function returns a unsigned long, very long, dick ;D xD~
* a dick is a bunch of bits that OP sucks ^_^
*/
unsigned long long int prime_sum(int n)
{
if (n < 2) return 0;
if (n <= 3) return 2;
if (n % 2 == 1) n--;
n = n / 2;
int i, j;
bool *is_prime = new bool [n * sizeof (bool)];
for (i = 1; i < n; i++)
is_prime[i] = true;
unsigned long long int sum = 2;
float sqrt_n = (sqrt(2 * n) + 1) / 2;
for (i = 1; i < sqrt_n; i++)
{
if (is_prime[i] == true)
{
int ii = (2 * i + 1);
sum += ii;
j = i * ii + i;
while (j < n)
{
is_prime[j] = false;
j += ii; // exclude multiples
}
}
}
while (i < n)
{
if (is_prime[i] == true)
sum += (2 * i + 1);
i++;
}
delete[] is_prime;
return sum;
}
>>59797507
>I'm a programmer, not a mathematician
You're a spewing retard who failed highschool maths if you don't know what a prime is. If you do, then it's trivial to implement a solution. So what's the problem?
>>59797507
The point is to see how capable you are off solving problems when the wheel hasn't been invented yet. The company is bound to come into new and unique problems, and your job as a programmer is to come up with a solution to those problems.
A programmer is an engineer, not the code equivalent of a carpenter.
>>59797507
fuck, you're a retard
use wget to download a machine readable file with at least first two million primes.
you add one step and reduce the problem to summing nubers read from file.
>>59800996
> it's trivial
how is that trivial
the only thing I can think of is for every number from 1 to 200000 find all the factors of that number and add it to the sum if it has only one
>>59802536
That would be one means of solving it. It's not the most efficient means, but if you can write it out in code, then congratulations, you solved a trivial problem. Now imagine someone who cannot even think up what you just thought up. Do you want to hire that person as a programmer?
for (int i = 0, i<2000000, i++){
GOTO 1
}This includes all numbers, which means all prime numbers up to 2mil
import jquery;
import addAllPrimeNumbersUpTo2Million;
>>59795827
>>59795341
>>59798588
>programming language that requires a huge custom keyboard
Great idea
>>59802869
> custom keyboard
Back in the day, you could get them from the factory this way, because APL was big business.
AIUI It had the special symbols accessed by the shift key, and only uppercase letters.
>>59802850
But seriously, you can't just import everything. You should at least know how to use your fucking libraries...require 'prime'
sieve = Prime::EratosthenesGenerator.new
sum = 0
loop do
prime = sieve.next
break if prime > 2_000_000
sum += prime
end
puts sum