0% found this document useful (0 votes)
52 views

Finding The Lucas Lehmer Test Using Object

This C++ program uses object-oriented coding to find Lucas-Lehmer series terms and Mersenne prime numbers. It defines functions to calculate terms of the Lucas-Lehmer series up to a given value n and to generate Mersenne prime numbers less than or equal to a given value n. These functions are called in a main driver program to output the first 5 terms of the Lucas-Lehmer series and all Mersenne primes less than or equal to 31.

Uploaded by

shubham
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Finding The Lucas Lehmer Test Using Object

This C++ program uses object-oriented coding to find Lucas-Lehmer series terms and Mersenne prime numbers. It defines functions to calculate terms of the Lucas-Lehmer series up to a given value n and to generate Mersenne prime numbers less than or equal to a given value n. These functions are called in a main driver program to output the first 5 terms of the Lucas-Lehmer series and all Mersenne primes less than or equal to 31.

Uploaded by

shubham
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Finding the Lucas lehmer test using object-oriented coding

// C++ program to find out Lucas-Lehmer series.

#include <iostream>

#include <vector>

using namespace std;

// Function to find out first n terms

// (considering 4 as 0th term) of

// Lucas-Lehmer series.

void Lucas Ehmer(int n) {

// the 0th term of the series is 4.

unsigned long long current_val = 4;

// create an array to store the terms.

vector<unsigned long long> series;

// compute each term and add it to the array.

series.push_back(current_val);

for (int i = 0; i < n; i++) {

current_val = current_val * current_val - 2;

series.push_back(current_val);

// print out the terms one by one.

for (int i = 0; i <= n; i++)

cout << "Term " << i << ": "

<< series[i] << endl;


}

// Driver program

int main() {

int n = 5;

LucasLehmer(n);

return 0;

Finding the Mersenne number using object-oriented coding.

// Program to generate Mersenne primes

#include<bits/stdc++.h>
using namespace std;

// Generate all prime numbers less than n.

void SieveOfEratosthenes(int n, bool prime[])

// Initialize all entries of Boolean array

// as true. A value in prime[i] will finally

// be false if i is Not a prime, else true

// bool prime[n+1];

for (int i=0; i<=n; i++)

prime[i] = true;

for (int p=2; p*p<=n; p++)

// If prime[p] is not changed, then it

// is a prime

if (prime[p] == true)

// Update all multiples of p

for (int i=p*2; i<=n; i += p)

prime[i] = false;

// Function to generate Mersenne primes less

// than or equal to n

void Mersenne Primes(int n)

{
// Create a Boolean array "prime[0.n]"

bool prime[n+1];

// Generating primes using Sieve

SieveOfEratosthenes(n , prime);

// Generate all numbers of the form 2^k - 1

// and smaller than or equal to n.

for (int k=2; ((1<<k)-1) <= n; k++)

long long num = (1<<k) - 1;

// Checking whether number is prime and is

// one less than the power of 2

if (prime[num])

cout << num << " ";

// Driven program

int main()

int n = 31;

cout << "Mersenne prime numbers smaller "

<< "than or equal to " << n << endl;

Mersenne Primes(n);

return 0;

You might also like