[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
/g/ Project Euler
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 77
Thread images: 5
File: i_am_gentoo.png (9 KB, 478x305) Image search: [Google]
i_am_gentoo.png
9 KB, 478x305
Show me a smoother and shorter solution then this.

> Protip
You can't
>>
>programming in C
>using module instead of do and on least significant bit (f & 1)
>>
>>51317307
Unreadable garbare, see me after class.
>>
>>51317307

>c
into the trash it goes
>>
>>51317307
>inconsistent use of spaces in variables declaration
>no spaces in ternary expression
>brackets on new line

I could keep going but you get the point, literally unreadable 0/10
>>
>>51317307
OP pls I hold a pro-C viewpoint on this board but, fuck, you're letting the side down.
>>
>not in hasklel
Come on its not even a challenging problem. Nobody other than you gives a shit about standard solution #5692
>>
>>51317608
wasn't the & 1 thing debunked a long time ago? i thought they were both compiled to the same instruction anyway.
>>
>>51317307
int main(int argc, char **argv)
{
int p=1,n=1,f=1;
int i = 0;
while(f<4000000){
i += !(f%2)?f:0;
f = p+n,p=n,n=f;
}
}



/thread
>>
File: ohreally.png (146 KB, 493x259) Image search: [Google]
ohreally.png
146 KB, 493x259
>>51317307
>value in i is never used.
>program does not produce any output therefor no result. compiler will get rid of it in an instant.
>p,n,f instead of real variable names
>multiple assignment in same line divided by comma
>no return in an int function
>unused include
>writes then instead of than.
>crowded ternary operator.
>code absolutely unreadable.

A smoother and shorter solution? An empty main function would produce the same result.

If i weren't so sure you're a14 year old who just learned what fibonacci is, i'd say you are a failing math junior.
>>
>>51318263
>he doesn't know what competitive programming is
>he thinks people waste time naming variables in programming competitions

git out
>>
>>51318263
>no return in an int function
Tha caught my eye. Does C compile if you don't return anything?
>>
>>51318295
yes but gives a warning
>>
How do I learn to understand what is going on here?
>>
>>51318290
not the person you're replying to, if he was code-golfing he could make it shorter by removing the parameters to main, which is acceptable on gcc, (generates issues on clang), as well as use the implied int type on his variables to save a bunch of space, as well as remove a number of newline and whitespace characters.
I'm not taking sides, but if this was intended as a code-golf solution, it's still not really that good.
>>
what problem faggot
>>
Also, you don't need to do all this checking and summing up to x, you can just add all numbers up to x/2
>>
>>51318445
main(){
int p=1,n=1,f=1,i=0;
while(f<4000000){
i += !(f%2)?f:0;
f = p+n,p=n,n=f;
}
}

this compiles too
>>
>>51318446
Adding up all the even fib numbers under 4M.

>>51318415
p and n are the previous numbers (the fib number is the sum of the 2 previous fib numbers), they are initialized to 1 for the base case

on each iteration of the while loop, it checks if the current fibonacci number is even (% is the mod operation, if it's zero it means there's no reminder when you divide the 2 numbers, therefore x mod 2 == 0 means it's divisible by two -> event)
then it sets p and n to the previous fib numbers and f to the current one

i is the accumulator, if the fib is even it adds it to i.
foo == bar ? baz : qux is shorthand for
if (foo == bar){
baz
}
else qux

The code is a mess, it's ugly. The problem is literally babby tier, and OP is a faggot.
>>
>>51318483
Yes, that's what I was proposing.

*should* be able to remove the int type declaration on line 2, perhaps. Also the whitespace on later lines around the i and f variable assignments in the loop body. Removing indentation is controversial but in this case the program is simple enough that you can probably keep it relatively readable for C golf.
>>
>>51318555
yeah and you dont need to write 4million , and curly braces
main(){
int p=1,n=1,f=1,i=0;
while(f<4e6)
i += !(f%2)?f:0,f=p+n,p=n,n=f;
}
>>
>>51318290
>competitive programming
>euler project
go home, kid.
>>
>>51318758
stay mad faggot
>>
>>51318774
It's the second problem, if it were a more advanced one it would be understandable, but this...
>>
File: crying-baby.jpg (33 KB, 600x399) Image search: [Google]
crying-baby.jpg
33 KB, 600x399
>>51318774
>>51318290
>>
>>51318846
so mad
>>
File: nul.png (78 KB, 500x376) Image search: [Google]
nul.png
78 KB, 500x376
>>51317307
How the fuck is this a solution. It doesn't output anything.
>>
File: dancegoose.gif (750 KB, 500x351) Image search: [Google]
dancegoose.gif
750 KB, 500x351
>>51317307
Here is something smoother:
#include <stdio.h>

int main( int argc, char *argv[] ) {
int fib_even=2,fib_prev=1,fib_tmp;
int sum=0;

while(fib_even < 4e6) {
sum += fib_even;
fib_tmp = fib_prev + fib_even;
fib_prev = fib_even + fib_tmp;
fib_even = fib_prev + fib_tmp;
}
printf("%d\n",sum);
return 0;
}


No checks if a number is even.

If you kids use your brain a little you'll know why this works.

Booya
>>
>>51319300
this image triggers the fuck out of me.

but it's very useful bait.
thanks.
>>
>>51318183


I got :

00000000004004ed <mod>:
4004ed: 55 push rbp
4004ee: 48 89 e5 mov rbp,rsp
4004f1: 89 7d fc mov DWORD PTR [rbp-0x4],edi
4004f4: 8b 45 fc mov eax,DWORD PTR [rbp-0x4]
4004f7: 99 cdq
4004f8: c1 ea 1f shr edx,0x1f
4004fb: 01 d0 add eax,edx
4004fd: 83 e0 01 and eax,0x1
400500: 29 d0 sub eax,edx
400502: 5d pop rbp
400503: c3 ret

0000000000400504 <and>:
400504: 55 push rbp
400505: 48 89 e5 mov rbp,rsp
400508: 89 7d fc mov DWORD PTR [rbp-0x4],edi
40050b: 8b 45 fc mov eax,DWORD PTR [rbp-0x4]
40050e: 83 e0 01 and eax,0x1
400511: 5d pop rbp
400512: c3 ret


source code :
int    mod(int nbr)
{
return (nbr % 2);
}

int and(int nbr)
{
return (nbr & 1);
}

>>
>>51319977
Triggered by the filename or the contents? If it's the contents, you may find some more over here: https://www.youtube.com/watch?v=3bjXGrycMhQ
>>
>>51317608
>>51318183
>>51320044
http://c2.com/cgi/wiki?PrematureOptimization
>>
>>51317608
It's optimized to exactly that in every compiler I ever used. You are supposed to code in a way that is as readable as possible (that's why you use abstractions in the first place), but can be optimized as efficiently as possible. You don't need to use obscure tricks to get the best performance any longer. The optimizer is probably a lot better at that than you are. I get the obsession with short and elegant code, but most of the time it's just useless to use shortcuts, like ? or :.
>>
x = 1
y = 0
h = 0
for w in range(1, 40):
x, y = x+y, x
if x % 2 == 0 and x <= 4000000:
h = x + h
print h
>>
>>51320284
>python
>for w in range(1,40)
>h = x + h
>print h for every even fibonacci
>not using python3
you got a long way to go son.
>>
>>51320423
>print ("string")
>not print "string"
enjoy typing the parentheses
moreover
>python
>not haskell
>>
>>51320044
Is your compiler retarded? Why not just
mov eax,edi ?
>>
You people are hopeless. Do not brute force Project Euler. It does not work.

In the Fibonacci sequence, if F(n) is even, F(n+3) is even. Use this, and memoization, and you can write a solution to problem 2 (in C) that doesn't suck dicks.
>>
>>51323410
You're still going to have to calculate every Fibonacci sequence. Congratulations, you saved yourself some time evaluating if conditionals! Do you also manually unroll your loops?
>>
>>51323526
Yes.

The point is not to do problem 2 faster or in less space, it is to demonstrate that even on problem 2, there are "better" solutions.

You should not need to check if F(n) is divisible by 2, because you should be analysing the problem posed and finding that when F(n) is even, so is F(n+3).

You should be memoizing problem 2 if you are writing C solutions, simply to demonstrate that you know (or can find) how to.
>>
>>51323584
Enjoy your 1% speedup.
>>
>>51323606
1% on problem 2 is 4E8% on problem 100 :^)
>>
>>51320044
what optimization flag?
>>
>>51318295
I'm not sure about C but it's officially legal in C++ only for main, otherwise undefined behavior. For main it's equivalent to return 0;
>>
>this thread
Let g_n only denote the even valued terms in f_n, then:

g_(n+2)=g_(n)+4*g_(n+1)

starting from g_0=2 and g_1=8 it generates all even valued terms in the fibonacci sequence, you don't have to calculate all the values between.

>learn2math
>>
>>51324624
so according to this
int main(int argc, char **argv)
{
int p=2,n=8;
int i = p+n;
int f;
while((f=p+4*n)<4000000){
i+=f;
p=n;
n=f;
}
}

Runs considerably faster, should return the same number.
>>
>>51324375
Undefined in C but will most likely return whatever the last function you called returned. Try this for example:
int foo(void)
{
return 41;
}

int main(void)
{
foo();
}
>>
>>51324665
Runs faster and returns the same number:
int main(void){return 176;}
>>
>>51317307
>argc and *argv[] unused entirely, why not just main(void)
>program that does whatever without ever outputting anything at all to screen or disk or anywhere else

Pointless stuff is just that, pointless.
>>
>>51317701
Then throw your computer into the trash right now, because its firmware, OS kernels, hardware drivers and whatnot are all written in C, retard.
>>
>>51324665
>should return the same number
>doesn't return or print any number
son your retardid
>>
>>51320089
>https://www.youtube.com/watch?v=3bjXGrycMhQ
I can listen to SPJ talking for hours.
>>
>all the faggots in this thread optimizing the second (2nd, just let that sink in) Euler problem

Do you do this because you're too stupid to work on the real problems but you want to feel smart so you delude yourself into believing you're actually writing optimized code?

And the worst part is, that even the best answer in the thread >>51319678 is still babby tier and is barely better than checking mod 2.

For all the patronizing attitude this board has against "plebs" and "normies," most of you are EVIDENTLY just, if not more, stupid than those people you criticize.
>>
>>51318621
main(){int p=1,n=1,f=1,i=0;while(f<4e6)i+=!(f%2)?f:0,f=p+n,p=n,n=f;}

>minimalism
can't get any smaller than this
>>
>>51317854
Fucking unreadable indeed

I hope faggot OP doesn't write this kind of shit code when it needs to be shared with others.
>>
print(4613732)
>>
anything looks better in haskell

main = print $ sum (takeWhile (<= 4000000) (filter even fibs))
where
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
>>
>>51328480
you literally copypasted that answer

this board sucks, and i'm an idiot for coming back
>>
>>51329049
Nuh uh. I thought of it all by myself. I guess great minds (Haskell users') just think alike :^)
>>
>>51329077
My friend uses Haskell regularly. He just told me he wants to suck a big cock.
>>
>>51329217
Ignore him, it's a troll (>>51329049). He's not me (>>51328480)

I didn't copy that though. The only not-invented-here part is the fibs definition, which I learned some years ago.
>>
>>51320284
Nice bird
>>
>>51329049
nope. all me

check out this method for calculating an inverse square root i just thought of

float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;

x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed

return y;
}
>>
>>51318414
>C gives you a warning
you're retarded
>>
>>51320044
>>51321291
>>51324352
That's because f%2 and f&1 do different things, you idiots.
>>
>>51318414
>I don't know C
>>
>>51324673
>Undefined in C
No, it's defined to return 0.
>>
>>51329491
John Carmack?
>>
>>51318290

No, instead they waste time writing include statements and main() arguments that they will never use.
>>
if f1 = { [ 1+sqrt(5) ] / 2 }^2
and f2 = { [ 1-sqrt(5) ] / 2 }^2
and n = 4*10^6
then the solution is
{ [1-f1^n] / [1-f1] - [1-f2^n] / [1-f2] } / sqrt(5)
>>
>>51318183
>i thought they were both compiled to the same instruction anyway.
Depends on if the compiler does that optimization for you.
>>
desu
>>
>>51320044
>intel syntax
>>
p=1,n=1,f=1,i=0;main(){while(f<4e6)i+=!(f%2)*f,f=p+n,p=n,n=f;}
>>
>>51317307
Can't read for shit.
Translate to python gommenasai arigato senfam.
>>
>>51320423
>not using python3
How is that an issue? Besides the unicode thing (and py3k arguably didn't even get that right) the difference between 2 and 3 is extremely shallow. On the other hand the 2.x series is far more widely available and looks more natural to more people (it's somewhat easier to communicate in)
>>
>>51330557
Did you test the example I posted. Because it sure as shit does not return 0 (on gcc 4.9.2)
Thread replies: 77
Thread images: 5

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.