[ art /
civ /
cult /
cyb /
diy /
drg /
feels /
layer /
lit /
λ /
q /
r /
sci /
sec /
tech /
w /
zzz ]
archive provided by **lainchan.jp**

[ Return /
Go to bottom ]

File: 1489362099263.png (876.97 KB, 300x284, Screen Shot 2017-03-12 at 4.41.11 PM.png)

I've recently gotten really into ProjectEuler and wanted to dedicate a thread to it and similar recreational programming websites. What have your experiences with them been?

At least for me,after progressing to Level 2, I feel like I've sort hit a wall... I try to program the answers for the problems, but most of the times I go about things the wrong way and my solutions take forever to run (and they're not even right most of the times)

I really enjoy doing these problems, but I feel like I'm not thinking about things the right way.

Any thoughts?

At least for me,after progressing to Level 2, I feel like I've sort hit a wall... I try to program the answers for the problems, but most of the times I go about things the wrong way and my solutions take forever to run (and they're not even right most of the times)

I really enjoy doing these problems, but I feel like I'm not thinking about things the right way.

Any thoughts?

I used to do project euler but I stopped for some reason. Great way to learn a new language is to do the first 20 or so challenges.

I think I should start setting aside time for it on weekends, I really liked doing them.

I think I should start setting aside time for it on weekends, I really liked doing them.

File: 1490308370303.png (6.48 KB, 128x128, rest_of_problems_I_am_working_on.rb.txt)

Ive done 20 of the first 30 Project Euler problems. A lot of the problems simply have too long of a runtime and I just had to use smaller inputs and trust that it would work for the inputs asked for. I used Ruby which is a perfect language for PE kind of problems as they have great array/list mapping functions and block functionality built into methods. If anyone wants to help me or give me hints on the problems Im still stuck on then take a look at the code I attached to this post. (I had to add .txt extension to upload it)

File: 1490311739734.png (1.26 KB, 128x128, long_runtime.rb.txt)

>>22625

ok, here Ive attached 4 problems I did that gave correct results with the smaller inputs specified in the problem but have too long a run time, Im talking 6+ hours and not terminating. If you can show me how to optimize the runtime Id be very curious, but I bet you cant.

ok, here Ive attached 4 problems I did that gave correct results with the smaller inputs specified in the problem but have too long a run time, Im talking 6+ hours and not terminating. If you can show me how to optimize the runtime Id be very curious, but I bet you cant.

File: 1490313114292.png (62.21 KB, 200x89, Capture.jpg)

>>22626

I won't show you how because I'm lazy and a bastard but I will tell you, you need to learn how to calculate primes better.

I won't show you how because I'm lazy and a bastard but I will tell you, you need to learn how to calculate primes better.

` ``from numpy import prod`

def lagrange(dp):

l = lambda i: lambda x: (prod([(x - dp[j][0]) / (dp[i][0] - dp[j][0]) for j in range(len(dp)) if i != j]))

return lambda x: sum([l(i)(x) * dp[i][1] for i in range(len(dp))])

My lagrange interpolator for PE 101^

>>22630

You know python lambdas can take multiple arguments right?

You know python lambdas can take multiple arguments right?

` ``[...] lambda i, x: [....]`

[...] l(i, x) [...]

IMO that should be a global function with a better name anyway, just have it take dp, i, and x. Conciseness in simplicity/logic > conciseness in code length. I guess that's up to taste though.>>22636

>I never used that. l is a closure, not a multivariable function.

I know you didn't. I'm saying you should have, and that

Let me be more clear. You code would be better like this:

>I never used that. l is a closure, not a multivariable function.

I know you didn't. I'm saying you should have, and that

`l`

should be a multivariable function. It's even shorter this way, which is apparently something you prioritize too much with function names like `l`

.Let me be more clear. You code would be better like this:

` ``from numpy import prod`

def lagrange(dp):

l = lambda i, x: (prod([(x - dp[j][0]) / (dp[i][0] - dp[j][0]) for j in range(len(dp)) if i != j]))

return lambda x: sum([l(i, x) * dp[i][1] for i in range(len(dp))])

And it would be even better like this:` ``from itertools import product # numpy arrays aren't necessary for this`

def lagrange_help(dp, i, x):

return product([(x - dp[j][0]) / (dp[i][0] - dp[j][0])

for j in range(len(dp)) if i != j])

def lagrange(dp, x):

return sum([lagrange_help(dp, i, x) * dp[i][1] for i in range(len(dp))])

PE101 is about finding a sum, not finding a function that returns a function which finds a sum. Just write it normally.