-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab2_Vasko.cpp
75 lines (67 loc) · 1.8 KB
/
Lab2_Vasko.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
bool isPrime(int num) {
if (num <= 1)
return false;
for (int i = 2; i <= sqrt(num); ++i) {
if (num % i == 0)
return false;
}
return true;
}
int digitSum(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
bool isDivisible(int num) {
int firstTwo = num / 100;
int lastTwo = num % 100;
int sum = firstTwo + lastTwo;
return (num % sum == 0);
}
int firstBiggerPrime(int num) {
while (true) {
num++;
if (isPrime(num))
return num;
}
}
int main() {
// Task 1: Four-digit numbers divisible by the sum of their first two and last
// two digits
int divisibleCount = 0;
cout << "Task 1: Four-digit numbers divisible by the sum of their first two "
"and last two digits:\n";
for (int i = 1000; i < 1100; ++i) {
if (isDivisible(i)) {
cout << i << " ";
divisibleCount++;
}
}
cout << "\nTotal count: " << divisibleCount << "\n\n";
// Task 2: Difference between a given number and the first bigger prime number
int input = 10;
int nextPrime = firstBiggerPrime(input);
cout << "Task 2: First bigger prime number after " << input << ": "
<< nextPrime << endl;
cout << "Difference: " << nextPrime - input << "\n\n";
// Task 3: Prime numbers smaller than 1000 whose sum of digits is also prime
int primeSumCount = 0;
cout << "Task 3: Prime numbers smaller than 1000 whose sum of digits is also "
"prime:\n";
for (int i = 2; i < 1000; ++i) {
if (isPrime(i) && isPrime(digitSum(i))) {
cout << i << " ";
primeSumCount++;
}
}
cout << "\nTotal count: " << primeSumCount << endl;
return 0;
}