Project Euler #3 with Python

I tried to solve some Euler’s problems, this is the solution I found for problem #3 with Python. The problem asks:

The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?

Can you, please, suggests me improvements?

def get_largest_prime2(value):     start = time.clock() #used to measure the execution time     divisors = []  #this list will be filled with all number's divisors     i = 1     while value != 1:          while value%i == 0: #check if the value can be divided by i and, if so, iterate the process.             value = (value // i)             divisors.append(i)             if i == 1 : break #it's needed to avoid an infinity loop         if i >= 3: i += 2 #when i is bigger than 3, search divisors among odd numbers( this reduces the search field)         else: i += 1       print( time.clock()-start)     return max(divisors) #returns the max prime divisors 

Elixir solution for Project Euler #2

I have already done many of the Project Euler questions in other languages before, but whenever learning a new language, I like to do the problems again in that language.

Here is my elixir version of

Find the sum of all fibbonaci numbers below 4,000,000.

stream = Stream.unfold({0,1}, fn {a, b} -> {a, {b, a + b}} end) Enum.reduce_while(stream, 0, &(     cond do         &1 < 4000000 and rem(&1, 2) == 0 ->             {:cont, &2 + &1}         &1 < 4000000 ->             {:cont, &2}         true ->             {:halt, &2}     end )) 

Can anyone spot a way to make my code fit the elixir paradigm more? Are there things I could improve?

Euler characteristics in the rank one case

Suppose $ E$ is an elliptic curve over a number field with good ordinary reduction at the primes above a fixed odd prime $ p$ . We are interested in the Iwasawa theory over the cyclotomic $ \mathbb{Z}_p$ extension under the additional assumption that $ E$ has a point of infinite order and also that $ L(E,s)$ has a simple zero at $ s=1$ .

Is there a definition of a modified Euler characteristic for the Selmer group over the cyclotomic extension which can be related to the derivative $ L'(E,1)$ in the framework of the BSD conjecture? Can someone give me some precise references.

bare hand integration of euler class of blow up of a complex surface at a point

let X be a product of two Riemann surface of genus $ \geq1$ and p be a point of X. If X’ is the blow up of X at p. Let E denote the exceptional divisor I am aware that the the second Chern number of X’ since it’s the euler characteristic. I would to verify this by integration of the second chern class. Up to now I try as follows:

Let W be a tubular neighborhood of the exceptional divisor E. Let N be the normal bundle of E $ c_2(X’) = p^*(c_2(X))+c_2(TN)=p^*(c_2(X))+ c_1(TE)c_1(N)$ Now I am not sure how to proceed. $ \int_{X’}p^*(c_2(X))+ c_1(TE)c_1(N)$ This reduces to $ \int_{X’}c_1(TE)c_1(N)$ which I tend to understand as the intersection number. But usually one performs this computation in $ \mathbb{C}P^n$ , which is not the case here. Could anybody give me some help? Thanks.

Project Euler #7 10001st prime in C++


By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

How to optimize this code?

#include <iostream>  bool is_prime(int num) {     for (int i = 2; i <= num/2; ++i)     {         if (num % i == 0)         {             return false;         }     }     return true; }  int main() {     int count = 2;     for (int i = 5; ; i = i+2)     {         if (is_prime(i))         {             count++;         }         if (count == 10001)         {             std::cout << i;             return 0;         }     } } 

Project Euler #1 : Finding the sum of multiples of 3 and 5 under a given number

I wrote the following code in c++14 for hackerRank Project Euler challenge 1.

First I came up with a naive solution by iteration and passed most test cases except 2 and 3 due to timeout as solution is O(N) and test cases 2 and 3 have large input.

The below code is my second attempt at a solution. ALthough I pass the sample test case 0 and custom test case successfully, while attempting to submit I fail at all hidden cases except case 5. I can’t seem to figure out, how do I proceed further. Is my code logic flawed and failing at edge cases that I dont seem to figure out or there is some other issue with the code.

As I am just starting out, please enlighten me with standardised way of doing things.

# include <iostream>  long T; long N;  /* Naive Solution 2 test cases failed due to timeout time complexity O(N)  int Sum_multiple_3and5 (int n) {  int sum = 0;  for (int j = 1; j < N; j++)  {    if ((j % 3 == 0) || (j % 5 == 0))    {      sum = sum + j;    }  }  return sum ; } */  long Sum_multiple_3and5(long n)  { long Given_No = n; long Kfor3 = (Given_No - 1) / 3; //std::cout << Kfor3 << "\n"; long Kfor5 = (Given_No - 1) / 5; //std::cout << Kfor5 << "\n"; long SKfor3 = (((Kfor3) * (3 + (3 * Kfor3))) /2); //std::cout << SKfor3 << "\n"; long SKfor5 = (((Kfor5) * (5 + (5 * Kfor5))) /2); //std::cout << SKfor5 << "\n"; long KforExtraSum = (Given_No - 1) / 15; //std::cout << KforExtraSum << "\n"; long SKforExtraSum = (KforExtraSum /2) * (15 + (15 * KforExtraSum)); //std::cout << SKforExtraSum << "\n"; long sum = ((SKfor3 + SKfor5) - SKforExtraSum); //std::cout << sum << "\n"; return sum; }  /* using sum of sequence formula to sum multiples of 3  to multiple of 5 and subtract from the total the  sum of multiples of 15 */  int main()  { std::cin >> T;  for ( long i = 0; i<T;i++) {  std::cin >> N; std::cout << Sum_multiple_3and5(N) << std::endl; } return 0; }     

Casson invariant and Euler characteristic

A slogan I frequently hear is: “the Casson invariant is the Euler characteristic of the Floer homology of flat SU(2)-connections on the integral homology sphere”. Is there a single paper/reference that essentially states this as a result? It has been difficult to locate precise definitions. In addition, a sketch of how this works would be very helpful.

Derivation of the vortex filament equation from Euler equation

How can the vortex filament equation $ $ \partial_t \chi = \partial_s \chi \wedge \partial_{ss} \chi,$ $ where $ \chi(t,s)$ is a curve in $ \mathbb R^3$ , be derived from the Euler equation $ $ \partial_t \omega + v\cdot \nabla \omega = \omega \cdot \nabla v, \quad \operatorname{div} v = 0,$ $ where $ v:\mathbb R \times \mathbb R^3 \to \mathbb R^3$ , and $ \omega = \operatorname{curl}(v):\mathbb R \times \mathbb R^3 \to \mathbb R^3.$


I’ve asked a more general question at Survey on the vortex filament equation.

Project Euler #357: Prime generating integers

Project Euler Problem 357 asks:

Consider the divisors of 30: 1,2,3,5,6,10,15,30. It can be seen that for every divisor d of 30, $ $ d+30/d$ $ is prime.

Find the sum of all positive integers n not exceeding 100 000 000 such that for every divisor d of n, $ $ d+n/d$ $ is prime.

import time start = time.time()  import math  def divisor_generator(n):     '''Generates the divisiors of input num'''     large_divisors = []     for i in range(1, int(math.sqrt(n) + 1)):         if n % i == 0:             yield i             if i*i != n:                 large_divisors.append(n / i)     for divisor in reversed(large_divisors):         yield divisor  def is_prime(n):     '''simple prime tester'''     if n == 2 or n == 3 :         return True     for i in range(2,int(n**(1/2))+1):         if n % i == 0:             return False     return True  def test_condition (divisor_array, num):      ''' Testing the condition of d+n/d by taking the input as array of divisor and num'''      if len(divisor_array) %2 != 0: # the divisors of num = d3 is [1,d2,d3], and d2*d2=d3 hence d2+d3/d2 = 2d2 which is not prime         return False     if sum(divisor_array) %2 != 0: # the divisors of num = d4, [1,d2,d3,d4] (1+d4)=Prime and (d2+d3)==Prime hence sum([1,d2,d3,d4]) must be even         return False     if len(divisor_array)%2 == 0:         for i in range(len(divisor_array)//2):             if is_prime(divisor_array[i] + divisor_array[-i-1]) == False:                 return False         return True  Count = 0 for num in range(2,31,2): #since for odd num we do not have prime numbers       divisor_list_num = list(divisor_generator(num))      if test_condition(divisor_list_num,num) == True:          Count += num  print(Count) end = time.time() print(end-start,"sec") 

It’s good up to $ \text{num} = 10^6$ but then it gets slow after that. I find that at it takes 16 min to find the $ \text{num} < 10^7$ .

How can I inrease the speed of my code ?

Project Euler Task 42 Solution

So I am relatively new to programming and have been recommended to work through the many tasks in order, in order to better my skills. For some reason, problem 42 had me stumped for a while and, in retrospect, I should have probably reused some code from problem 22 since they involve the same skills and would have probably left me not as stumped. Oh well.

The following is my final – probably horrible – beginner-level code, and I was wondering whether any of you wonderful people would help me improve it and give me some tips for the future. I am learning Python, which is versatile as heck so here goes.

    def calculate_total(word):     alphabet = {'a':1, 'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,'j':10,'k':11,'l':12,'m':13,'n':14,'o':15,'p':16,'q':17,'r':18,'s':19,'t':20,'u':21,'v':22,'w':23,'x':24,'y':25,'z':26}     total = 0     index = 1     while word[index] != '"':         total += alphabet[word[index].lower()]         index += 1     return total  def gen_triangles(limit):     triangles = [0, 1]     index = 1     while triangles[index] <= limit:         triangles.append(((index ** 2) + index) // 2)         index += 1     return sorted(list(set(triangles))) 

So I have been teaching myself some computing theory and have seen that Python’s inbuilt searching keyword ‘in’ performs a linear search on the array so I decided to branch out and see if I can make my own binary search, it seemed to have worked 😀

def binary_search(aList, itemToFind, first, last):   if last < first:     #print(itemToFind + "not in list")     return False   else:     midpoint = (first + last)//2     if aList[midpoint] > itemToFind:       return binarySearch(aList, itemToFind, first, midpoint - 1)     else:       if aList[midpoint] < itemToFind:         return binarySearch(aList, itemToFind, midpoint + 1, last)       else:         #print(str(itemToFind) + " Found at position: " + str(midpoint))         return True   def solution():     myFile = open('Words.txt', 'r')     wordsArray = myFile.read().split(',')     for i in range(0, len(wordsArray)):         wordsArray[i] = calculate_total(wordsArray[i])      triangles = gen_triangles(max(wordsArray))      wordTriangles = []     lengthTriangles = len(triangles) - 1       for i in range(0, len(wordsArray)):         #print('i:', i, 'current index:', wordsArray[i])         if binarySearch(triangles, wordsArray[i], 0, lengthTriangles) == True:             wordTriangles.append(wordsArray[i])      print(len(wordTriangles))  solution() 

What was a surprise to me was that this actually runs really quickly. To all of you guys, though, this is probably a huge mess, but hey, that’s why I’m here!

For those of you who aren’t familiar with the problem, here is the synopsis:

The nth term of the sequence of triangle numbers is given by, t = ½n(n+1); so the first ten triangle numbers are:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …

By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.

Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words?

I can’t wait to see what you all suggest!