Merge pull request #105 from koseokkyu/heapsort

Project Euler solving Problem 01
This commit is contained in:
Christian Bender 2017-12-22 23:30:12 +01:00 committed by GitHub
commit 4021a3f8ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 200 additions and 0 deletions

View File

@ -0,0 +1,19 @@
/*
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.
*/
#include <stdio.h>
int main() {
int n = 0;
int sum = 0;
scanf("%d", &n);
for (int a = 0; a < n; a++) {
if ((a % 3 == 0) || (a % 5 == 0)) {
sum += a;
}
}
printf("%d\n", sum);
}

View File

@ -0,0 +1,24 @@
/*
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 N.
'''
'''
This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3.
*/
#include <stdio.h>
int main() {
int n = 0;
int sum = 0;
scanf("%d", &n);
int terms = (n - 1) / 3;
sum += ((terms)*(6 + (terms - 1) * 3)) / 2; //sum of an A.P.
terms = (n - 1) / 5;
sum += ((terms)*(10 + (terms - 1) * 5)) / 2;
terms = (n - 1) / 15;
sum -= ((terms)*(30 + (terms - 1) * 15)) / 2;
printf("%d\n", sum);
}

View File

@ -0,0 +1,49 @@
/*
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 N.
'''
'''
This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3.
*/
#include <stdio.h>
int main() {
int n = 0;
int sum = 0;
int num = 0;
scanf("%d", &n);
while (1) {
num += 3;
if (num >= n)
break;
sum += num;
num += 2;
if (num >= n)
break;
sum += num;
num += 1;
if (num >= n)
break;
sum += num;
num += 3;
if (num >= n)
break;
sum += num;
num += 1;
if (num >= n)
break;
sum += num;
num += 2;
if (num >= n)
break;
sum += num;
num += 3;
if (num >= n)
break;
sum += num;
}
printf("%d\n", sum);
}

View File

@ -0,0 +1,28 @@
/*
Problem:
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 n, find the sum of the even-valued terms.
e.g. for n=10, we have {2,8}, sum is 10.
*/
#include <stdio.h>
int main() {
int n = 0;
int sum = 0;
int i = 1;
int j = 2;
int temp;
scanf("%d", &n);
while (j <= n) {
if ((j & 1) == 0) //can also use(j%2 == 0)
sum += j;
temp = i;
i = j;
j = temp + i;
}
printf("%d\n", sum);
}

View File

@ -0,0 +1,57 @@
/*
Problem:
The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N?
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
*/
#include <stdio.h>
#include <math.h>
int isprime(int no) {
int sq;
if (no == 2) {
return 1;
}
else if (no%2 == 0) {
return 0;
}
sq = ((int)(sqrt(no))) + 1;
for (int i = 3; i < sq; i + 2) {
if (no%i == 0) {
return 0;
}
}
return 1;
}
int main() {
int maxNumber = 0;
int n = 0;
int n1;
scanf("%d", &n);
if (isprime(n) == 1)
printf("%d", n);
else {
while (n % 2 == 0) {
n = n / 2;
}
if (isprime(n) == 1) {
printf("%d\n", n);
}
else {
n1 = ((int)(sqrt(n))) + 1;
for (int i = 3; i < n1; i + 2) {
if (n%i == 0) {
if (isprime((int)(n / i)) == 1) {
maxNumber = n / i;
break;
}
else if (isprime(i) == 1) {
maxNumber = i;
}
}
}
printf("%d\n", maxNumber);
}
}
}

View File

@ -0,0 +1,23 @@
/*
Problem:
The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N?
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
*/
#include <stdio.h>
int main() {
int n = 0;
scanf("%d", &n);
int prime = 1;
int i = 2;
while (i*i <= n) {
while (n%i == 0) {
prime = i;
n /= i;
}
i += 1;
}
if (n > 1)
prime = n;
printf("%d\n", prime);
}