Python Project Euler - 1 , 2 , 3 , 4

preview_player
Показать описание
like 'lets play' but with more code
Slightly more advanced python doing some number problems from ProjectEuler. List comprehensions and generator expressions. Generators.

1) Multiples of 3 and 5
2) Even Fibonacci numbers
3) Largest prime factor
4) Largest palindrome product
Рекомендации по теме
Комментарии
Автор

“Obviously I’m not doing it right... But I feel like it’s right”

Programming in a nutshell

Philgob
Автор

Solving the problem first and then watching your videos is a great way of learning to write better code. I'm enjoying myself immensely, thank you very much!

counterretort
Автор

For the 3rd problem, when you factored 21, it only output 3 as it's factors, but wouldn't 7 be the largest prime factor of 21? also, how come it only need to iterate to the square root of the number?

andrewlee
Автор

in euler2, the problem said fib number below 4 million. why did the sol have fib(10million)??

fardeenpanjwani
Автор

4)
def is_palindrome(num):
num = str(num)
return num == num[::-1]

def all_combinations(first, last):
base = []
for i in range(first, last):
for n in range(first, last):
r = i * n
if r not in base and is_palindrome(r):
base.append(r)
return base

print(max(all_combinations(10, 100)))

nddonkj
Автор

hi thanks for your efforts,
i have a question for third question....
why did you say if len (factor(i))==0, not understand very well actually

ihsankundup
Автор

I tried to do euler 1 by hand but was too lazy, but here is my very efficient solution:
def sumMultiplesOf3or5(N):
return sumMultiplesOfKupToN(3, N) + sumMultiplesOfKupToN(5, N) - sumMultiplesOfKupToN(15, N)

def sumMultiplesOfKupToN(K, N):
n =(N-1)//K
sumNaturalsUpton = (n*(n+1))/2
return K*sumNaturalsUpton

poketoscoparentesesloparen
Автор

in euler 1 you're using O(N) complexity, you can solve that problem using O(1)

armandomendivil
Автор

Problem 4: One could use itertools.combinations(range(100, 1000), 2) and the is_palindrome function is as simple as: return str(number) == str(number)[::-1] (i know this vid is over 2 years old and i assume you would code it this way by now, too. No pun intended.)

brotherlui
Автор

for the even fibonacci question why did you put "to = 10" as a parameter in the function

Kingofqueers
Автор

Can you explain the for loop used in problem 3

nimmakayalakaushik
Автор

Interesting, but I made same mistakes... Thank you for the tutorial.

iKostanCom
Автор

You say that you don’t want 2 back from your prime factorization, but 2 is a prime number...

jimjam
Автор

Hey i am trying to solve with this method but not getting it 4th problem.
First i define is_palindrome function which returns true or false, and then calling two nested while loops from 10 to 100
and sending their product to is_palindrome function, if it returns true then appending that number to an empty list, afterwards i am calling max operator on that list, is this right way of doing it?

subhambiswas
Автор

What is the name of the step we are doing at 15:50 ?

mrmertozturk
Автор

hey how many have you done on your own profile?

asadullahfarooqi
Автор

for problem 1 my c++ program was around 8 times faster than his python solution (idea: utilize Gauß sum):
#include<bits/stdc++.h>
using namespace std;
int f(int n)
{
int sum=0, a=ceil((float)n/5.0)-1, b=ceil((float)n/3.0)-1, c=ceil((float)n/15.0)-1;
sum += 5*a*(a+1)/2;
sum += 3*b*(b+1)/2;
sum -= 15*c*(c+1)/2;
return sum;
}
int main()
{
printf("%d", f(1000));
return 1;
}

ep
Автор

Почему во второй задаче он поставил Увы английский только начал учить

ЭдвардЕвгенич
Автор

Problem 3, I would have done the following (in C)
for (int i = 2; i*i <= n; i++) {
while(n%i == 0 && n != i) n /= i;
}
return n;

ylamummo