
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime Using C++
In this tutorial, we will be discussing a program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are coprime.
In this, we will be given an integer N, we have to print N integers less than 109 such that no two consecutive numbers are coprime but a pair of 3 consecutive integers must be coprime.
For example, let us say we have the integer 4. Then the numbers that follow both of the above-provided conditions are
6 15 35 14
Example
#include <bits/stdc++.h> using namespace std; #define limit 1000000000 #define MAX_PRIME 2000000 #define MAX 1000000 #define I_MAX 50000 map<int, int> map1; int b[MAX]; int p[MAX]; int j = 0; bool prime[MAX_PRIME + 1]; void sieve(int n){ memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++){ if (prime[p] == true){ for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int p = 2; p <= n; p++){ if (prime[p]) { b[j++] = p; } } } int gcdiv(int a, int b){ if (b == 0) return a; return gcdiv(b, a % b); } //printing the required series void print_elements(int n){ sieve(MAX_PRIME); int i, g, k, l, m, d; int ar[I_MAX + 2]; for (i = 0; i < j; i++){ if ((b[i] * b[i + 1]) > limit) break; p[i] = b[i]; map1[b[i] * b[i + 1]] = 1; } d = 550; bool flag = false; for (k = 2; (k < d - 1) && !flag; k++){ for (m = 2; (m < d) && !flag; m++){ for (l = m + k; l < d; l += k){ if (((b[l] * b[l + k]) < limit) && (l + k) < d && p[i - 1] != b[l + k] && p[i - 1] != b[l] && map1[b[l] * b[l + k]] != 1){ if (map1[p[i - 1] * b[l]] != 1){ p[i] = b[l]; map1[p[i - 1] * b[l]] = 1; i++; } } if (i >= I_MAX) { flag = true; break; } } } } for (i = 0; i < n; i++) ar[i] = p[i] * p[i + 1]; for (i = 0; i < n - 1; i++) cout << ar[i] << " "; g = gcdiv(ar[n - 1], ar[n - 2]); cout << g * 2 << endl; } int main(){ int n = 4; print_elements(n); return 0; }
Output
6 15 35 14
- Related Articles
- What are prime numbers and co-prime numbers?
- Checking for co-prime numbers - JavaScript
- Maximum subsequence sum such that no three are consecutive in C++ Program
- Maximum subsequence sum such that no three are consecutive
- Which of the following numbers are co-prime? $( a).\ 18$ and $35$ $( b).\ 15$ and $37$.
- What are consecutive numbers?
- What are prime numbers and coprime numbers?
- Write seven consecutive composite numbers less than 100 so that there is no prime number between them
- The sum of two consecutive numbers are 45 find the numbers.
- Check three consecutive numbers - JavaScript
- The numbers 13 and 31 are prime numbers. Both these numbers have same digits 1 and 3. Find such pairs of prime numbers upto 100 .
- Program to print prime numbers in a given range using C++ STL
- Java program to print prime numbers below 100
- What are Prime and Composite Numbers?
- C++ Program to Display Prime Numbers Between Two Intervals Using Functions

Advertisements