Trending March 2024 # C++ Program To Check If Given Numbers Are Coprime Or Not # Suggested April 2024 # Top 8 Popular

You are reading the article C++ Program To Check If Given Numbers Are Coprime Or Not updated in March 2024 on the website Moimoishop.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested April 2024 C++ Program To Check If Given Numbers Are Coprime Or Not

Suppose, we have n integers in an array nums. We have to find out if the numbers in the array are pairwise coprime, setwise coprime, or not coprime.

Two numbers nums[i] and nums[j] are said to be pairwise coprime if gcd(nums[i], nums[j]) = 1. This should hold for every number pair in the array and i < j.

The numbers are said to be setwise coprime if gcd(nums[i]) = 1.

If they are neither, we say that they are not coprime.

So, if the input is like n = 4, nums = {7, 11, 13, 17}, then the output will be the numbers are pairwise coprime.

If we check every number pair in the array, the gcd of them will always be 1.

To solve this, we will follow these steps −

Define an array fac of size: 100 initialized with 0s. Define an array checkPrime of size: 100 initialized with 0s. gcdVal := 0 for initialize i := 0, when i < n, update (increase i by 1), do:     gcdVal := gcd of (nums[i], gcdVal)     (increase fac[nums[i]] by 1) if gcdVal is same as 1, then:    pw := true    for initialize k := 2, when k < 100, update (increase k by 1), do:       if checkPrime[k] is non-zero, then:          Ignore following part, skip to the next iteration     c := 0 for initialize j := k, when j < 100, update j := j + k, do: c := c + fac[j] checkPrime[j] := true pw := pw AND true if c <= 1 if pw is non-zero, then: print("The numbers are pairwise coprime") Otherwise print("The numbers are setwise coprime") Otherwise using namespace std; void solve(int n, int nums[]){    int fac[100] = {0};    bool checkPrime[100] = {0};    int gcdVal = 0;    for(int i = 0; i < n ; i++) {       gcdVal = __gcd(nums[i], gcdVal);       ++fac[nums[i]];    }    if(gcdVal == 1) {       bool pw = true;       for(int k = 2; k < 100; ++k) {          if(checkPrime[k])         continue; int c = 0; for(int j = k; j < 100; j += k) { c += fac[j]; checkPrime[j] = true; } pw = pw && c <= 1; } if(pw) cout<< "The numbers are pairwise coprime"; else cout<< "The numbers are setwise coprime"; } else cout << "The numbers are not coprime"; } int main() { int n = 4, nums[] = {7, 11, 13, 17}; solve(n, nums); return 0; } Input 4, {7, 11, 13, 17}; Output The numbers are pairwise coprime

You're reading C++ Program To Check If Given Numbers Are Coprime Or Not

C++ Program To Find The Product Of Two Numbers Using Recursion

Recursion is a technique where we call a function from the same function itself. There must be some base or terminating condition to end the recursive call. Recursive procedures are very much helpful to perform complex iterative solution with fewer codes and easier solution approach through sub-operation finding.

In this article we shall discuss a recursive approach to perform product (multiplication) between two numbers in C++. Initially we will understand the basic principle, the syntax for recursive function call, the algorithm and the source code.

Multiplication using Recursion

In high-level languages, there is the multiplication operators available to directly perform multiplication. However we know that the multiplication is actually a repetitive addition. So the result of A * B is repetitive addition of A, B number of times, or we can say repetitive addition of B, A number of times. Whenever there is a repetition, we can do this using recursion. Let us see the recursive function definition syntax first.

Syntax

if ( base condition ) { terminate recursive call } recursive function call: function_name ( updated parameter list ) }

define a function multiply() which takes two numbers A and B

if A < B, then

return multiply( B, A )

otherwise when B is not 0, then

return A + multiply( A, B – 1 )

otherwise

return 0

end if

end of function definition

Read two inputs A and B

res = multiply( A, B )

display res

Example

using

namespace

std

;

int

multiply

(

int

A

,

int

B

)

{

if

(

A

<

B

)

{

return

multiply

(

B

,

A

)

;

}

else

if

(

B

!=

0

)

{

return

A

+

multiply

(

A

,

B

1

)

;

}

else

{

return

0

;

}

}

int

main

(

)

{

cout

<<

“Multiplication of 5, 7 is: “

<<

multiply

(

5

,

7

)

<<

endl

;

cout

<<

“Multiplication of 8, 0 is: “

<<

multiply

(

8

,

0

)

<<

endl

;

cout

<<

“Multiplication of 25, 3 is: “

<<

multiply

(

25

,

3

)

<<

endl

;

cout

<<

“Multiplication of 9, 1 is: “

<<

multiply

(

9

,

1

)

<<

endl

;

}

Output Multiplication of 5, 7 is: 35 Multiplication of 8, 0 is: 0 Multiplication of 25, 3 is: 75 Multiplication of 9, 1 is: 9

See, in this program, the function is taking parameters A and B both are two integer numbers. Now after each step it reduces the second parameter B by 1 and adding A with A itself. Like this the function is performing multiplication procedure.

Conclusion

Recursion is a process to call the same function from the function itself. While calling a function recursively, we update or change the parameter set a little so that the same effect is not coming again and again, then it divides the problem into smaller subproblems and solve the problem by solving these smaller problems in a bottom-up manner. Almost anything which can be implemented using loops, can also be implemented using recursion. In this article we have seen simple process to multiply two integers using recursion. The integers are added multiple times to get the final multiplication result.

C++ Program To Implement The Solovay

Solovay-Strassen Primality Test is used to test a number whether it is a composite or possibly prime number.

Algorithms Begin    Declare a function modulo to the long datatype to perform binary calculation.       Declare m_base, m_exp, m_mod of long datatype and pass them as a parameter.       Declare two variables a, b of long datatype.          Initialize a = 1, b = m_base.          if (m_exp % 2 == 1) then             a = (a * b) % m_mod.          b = (b * b) % m_mod.          m_exp = m_exp / 2.       Return a % m_mod. End Begin    Declare a function Jecobian of the int datatype to calculate Jacobian symbol of a given number.    Declare CJ_a, CJ_n of the long datatype and pass them as a parameter.    if (!CJ_a) then       return 0.    Declare answer of the integer datatype.       Initialize answer = 1.    if (CJ_a < 0) then       CJ_a = -CJ_a.       if (CJ_n % 4 == 3) then          answer = -answer.    if (CJ_a == 1) then       return answer.    while (CJ_a) do       if (CJ_a < 0) then          CJ_a = -CJ_a.          if (CJ_n % 4 == 3) then          answer = -answer.    while (CJ_a % 2 == 0) do       CJ_a = CJ_a / 2;          answer = -answer.    swap(CJ_a, CJ_n)    if (CJ_a % 4 == 3 && CJ_n % 4 == 3) then       answer = -answer.          CJ_a = CJ_a % CJ_n;       CJ_a = CJ_a - CJ_n.    if (CJ_n == 1) then       return answer. End Begin    Declare a function Solovoystrassen to the Boolean datatype to perform the Solovay-Strassen Primality Test.       Declare SS_p to the long datatype and pass as a parameter.       Declare itr to the integer datatype and pass them as a parameter.       if (SS_p < 2) then          return false.       if (SS_p != 2 && SS_p % 2 == 0) then          return false.       for (int i = 0; i < itr; i++)          long a = rand() % (SS_p - 1) + 1;          long jacob = (SS_p + Jacobian(a, SS_p)) % SS_p;          long mod = modulo(a, (SS_p - 1) / 2, SS_p);          return false       return true. End Begin    Declare iter of the integer datatype.       Initialize iter = 50.    Declare num1, num2 of the long datatype.    Print “Enter the first number:”    Input the value of num1.    if (Solovoystrassen(num1, iter)) then       print the value of num1 and ”is a prime number”.    Else       print the value of num1 and ”is a composite number”.    Print “Enter another number:”       Input the value of num2.    if (Solovoystrassen(num2, iter)) then       print the value of num2 and ”is a prime number”.    Else       print the value of num2 and ”is a composite number”. using namespace std; long modulo(long m_base, long m_exp, long m_mod) function to perform binary calculation {    long a = 1;    long b = m_base;       if (m_exp % 2 == 1)          a = (a * b) % m_mod;       b = (b * b) % m_mod;       m_exp = m_exp / 2;    }    return a % m_mod; } int Jacobian(long CJ_a, long CJ_n) {    if (!CJ_a)       return 0;// (0/n) = 0    int answer = 1;    if (CJ_a < 0) {       CJ_a = -CJ_a;       if (CJ_n % 4 == 3)          answer = -answer;    }    if (CJ_a == 1)       return answer;    while (CJ_a)  {       if (CJ_a < 0) {          CJ_a = -CJ_a;          if (CJ_n % 4 == 3)             answer = -answer;       }       while (CJ_a % 2 == 0) {          CJ_a = CJ_a / 2;             answer = -answer;       }       swap(CJ_a, CJ_n);       if (CJ_a % 4 == 3 && CJ_n % 4 == 3)          answer = -answer;       CJ_a = CJ_a % CJ_n;             CJ_a = CJ_a - CJ_n;    }    if (CJ_n == 1)       return answer;    return 0; } bool Solovoystrassen(long SS_p, int itr) {    if (SS_p < 2)       return false;    if (SS_p != 2 && SS_p % 2 == 0)       return false;    for (int i = 0; i < itr; i++) {             long a = rand() % (SS_p - 1) + 1;       long jacob = (SS_p + Jacobian(a, SS_p)) % SS_p;       long mod = modulo(a, (SS_p - 1) / 2, SS_p);          return false;    }    return true; } int main() {    int iter = 50;    long num1;    long num2;    cout<< "Enter the first number: ";    cout<<endl;    if (Solovoystrassen(num1, iter))       cout<<num1<<" is a prime numbern"<<endl;    else       cout<<num1<<" is a composite numbern"<<endl;       cout<<"Enter another number: ";    cout<<endl;    if (Solovoystrassen(num2, iter))       cout<<num2<<" is a prime numbern"<<endl;    else       cout<<num2<<" is a composite numbern"<<endl;    return 0; } Output Enter the first number: 24 24 is a composite number Enter another number: 23 23 is a prime number

Python Program To Remove Numbers With Repeating Digits

In this article, we will learn how to remove numbers with repeating digits in python.

Methods Used

The following are the various methods to accomplish this task −

Using list comprehension and set() function

Using re module

Using the Counter() function

Example

Assume we have taken an input list containing numbers. We will now remove all the numbers from the list containing repeating digits and return the resultant list using the above-mentioned methods.

Input inputList = [3352, 8135, 661, 7893, 99] Output [8135, 7893]

In the above input list, in the 1st element 3352, 3 is repeated twice. Hence it is removed. But the 8135 has no repeating digits hence it is retained. Similarly, 661, and 99 are removed as they contain repeating characters.

Therefore the output list contains only 8135, 7893 elements.

Method 1: Using list comprehension and set() function

len() − The number of items in an object is returned by the len() method. The len() function returns the number of characters in a string when the object is a string.

set() function(creates a set object. A set list will appear in random order because the items are not ordered. It removes all the duplicates)

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task –.

Create a variable to store to the input list and print the given list.

Traverse through numbers(elements) of the given list using list comprehension.

Convert each number to a string using the str() function(returns the string format of the object i.e converts it into a string).

Convert this number string to set using the set() function that removes the duplicate digits of the number.

Check if the length of the string(number) is equal to the length of the above set.

Print the resultant list after removing elements with repeating digits from the input list.

Example

The following program returns the resultant list after removing the numbers containing repeating digits from the input list using the list comprehension and set() function –

# input list inputList = [3352, 8135, 661, 7893, 99] # printing the input list print("Input list: ", inputList) # Traversing through the numbers of the list using list comprehension # Convering numbers to string and finding a set of strings (removes duplicates) # Checking if the length of the set is equal to the number of digits resultList = [i for i in inputList if len(set(str(i))) == len(str(i))] # printing resultant list after removing elements with repeating digits print("Resultant list after removing elements with repeating digits:") print(resultList) Output

On executing, the above program will generate the following output –

Input list: [3352, 8135, 661, 7893, 99] Resultant list after removing elements with repeating digits: [8135, 7893]

Time Complexity − O(n)

Auxiliary Space − O(n)

Method 2: Using re module Syntax re.search() function

Searches the entire target string for occurrences of the regex pattern and returns the appropriate Match Object instance where the match was found. It returns only the first match to the pattern from the target string.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

Use the import keyword to import the re module (regex).

Use the compile() function of re module by giving the regex pattern to remove elements with repeating digits.

Traverse through the elements of the list and check if the list elements match the above regex pattern using the search() function.

Example # importing re module import re # input list inputList = [3352, 8135, 661, 7893, 99] # printing the input list print("Input list: ", inputList) # regex pattern to remove elements with repeating digits # Checking list elements for the above regex pattern matches resultList = [i for i in inputList if not regexPattern.search(str(i))] # printing resultant list after removing elements with repeating digits print("Resultant list after removing elements with repeating digits:") print(resultList) Output

On executing, the above program will generate the following output –

Input list: [3352, 8135, 661, 7893, 99] Resultant list after removing elements with repeating digits: [8135, 7893]

Time Complexity − O(n)

Auxiliary Space − O(n)

Method 3: Using the Counter() function

Counter() function(a sub-class that counts the hashable objects. It implicitly creates a hash table of an iterable when called/invoked).

Example

The following program returns the resultant list after removing the numbers containing repeating digits from the input list using the Counter() function –

# importing a Counter function from the collections module from collections import Counter # input list inputList = [3352, 8135, 661, 7893, 99] # printing the input list print("Input list: ", inputList) # Counter gives the unique keys(digits) of the number resultList = [i for i in inputList if len(Counter(str(i))) == len(str(i))] # printing resultant list after removing elements with repeating digits print("Resultant list after removing elements with repeating digits:") print(resultList) Output

On executing, the above program will generate the following output –

Input list: [3352, 8135, 661, 7893, 99] Resultant list after removing elements with repeating digits: [8135, 7893]

Time Complexity − O(n)

Auxiliary Space − O(n)

The frequency of each digit of the number is given by the Counter() method here. It thus has the unique keys (digits) for the given number. The length of the given number was then compared to the number of unique digits returned by the counter

Conclusion

In this article, we learned 3 different methods for removing integers from a list that have repeated digits. Additionally, we learned how to find pattern matches in iterables using the regex module.

Golang Program To Get The Magnitude Of The Given Number

In this article we will discuss about how to get the magnitude of a number in Go language.

Magnitude of a quantity is defined as its numeric value. Magnitude of a number is always positive. In this article we will discuss about different methods by which we can obtain the magnitude of any number.

Syntax func Abs(number float64) float64

Abs() is a method defined in math library. This method accepts a 64 bit float number as an argument and return the absolute value of it excluding the sign.

The source code to the above stated problem is compiled and executed below.

Example 1

The simplest way to obtain the magnitude of any integer in go language is by using a library function Abs(). This function returns the absolute value of an integer.

Algorithm

Step 1 − Import the package fmt.

Step 2 − start the main function().

Step 3 − Initialize a variable of data type int and store value in it.

Step 4 − call the abs() function defined in math package and store the result.

Step 5 − print the results on the screen.

Example

package

main

import

(

“fmt”

“math”

)

func

main

(

)

{

var

number float64

=

3.8

result

:

=

math

.

Abs

(

number

)

fmt

.

Println

(

“Magnitude of:”

,

number

,

“is “

,

result

)

}

Output

Magnitude of: -3.8 is 3.8

Description of the Code

First, we import the package fmt that allows us to print anything and math package to use Abs() method.

Then we call the main() function.

Now we need to get the number whose magnitude we wish to calculate.

Pass this number in Abs() method defined in math package and store the result in a separate variable.

Abs() is a method defined in math package that takes a float number as an argument and returns the numeric value of the number (excluding the sign).

Print the results on the screen using fmt.Println() function.

Example 2

Now there is one more way by which we can obtain the magnitude of a number. This method includes creating our own logic to implement the result.

Algorithm

Step 1 − Import the package fmt.

Step 2 − start the main function().

Step 3 − Initialize a variable and store values in it.

Step 4 − Implement the logic and store the result.

Step 5 − print the results on the screen.

Example

The source code for that is compiled and executed below.

import

(

“fmt”

“math”

)

func magnitude

(

number float64

)

float64

{

var

temp float64

temp

=

math

.

Pow

(

number

,

2

)

result

:

=

math

.

Pow

(

temp

,

0.5

)

return

result

}

func

main

(

)

{

var

number float64

number

=

8.9

result

:

=

magnitude

(

number

)

fmt

.

Println

(

“Magnitude of:”

,

number

,

“is “

,

result

)

}

Output Magnitude of: -3.8 is 3.8 Description of the Code

First, we import the package fmt that allows us to print anything and math package to use Pow() method defined in the math package.

Then we create and define the magnitude() function which will contain our logic to find the magnitude of the number.

Then we call the main() function.

Now we need to get the number whose magnitude we wish to calculate.

Call the magnitude function by passing the number in it as an argument.

To find the magnitude we are using the standard mathematical definition of magnitude which says magnitude of a number is the root of its square.

To find the square of a number in golang we are using math.Pow() method. This method takes two arguments one is the number whose power we need to raise and second is the number of times we want to raise the power for example: to find the square of a number using this function the code will be math.Pow(number, 2).

Once the square is calculated we need to store the result to a variable which in our case is temp and use the similar approach to find the root of the function. To find the root of a number using math.Pow() method in golang the code will be math.Pow(number, 0.5).

Once we have achieved the magnitude we need to return this value back.

We have then stored the final result in the result variable.

Then we print the results on the screen using fmt.Println() function.

Conclusion

We have successfully compiled and executed the Go language program that will get us the magnitude of any number along with examples using both library and user defined functions.

6 Ways To Check If Usb Cable Supports Fast Charging Or High Speed Transfer

1. Firstly, a fast-charging or High-Speed Data Transfer USB cable is comparatively thicker than normal cables comprising a dense network of wires within them.

2. Check for any existing text engraved on the cable connector. If you find a ‘SS’ mark on the connector head, it stands for ‘super-speed’ which verifies that the cable supports fast charging and high data transfer speeds.

3. Some USB cables also have a specific number engraved on the connector end such as 5, 10, 20, etc. to state their transfer speeds (in Gbps).

You can also determine whether your USB cable supports fast-charging or high-speed data transfer by looking at its connector end. Generally, there are six different types of connector ends that you might come across:

Micro-USB: It is mostly found in older smartphones and external drives offering a max data transfer speed of up to 480 Mbps.

Mini-USB: It is commonly used by portable electronic devices, like cameras and MP3 players offering the same speed as Micro-USB.

Lightning: Found on Apple products, and offers USB 2.0 transfer speeds with a maximum speed of up to 480 Mbps.

USB Type-C: Widely used on new generation smartphones and laptops with a max data transfer speed of up to 10 Gbps.

USB Type A and B: These are the most common connectors that you will find on a USB cable offering a maximum data transfer speed of up to 12 Mbps.

The easiest method to check if a USB cable supports fast charging is to connect it to your smartphone and wait for a few minutes to track the charging speed. You can either trace it manually or install free third-party Apps such as Ampere, or Accubattery to get it done. Here’s how:

1. Install the Ampere app (Android, iPhone) on your smartphone.

3. Wait for a few seconds, to see charging details such as Input ampere, voltage, and battery health. You can multiply the input ampere value (mA) with the charging voltage (V) to obtain the power output (W) your phone is charging at.

4. If the calculated output value is greater than 10 watts, your phone is charging at a fast rate.

If you don’t wish to install any third-party app to check the fast-charging compatibility of a USB cable, you can buy a customized fast-charging cable from Amazon with an in-built display. Check out the best buy links USB-C and Lightning Cable on Amazon.

Now that you’ve learned how to check a USB cable for fast charging and high-speed data transfer speeds, you must verify if your phone supports fast charging as well. Read our detailed explainer to check Fast Charging support on phone to learn about the same.

Q: How to identify a Fast Charging Adapter?

A: You can check the Ampere (A) and volt (V) values on the adapter to check its power output (W). Further, refer to the steps mentioned above to verify if your cable supports fast charging with it.

A: The Ampere or Accubattery app can help you measure the charging current and speed in real-time.

Q: What makes the phone charge faster, the USB cable or Plug?

A: Both, incompatibilities in both situations will not charge your phone faster.

You should also check out:

You can also follow us for instant tech news at Google News or for tips and tricks, smartphones & gadgets reviews, join GadgetsToUse Telegram Group, or for the latest review videos subscribe GadgetsToUse Youtube Channel.

Update the detailed information about C++ Program To Check If Given Numbers Are Coprime Or Not on the Moimoishop.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!