Python code:
end = 1000
nums = []
for i in range(1, end):
if i%3==0 or i%==:
nums.append(i)
total = sum(nums)
C++ code:
// problem1.cpp
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
int main(){
clock_t tSTart = clock();
int n = 10;
float A = 0;
for (int i = 1; i < 1000; i++) {
if ((i % 3 == 0) || (i % 5 == 0)) {
A += i;
}
}
cout << "A: " << A << '\n';
cout << "Calculation time: " << (double)(clock() - tSTart) / CLOCKS_PER_SEC << " [sec]\n";
}
The sum of multiples of 3 and 5 below 1000 is 233168
Python code:
fibonacci_list_even = []
max = 4000000
fib_num = 0
num1 = 0
num2 = 1
while fib_num < max:
fib_num = num1+num2
num1 = num2
num2 = fib_num
if fib_num%2==0:
fibonacci_list_even.append(fib_num)
total = sum(fibonacci_list_even)
C++ code:
// problem2.cpp
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
int main(){
clock_t tSTart = clock();
int fib_num = 0;
int fib1 = 0;
int fib2 = 1;
int fib_sum = 0;
int max = 4000000;
while (fib_num < max) {
fib_num = fib1 + fib2;
fib1 = fib2;
fib2 = fib_num;
if (fib_num % 2 == 0) {
fib_sum += fib_num;
}
}
cout << "The sum of even Fibonacci numbers below 4 miliion: " << fib_sum << '\n';
cout << "Calculation time: " << (double)(clock() - tSTart) / CLOCKS_PER_SEC << " [sec]\n";
}
The sum of even Fibonacci numbers under 4 million is 4613732
C++ code:
// problem3.cpp
#include <iostream>
#include <math.h>
#include <time.h>
#include <list>
using namespace std;
bool primeCheck(int n) {
bool isPrime = true;
// 0 and 1 are not prime numbers
if (n == 0 || n == 1) {
isPrime = false;
}
else {
for (int i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
return isPrime;
}
int main() {
clock_t tSTart = clock();
int n = 600851475143;
cout << "The Prime Divisors of " << n << " are:\n";
for (int i=1; i<floor(pow(n,0.5)+1); i++) {
if (n % i == 0) {
int div = n / i;
if (primeCheck(div))
cout << div << "\n";
if (primeCheck(i))
cout << i << "\n";
}
}
cout << "Calculation time: " << (double)(clock() - tSTart) / CLOCKS_PER_SEC << " [sec]\n";
}
The answer is 6857
*please note: this code didn't produce the correct answer when I ran it on my laptop because my version of visual studio uses a 32 bit version of windows, leading to overflow.
n = 1000
num = 2**n
num_str = str(num)
sum_num = 0
for i in range(len(num_str)):
sum_num += int(num_str[i])
print(sum_num)
the sum of the digits of the number 2^1000 is: 1366
def fact(x):
if x==0:
ans = 1
else:
ans = x
while x != 1:
x -= 1
ans *= x
return ans
num = fact(100)
num_str = str(num)
sum_num = 0
for i in range(len(num_str)):
sum_num += int(num_str[i])
print(sum_num)
The answer is 648
F = [1,1]
counter = 2
while True:
x = F[len(F)-1]+F[len(F)-2]
F.append(x)
x_str = str(x)
counter += 1
if len(x_str) > 999:
print(counter)
break
The answer is 4782
a_l = [2]
b_l = [2]
max_num = 100
for x in range(3, max_num+1):
a_l.append(x)
b_l.append(x)
exp_list = []
for a in a_l:
for b in b_l:
exp_list.append(a**b)
exp_list.sort()
print(len(set(exp_list)))
The answer is 9183
def is_palindrome(x):
return x == x[::-1]
num_dp = 0
for num in range(10**6):
num_str = str(num)
if is_palindrome(num_str) == True:
bin_num2 = din(num)
binary2 = []
for i in range(2, len(bin_num2)):
binary2.append(int(bin_num2[i]))
if is_palindrome(binary2) == True:
num_dp += num
The sum of double palindromes below 1 million is 872187
def same_nums(num1, num2):
num1_str = str(num1)
num2_str = str(num2)
if len(num1_str) == len(num2_str):
for i in range(len(num1_str)):
if num1_str[i] not in num2_str or num2_str[i] not in num1_str:
return 0
return num1
else:
return 0
num1 = 1
check = False
while check is False:
num2 = 2*num1
num3 = 3*num1
num4 = 4*num1
num5 = 5*num1
num6v = 6*num1
if same_nums(num1, num2) != 0 and same_nums(num1, num3) != 0 and same_nums(num1, num4) != 0 and same_nums(num1, num5) != 0 and same_nums(num1, num6) != 0:
print(num1)
check = True
num1 += 1
The answer is 142857
num = 10**6
n_div_phi_max = 0
n_max = 0
for n in range(2, num+1):
relative_prime = []
for i in range(1, n):
coprime_check = gcd(n, i)
if coprime_check ==1:
relative_prime.append(i)
phi = len(relative_prime)
n_div_phi = n/phi
if n_div_phi > n_div_phi_max:
n_div_phi_max = n_div_phi
n_max = n
The answer is 510510
prime = (28433*(2**7830457))+1
The answer is 8739992577
def is_bouncy(num):
num = str(num)
num_list = []
nums = []
for i in num:
num_list.append(int(i))
nums.append(int(i))
nums.sort()
if nums == num_list:
return 0
nums.sort(reverse=True)
if nums == num_list:
return 0
return 1
def find(pd):
num = 100
bouncy = 0
while True:
x = is_bouncy(num)
if x == 1:
bouncy += 1
if bouncy => pd:
return num
num += 1
percent_bouncy = 0.99
number = find(percent_bouncy)
The answer is 1587000