int main()
{
int64_t* first_row = (int64_t*)malloc(sizeof(int64_t));
int64_t* previous_row;
int64_t* next_row;
int64_t i,size;
first_row[0] = 1;
size = 1;
print_row(first_row,size);
previous_row = first_row;
for(i = 0; i<30;i++) {
next_row = next_pascal_row(previous_row,size);
size++;
print_row(next_row,size);
free(previous_row);
previous_row = next_row;
}
free(next_row);
}
Opinions? Yeah, int64_t is probably a GNU extension, I don't care as long as it works with gcc. U mad? Also, post you're own solutions. If you can't do the Pascal, you can just go ahead and apply to your're nearest McDonald's, because you ain't worth shit.
Looks like crap. I did this, in C, in my college entrance exam (not in the US, I have no idea if there's such a thing there) and it didn't look like crap. I don't remember much else. Please, don't turn this into another fizzbuzz.
If you wish to assume the role of a post-USSR mathematical genius you better start posting some evidence that you're just a good-for-nothing blabbermouth.
Name:
Anonymous2012-01-05 18:33
>>1
And what do you do for a living? BTW, there are two three memory leaks in your program.
Name:
Anonymous2012-01-05 18:34
>>5
No there isn't. Every int64_t array that's malloced gets freed.
Name:
Anonymous2012-01-05 18:38
>>4
Fortunately, I missed USSR by one step to the west. I'm just about the furthest imaginable thing from a mathematical genius but I know how to write simple solutions to simple problems.
>>6
You still haven't told us what you do for a living. But yes, there is at least one memory leak. Pstt.. it's in main() you stupid fucker.
Name:
Anonymous2012-01-05 18:50
>>8
There is just one malloc()'d pointer in main() and it is free()'d because the for-loop is entered at least once. See, first_row is assigned to previous_row just before the loop and previous_row is free()'d after that. Pointers malloc()'d by next_pascal_row are free()'d by the same row, with the exception of the last one which is free()'d just after the loop.
But I provide expensive senior software engineering services to our client company as my nine to five job, if that was what you were asking.
This line will cause a memory leak if the sign changes. Now there is another leak in main(). And for anyone that has a clue about C, please don't help this mental midget.
>>12
Listen you stupid fucker. What happens if the bit pattern is becomes unsigned? There will be a memory leak.
A sign change here is an impossibility and makes no semantical sense.
A sign change is a possbility. The fact that you think it's impossible leads me to believe that you've never written a single line of real C code in your entire life. Also, a compiler can't detect a sementic error.
Name:
Anonymous2012-01-05 19:04
>>12
Now what's the range of int64_t you stupid shit jew? Now, compare this range to an unsigned int you stupid fucker. Now shut up and learn something.
>>15
Admit it. This guy is a bigger retard than FrozenRetard, I mean FrozenVoid.
Name:
Anonymous2012-01-05 19:09
>>13
uh what. int64_t* is a pointer type sharing the same value range as int*, void* or char*. The fact that it points to signed values is just a coincidence. Pointer is a pointer, and it's safe to cast the return value of malloc() to any kind of pointer.
I am not casting a pointer to a signed integer. Pay attention to the asterisks.
>>17
But what it points at will cause a memory leak. An yes, the bit pattern is signed. Maybe you should just shut the fuck up and take read the ANSI/ISO C89 standard. Also, you still haven't told me where the other memory leak in main() is?
Geeze, I'm I gonna have to run a memory profiler against your shit code and post the results for you?
Name:
Anonymous2012-01-05 19:12
Hacker News
I'm sorry, I can't hear you over the sound of a million dead Steve Jobs cocks in your mouth.
>>17
There is also a semenatic error in the for loop in print_row(). In other words, depending on the system, the results might not actually display to the screen.
>>18
As far as I know, I can malloc whatever I want and free cleans it up as far as I use the same pointer and don't do any dirty tricks. I also ran the code with instrumentation and checked that amounts of malloc and free match up.
GCC guarantees it.
Name:
Anonymous2012-01-05 19:17
>>20
But it works on my systems and adheres to the definition of printf(). If you're comjpiler doesn't support %lld, well, fuck you then.
Name:
Anonymous2012-01-05 19:19
>>21
Yes, that's true for a signed int. But again, what happens if the int becomes unsigned? Again, there will be a memory leak. And yes, there are trivial situations where something like this could happen.
>>22
I said it was a semantic error. In other words, the code will pass the compilation phase. However, the error will occur during run time. Holy shit you're one stupid fucker. Please tell me your trolling.
>>28
This is actually a simple solution to a simple problem. Won't bother if it's O(fast) but it's way better than that faggot's convoluted answer to a simple problem.
Hey, kodak. Fuck off, ``faggot".
Name:
Anonymous2012-01-05 21:00
>>32
>implying it wasn't long established that kodak is a shitty troll
var counter as integer;
var ptLine as integer[20];
procedure Step(istep as integer);
var couter as integer;
begin
ptLine[istep] := 1;
if (istep<>1) then write( " " + ptLine[1]);
if (istep>=3) then begin
for couter := 2 to istep-1 do begin
ptLine[couter] = ptLine[couter] + ptLine[couter-1];
write(" " + ptLine[couter]);
end;
end;
writeln(" " + ptLine[istep]);
end;
begin
for counter:=1 to 20 do begin
Step(counter);
end;
end.
>>37
Only appears to make correct answers for first 20 rows; 21 and later are completely wrong, and the 66th row causes a SIGFPE.
Name:
Anonymous2012-01-05 23:03
>>1
I've also discovered that going up into high row numbers, there are lots of huge negative values in the triangle, immediately signalling that your program gives the wrong result.
>>42
Yes, that is because I use factorials. When fact(n) starts returning UINT64_MAX (18446744073709551615) or overflowing, the division in pascal() stops working properly. The SIGFPE happens when pascal() tries to divide by zero because of an overflow in either fact(col) or fact(row-col) which perchance happens to make either of them 0.
It shows up as early as row 20 because factorials grow faster than the binomial coefficients.
If one tried hard enough they could exploit the fact that n!/k! for k < n is the same as n*(n-1)*...*(k+1), to make it not overflow as quickly.
Name:
Anonymous2012-01-06 0:14
>>49
Isn't the use of factorials extremely slow, especially as your factorial results aren't cached? Wouldn't it be faster to iteratively add?
>>50
I don't particularly care about the speed, though. It would certainly be faster to add, as well as less error prone, because you can't trivially eliminate the factorial divisions and so things overflow rapidly.
The below works properly with row 20, but still gives incorrect results quickly:
uint64_t mul(uint64_t n, uint64_t k) {
if (k >= n || n == 0) { return 1; }
if (k == n-1) { return n; }
uint64_t r = 1;
while (n > k) {
r *= n--;
}
return r;
}
Not to mention that going as far as implementing caching for something like this would be going way too far. I'm sure the cleanest, simplest and probably most efficient version will end up being >>38's.
>>38
You could save a lot more memory by storing only the last and current rows, but I believe the malloc time overhead would be large.
Name:
Anonymous2012-01-06 0:35
>>53
How about allocating two arrays of n_rows length? That'd make the memory complexity O(2r) => O(r) (as opposed to O((r*r+r)/2) => O(r^2)) and still only require one or two (constant) malloc calls.
Name:
Anonymous2012-01-06 0:36
>>54
Hmm, didn't think of that. I could modify my pascal.c to implement that, thanks.
Name:
Anonymous2012-01-06 1:52
>>11
C89 doesn't have int64_t so of course that code doesn't conform.
From article:
>That gives a row from a Pascal triangle
#!/bin/python
def main():
print("Gief number plox")
row = input()
print "row", row, "is"
for i in xrange(row+1):
print(fac(row)/(fac(i) * fac(row-i)))
def fac(x):
if x<=1:
return 1
else:
return x * fac(x-1)
main()
herpderp
Name:
Anonymous2012-01-06 8:08
>>60
>using the slower factorial method over the faster iterative addition method (time complexity: O(fuckingretarded))
>using input() instead of int(raw_input()) (allowing user code injection)
>using python
>using /bin/python which no known distribution uses instead of /usr/bin/python or better /usr/bin/env python
Name:
Anonymous2012-01-06 8:14
>>61
I haven't used python in a year. Cut me some slack.
Also, if I do raw_input() it'd be a string and I'd have to recast it.
Name:
Anonymous2012-01-06 8:17
>>62
There is no excuse for using Python at all. Also, using input(), like I said but you couldn't read, allows users to break your program with errors, and/or run code in the program with their input. This is fucking retarded.
Name:
Anonymous2012-01-06 8:24
>>63
This isn't meant to be enterprise code that millions of dollars depend on. It's a small sample to demonstrate a way of generating a row of Pascal's triangle.
If you can write better code than this, that's great for you. But don't get so worked up over every irrelevant detail, being butthurt doesn't help anyone.
You don't want to get a heart attack before you're 50.
Name:
Anonymous2012-01-06 8:40
>>64
Having a shitty time complexity is a BIG FUCKING DEAL.
Name:
Anonymous2012-01-06 8:50
>>65
It's a real deal in other situations and not relevant in this one.
Name:
Anonymous2012-01-06 8:50
>>66
Not relevant? How's blowing out reasonable runtime for any non-trivial triangle size?
Name:
Anonymous2012-01-06 8:53
>>67
I don't do much mathematical processing (like in graphics or physics or other calculations), and I've never needed to deal with triangles like this in any of my work.
Here is the ROBUST SCALABLE ENTERPRISE SOLUTION, it came out a lot faster than the program in >>38 I'm guessing this is more cache efficient. It's also more robust when it comes to bad input. I decided not to comment this time since some of you showed some ill-will the last time I did that.
int main()
{
int col;
printf("Which column to print? ");
scanf("%d",&col);
printPascal(col);
return 0;
} Implementation of printPascal function is left as an exercise for the reader.
Name:
Anonymous2012-01-06 18:39
This one should use up the least memory. Works until integer overflow happens. #include <stdio.h>
#include <stdlib.h>
int main (int argc, char **argv) {
unsigned long int inputrow, i, j;
unsigned long int *row;
scanf("%ld", &inputrow);
row = malloc(inputrow*sizeof(unsigned long int));
row[0] = 1;
for(i=1; i<inputrow; i++) {
row[i]=1;
for(j=i-1; j>0; j--)
row[j]+=row[j-1];
}
for(i=0; i<inputrow; i++)
printf("%ld ", row[i]);
free(row);
return 0;
}
Name:
Anonymous2012-01-06 21:06
>>73 int main(/* ... */)
{
/* ... */
} Implementation of main() function is left as an exercise for the reader.
>>77
But it is capable of doing Strong AI after all!
Name:
Anonymous2012-01-07 4:06
>>78
Is it? You can't even multiply (by any variable) in Presburger arithmetic. It's much weaker than Peano Arithmetic, which allows multiplication, but then if you add multiplication, you can encode entire programs within arithmetic, making the deciding the truth value of some sentences undecidable (this result is known in some forms as Godel's incompleteness theorems). No Strong AI will be able to solve the general halting problem, but any strong AI (or even human "AI"), should be able to decide on various specific cases, and they can self-improve to decide on more and more of them, just never 'all'.
>>88 void main the space allocated in r0 = (size *)malloc(sizeof(size) * (n + 1)); is dereferenced but not freed before it's even used [m]r0[/code] is there for no reason
Why do you do that?
Name:
Anonymous2012-01-08 18:10
lol
U MENA PASKALL
Name:
Anonymous2012-01-08 18:12
>>90
What do you mean r0 is not used? I use it in every iteration. Also, why would you free r0 before it's used? I don't get it....
I also never understood the point of declaring main as int then not returning anything.
>>92
First, the first thing you do in the loop is assign r to r0, effectively discarding r0's malloced space.
Second, you can easily remove r0 altogether and your shitty code will still do the same thing it does now, just a bit better.
Third, the return value from main is returned to the OS (usually the shell or other program executing it) and is required by the C standard.
Go read your K&R already.
Name:
Anonymous2012-01-08 18:39
No one has said anything about my code. Can someone tell me if it sucks? Pretty please? I'm not very good at this C thing. >>74
Name:
Anonymous2012-01-08 18:57
>>94
I think I understand now. I did think that the r0 = r part of my code was odd. It seems work work now. Thanks.
#include <stdio.h>
#include <stdlib.h>
typedef long long size;
void copy(size*, size*, int);
First, the first thing you do in the loop is assign r to r0, effectively discarding r0's malloced space
That's incorect. r0's malloced space doesn't get discarded at this point in the loop. This space actually gets used. Cripes, relearn pointers you fucking stupid shit.
Third, the return value from main is returned to the OS (usually the shell or other program executing it) and is required by the C standard.
The return value is undefine according to both ANSI/ISO C89 and C99.
Go read your K&R already.
I think you need to go practice what you preach you mental midget.
I think you need to find a programming job you dumb toilet scrubber.
Name:
Anonymous2012-01-08 19:14
>>103
i'm sorry did i hurt your feelings? It's okay, I'm sure it was 100% your fault the company failed and went under. Maybe next time you'll learn better.
>>102
Depending on the OS, the return value could possibly be the exit status of the process. But I don't know because what you have is non standard C code. In other words, I can't get this thing to compile on either my Linux or Windows XP box.
>>1 the Hacker News challenge
It's not a fucking challenge. It was mentioned as an example of a simple programming problem to weed out weak job candidates. Only you talentless autistic hacks would ever consider something like this a challenge, and then actually do it to ``prove yourself''.
If you want a challenge, build a facial recognition system from scratch using only training data that you've gathered yourself. That's a fucking challenge. Not this two-lines-of-C Pascal triangle shit, Jesus Christ.
stop listening to kodak_jobless_moron, he's just like all the other tripfags who have no clue what they are talking about and act like elitists because they can.
Name:
Anonymous2012-01-08 19:26
>>109
Here's one that we use to weed out some of the week candidates.
We show the person a balanced binary tree. Then we ask them to write a function in either C or C++ that would convert this tree to a doubly linked list. The restrictions being that you can't use an external stack and you can't use any kind of global variables.
>>119
Huh? You still can't find the passage to support your incorrect assertions can you? Also, I still have a job. Now shut up and go scrub another toilet.
Name:
Anonymous2012-01-08 19:53
>>118
I find it kind of funny how >>109 hasn't posted a solution yet.
>>125
You forgot your tripcode jobless_programmer.
thanks for making it clear you take it off to try to get your points across in a lame attempt to make it seem like other people agree with you.
Name:
Anonymous2012-01-08 19:59
>>127
Well, again, for like the 5th time you ADD fucker, you still haven't cited the C89 or C99 passage that supports your asssertion. And you still haven't told us what you do for a living.
Are these questions too difficult for you to answer you fucking retarded stupid shit?
Name:
Anonymous2012-01-08 19:59
>>126
What do you do for a living? I scrub women's toilets.
>>133
Give me your room number kodak-san and your name.
I'll gladly take a drive up to Emeryville CA just to see no kodak-san.
I do like how you keep forgetting to put on your trip.
Name:
Anonymous2012-01-08 20:04
>>132
Well you made light of the Pascal's Triangle problem. So I decided to post a problem that you might kind non trivial. I was really expecting a person of your caliber to have posted a solution by now.
I do like how you keep forgetting to put on your trip.
I do it to pass the filters.
Name:
Anonymous2012-01-08 20:06
>>133
You're more than welcomed to post your badge + timestamp to prove you even work anywhere in the corporate world
Name:
Anonymous2012-01-08 20:07
>>136
Like I said, I missed the part where you said, "solve this problem", or "can you solve this?", because you never did, and if you did I would probably reply "are you offering me a job?"
>>137
So you purposely try to ruin /prog/ by annoying everyone that you can?
Name:
Anonymous2012-01-08 20:10
>>140
Nah. I'm just annoyed with this uneducated burger flipper. Seriously. This guy needs to learn something otherwise he will be working some shit hourly job for the rest of his life.
kodak-san how does it feel to:
--> Have no job
--> Live in your parents basement
--> Spend your entire day on /prog/ and probably an imageboard
--> Be less intelligent than the great FrozenShit
--> Have to make fun of people just to sleep at night without crying about your shitty life
Dear Anonymous (>>145), how does it feel to:
--> have no life
--> be fat
--> be ugly
--> never kissed a girl
--> never hugged a girl
--> never had a girlfriend
--> spend your entire day on /prog/ and probably an imageboard
--> scrub toilets all day erry day
--> be a mental midget
--> attack your betters
I wouldn't know because I'm a superior specimen of the human species and so is mai waifu.
It's okay that you're all of those points, not everyone is a sad lonely faggot such as yourself. Please finish scrubbing my toilet and suck on my big meaty balls.
Name:
Anonymous2012-01-08 20:29
>>147
Prepare to defend yourself, I will not have you tarnish the reputation of mai waifu in this manner.
FOR KODAK-SAAAAAAAAAAAAAAAAAAAAAAAAAAN
Name:
Anonymous2012-01-08 20:36
I think Kodak-san is kind of cute, he's just tsundere.
>>150
Maybe he is a really intelligent burger flipper who never got an education because he had to help his single mother pay for the bills while she worked as a stripper at a local bar who now knows the true value of education and work experience but doesn't have the opportunity to get it himself so he helps us with our C programs since he's read the standards and understand them.
>>154
Or maybe his father killed himself, while his mom just watched TV, drank booze and smoked shit all day long just like the rest of /prog/ and he picked up some C on the side.