Project Euler

...του ubuntu και έργων ΕΛ/ΛΑΚ (Έργα-Οδηγοί-Προτάσεις)

Συντονιστής: konnn

Re: Πρόβλημα 7 - Project Euler

Δημοσίευσηαπό migf1 » 23 Ιούλ 2011, 11:43

Project Euler, Problem #7
Project Euler, Problem #7 έγραψε:
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?


Λύση #1, σε C (με χρήση συνάρτησης)
Spoiler: show
Κώδικας: Επιλογή όλων

#include <stdio.h>
#include <math.h> // for sqrt()
// -------------------------------------------------------------
int isprime( int num )
{
register int i;

for (i=2; i<=sqrt(num); i++)
if ( num % i == 0 )
return 0; // FALSE

return num < 2 ? 0 : 1;
}
// -------------------------------------------------------------
int main( void )
{
register int i, primecount=0;

for (i=2; primecount < 10001; i++)
if ( isprime(i) )
primecount++;

printf("%d\n", --i);
return 0;
}

Λύση #2, σε C (χωρίς χρήση συνάρτησης)
Spoiler: show
Κώδικας: Επιλογή όλων

#include <stdio.h>
#include <math.h> // for sqrt()
// -------------------------------------------------------------
int main( void )
{
int i,j, primecount = 0;
int isprime = 1; // TRUE

for (i=2; primecount < 10001; i++)
{
for (j=2; j<=sqrt(i); j++)
{
if ( i%j == 0 ) {
isprime = 0; // FALSE
break;
}
}

if ( isprime )
primecount++;

isprime = 1; // TRUE
}

printf("%d\n", --i);
return 0;
}

Τελευταία επεξεργασία από migf1 και 23 Ιούλ 2011, 14:19, έχει επεξεργασθεί 2 φορά/ες συνολικά
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: Πρόβλημα 10 - Project Euler

Δημοσίευσηαπό migf1 » 23 Ιούλ 2011, 13:21

Project Euler, Problem #10 έγραψε:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.


Αυτή με ταλαιπώρησε μέχρι να καταλάβω πως γινόταν υπερχείλιση (overflow) στη μεταβλητή που υπολογίζει το άθροισμα. Τελικά χρησιμοποίησα τύπο: unsigned long long και "%llu" στο printf. Επειδή το long δεν είναι ίδιο σε όλες τις πλατφόρμες, μπορείτε να κάνετε: #include <stdint.h> και να χρησιμοποιήσετε τον τύπο: int64_t με "%lld" στο printf (αν δεν σας δουλεύει δηλαδή καλά με long long).

Επίσης, ο αλγόριθμος που χρησιμοποίησα είναι αυτός του προβλήματος #7 ο οποίος είναι πολύ αργός, ειδικά σε αργά μηχανήματα. Υπάρχουν ειδικοί αλγόριθμοι για το άθροισμα πολλών πρώτων αριθμών, με πιο γνωστό το "κόσκινο του Ερατοσθένη" και της μετέπειτα βελτιώσεις του, η τον αλγόριθμο του ίδιου του Euler (θα τα βρείτε αν γκουγκλάρετε) καθώς και μερικοί ακόμα.

Εγώ βαρέθηκα να ασχοληθώ σε τόσο βάθος, οπότε σας παρουσιάζω την απλή λύση (με χρήση συνάρτησης). Σε I3 με Windows7 64bit Home και compiled με Pelles-C που παράγει 64-μπιτο εκτελέσιμο, κάνει γύρω στα 5 δευτερόλεπτα. Με mingw32 (minimal gcc port για Windows) που βγάζει 32-μπιτο εκτελέσιμο κάνει γύρω στα 20 δευτερόλεπτα.

Λύση 1, σε C (με χρήση συνάρτησης)
Spoiler: show
Κώδικας: Επιλογή όλων

#include <stdio.h>
#include <math.h>
// --------------------------------------------------------------
int isprime( unsigned long long int num )
{
unsigned long long int i;
for (i=2; i <= sqrt(num); i++)
if ( num % i == 0 )
return 0; // FALSE

return num < 2 ? 0 : 1;
}
// --------------------------------------------------------------
int main ( void )
{
unsigned long long int n, primesum = 0;

for (n=2; n < 2000000; n++)
if ( isprime(n) )
primesum += n;
printf("%llu\n", primesum);

return 0;
}
Τελευταία επεξεργασία από migf1 και 23 Ιούλ 2011, 14:36, έχει επεξεργασθεί 2 φορά/ες συνολικά
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: Πρόβλημα 1 - Project Euler

Δημοσίευσηαπό Ilias95 » 23 Ιούλ 2011, 13:50

Μιας και από τι βλέπω το θέμα συνεχίζεται και με άλλα προβλήματα του Project Euler , μια καλή ιδέα θα ήταν να το αλλάξουμε σε Project Euler και να γράφουμε εδώ τις δικές μας λύσεις για τα διάφορα προβλήματα, σε διάφορες γλώσσες. Στο πρώτο post μπορούν να ανανεώνονται οι απαντήσεις οι οποίες θα είναι ταξινομημένες ανά πρόβλημα και ανά γλώσσα.
Ilias95
saintTUX
saintTUX
 
Δημοσιεύσεις: 1548
Εγγραφή: 29 Απρ 2011, 23:26
Εκτύπωση

Re: Πρόβλημα 1 - Project Euler

Δημοσίευσηαπό migf1 » 23 Ιούλ 2011, 14:12

Εγώ το χω ξεκινήσει ήδη όπως βλέπετε πάντως :lol:
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: Πρόβλημα 1 - Project Euler

Δημοσίευσηαπό migf1 » 23 Ιούλ 2011, 14:22

Έκανα μια μικρή διόρθωση στη τιμή επιστροφής της συνάρτησης: isprime(). Αντί για: return num == 1 ? ... ; που είχα πριν το άλλαξα σε: return num < 2 ? ... ;
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: Πρόβλημα 1 - Project Euler

Δημοσίευσηαπό UnKnown96 » 23 Ιούλ 2011, 14:44

Τίτλο να βάλω ως Project Euler αλλά δεν νομίζω να χρείαζετε να τα ταξινομούμε... είναι 300+ προβλήματα νομίζω... και αν βάλουμε και λύσεις, πάει...
Λοιπόν, βάζω τίτλο και συνεχίστε με τα προβλήματα σε οποιαδήποτε γλώσσα!

(Πως τα καταφέρνω και τα θέματα μου πάντα καταλήγουν να έχουν ένα σορό σελίδες! :lol: :lol: :lol: )
Άβαταρ μέλους
UnKnown96
dudeTUX
dudeTUX
 
Δημοσιεύσεις: 370
Εγγραφή: 08 Ιουν 2010, 15:23
Τοποθεσία: Ρόδος
Εκτύπωση

Re: Project Euler

Δημοσίευσηαπό Ilias95 » 23 Ιούλ 2011, 14:57

Απλά θα είναι λίγο δύσκολο να το παρακολουθήσουμε αν απλά τα παραθέτουμε.
Anyway, οι παρακάτω λύσεις όλες σε python:

Problem 1: Add all the natural numbers below one thousand that are multiples of 3 or 5.
Spoiler: show
Κώδικας: Επιλογή όλων
# If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
# The sum of these multiples is 23.
# Find the sum of all the multiples of 3 or 5 below 1000.

def multiple_3or5(n):
assert n > 0, 'Πρέπει να είναι μεγαλύτερος από 0.'
return True if not n % 3 or not n % 5 else False
sum = 0
for number in range(1, 1000):
if multiple_3or5(number):
sum += number
print(sum)


Problem 2: By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Spoiler: show
Κώδικας: Επιλογή όλων
# Each new term in the Fibonacci sequence is generated by adding the previous two terms.
# By starting with 1 and 2, the first 10 terms will be:
# 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
# By considering the terms in the Fibonacci sequence whose values do not exceed four million,
# find the sum of the even-valued terms.

sum = 0
a, b = 0, 1
lista = [a, b]
while True:
a, b = b, a + b
if b >= 4000000:
break
lista.append(b)
for number in lista:
if not number % 2:
sum += number
print(sum)


Problem 3: Find the largest prime factor of a composite number.:
Spoiler: show
Κώδικας: Επιλογή όλων
# The prime factors of 13195 are 5, 7, 13 and 29.
# What is the largest prime factor of the number 600851475143 ?

def largest_prime_factor(n):
assert n > 1, 'Ο αριθμός πρέπει να είναι μεγαλύτερος του 1.'
while True:
a = False
for number in range(2, n):
if not n % number :
n /= number
n = int(n)
a = True
break
if a == False: # Το while σταματάει όταν το a = False, δηλαδή όταν ο n δεν μπορεί να έχει τέλεια διαίρεση
break # με κανέναν αριθμό.
return n
print(largest_prime_factor(600851475143))


Problem 4: Find the largest palindrome made from the product of two 3-digit numbers.
Spoiler: show
Κώδικας: Επιλογή όλων
# A palindromic number reads the same both ways.
# The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
# Find the largest palindrome made from the product of two 3-digit numbers.

def is_palindrome(string):
assert isinstance(string, str) or isinstance(string, int) or isinstance(string, float), 'Ακατάλληλος τύπος.'
string = str(string)
reverse = string[::-1]
return True if string == reverse else False
palindroma = []
for a in range(100, 1000):
for b in range(100, 1000):
c = a * b
if is_palindrome(c) and not c in palindroma:
palindroma.append(c)
print(max(palindroma))


Problem 5: What is the smallest number divisible by each of the numbers 1 to 20?:
Spoiler: show
Κώδικας: Επιλογή όλων
# 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
# What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

def evenly_divisible_from_1_to_n(n):
assert n > 1, 'Πρέπει να είναι μεγαλύτερος του 1'
i = 1
for a in range(1, n + 1):
if i % a:
for b in range(1, n + 1):
if not (i * b) % a:
i *= b
break
return i
print(evenly_divisible_from_1_to_n(20))


Problem 6: What is the difference between the sum of the squares and the square of the sums?:
Spoiler: show
Κώδικας: Επιλογή όλων
# The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385
# The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025
# Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
# Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

def sum_of_the_squares(n):
assert n > 0, 'Πρέπει να είναι μεγαλύτερο του 1.'
sum = 0
for i in range(1, n + 1):
i **= 2
sum += i
return sum
def square_of_the_sum(n):
assert n > 0, 'Πρέπει να είναι μεγαλύτερο του 1.'
sum = 0
for i in range(1, n + 1):
sum += i
square = sum ** 2
return square
difference = square_of_the_sum(100) - sum_of_the_squares(100)
print(difference)
Τελευταία επεξεργασία από Ilias95 και 23 Ιούλ 2011, 15:20, έχει επεξεργασθεί 3 φορά/ες συνολικά
Ilias95
saintTUX
saintTUX
 
Δημοσιεύσεις: 1548
Εγγραφή: 29 Απρ 2011, 23:26
Εκτύπωση

Re: Project Euler

Δημοσίευσηαπό migf1 » 23 Ιούλ 2011, 15:08

@Ilias95: νομίζω θα ήταν χρήσιμο να έβαζες και τις εκφωνήσεις των προβλημάτων πάνω από (ή έστω μέσα) τις λύσεις.
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: Project Euler

Δημοσίευσηαπό Ilias95 » 23 Ιούλ 2011, 15:19

migf1 έγραψε:@Ilias95: νομίζω θα ήταν χρήσιμο να έβαζες και τις εκφωνήσεις των προβλημάτων πάνω από (ή έστω μέσα) τις λύσεις.


Σωστός! Έκανα edit...
Ilias95
saintTUX
saintTUX
 
Δημοσιεύσεις: 1548
Εγγραφή: 29 Απρ 2011, 23:26
Εκτύπωση

Re: Πρόβλημα 8, Project Euler

Δημοσίευσηαπό migf1 » 24 Ιούλ 2011, 22:04

Project Euler, Problem #8 έγραψε:
Find the greatest product of five consecutive digits in the following 1000-digit number:

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

Η λύση μου σε C
Spoiler: show
Κώδικας: Επιλογή όλων

#include <stdio.h>
int main ( void )
{
int i, mul5=1, maxprod=0;
const char *snum = "731671765313306249192251196744265747423553491949349\
6983520312774506326239578318016984801869478851843858615607891129494954595\
0173795833195285320880551112540698747158523863050715693290963295227443043\
5576689664895044524452316173185640309871112172238311362229893423380308135\
3362766142828064444866452387493035890729629049156044077239071381051585930\
7960866701724271218839987979087922749219016997208880937766572733300105336\
7881220235421809751254540594752243525849077116705560136048395864467063244\
1572215539753697817977846174064955149290862569321978468622482839722413756\
5705605749026140797296865241453510047482166370484403199890008895243450658\
5412275886668811642717147992444292823086346567481391912316282458617866458\
3591245665294765456828489128831426076900422421902267105562632111110937054\
4217506941658960408071984038509624554443629812309878799272442849091888458\
0156166097919133875499200524063689912560717606058861164671094050775410022\
5698315520005593572972571636269561882670428252483600823257530420752963450";

for (i=0; *snum; snum++)
{
mul5 *= (*snum - '0');
if (i == 4) {
maxprod = (maxprod > mul5) ? maxprod : mul5;
mul5 = 1;
i = 0;
snum -= 4;
}
else
i++;
}
printf("%d\n", maxprod);
return 0;
}
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

ΠροηγούμενηΕπόμενο

Επιστροφή στο Ανάπτυξη Λογισμικού / Αλγόριθμοι