We are given two numbers A and B, we need to write a program to determine if A and B can be reached starting from (1, 1) following the given steps. Start from (1, 1) and at every step choose a random number K and multiply K to any one of the two numbers obtained in the previous step and K^{2} to the other number.

**Examples:**

Input : A = 3, B = 9 Output : yesExplanation: Starting from A = 1 and B = 1. We choose k=3 and multiply 3 with the first number to get A=3 and multiply k^{2}=9 to the second- number to get B=9. Input : A = 60, B = 450 Output : yesExplanation: Starting from A = 1 and B = 1, Step 1: multiply k=3 and k^{2}to get 3 and 9 Step 2: Multiply k=5 and k^{2}= 25 to get to 15 and 225 Step 3: Multiply k^{2}=4 and k=2 to get to A=60 and B=450

The idea to solve this problem is to observe closely that at each step we are multiplying k and k^{2} to the numbers. So if A and B can be reached, it will have **k^3 at every step as factors in A*B**. In simple words, if the number **A*B is a perfect cube and it divides A and B both**, only then the number can be reached starting from 1 and 1 by performing given steps.

Below is the implementation of above idea:

## C++

`// CPP program to determine if ` `// A and B can be reached starting ` `// from 1, 1 following the given steps. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to check is it is possible to reach ` `// A and B starting from 1 and 1 ` `bool` `possibleToReach(` `int` `a, ` `int` `b) ` `{ ` ` ` `// find the cuberoot of the number ` ` ` `int` `c = cbrt(a * b); ` ` ` ` ` `// divide the number by cuberoot ` ` ` `int` `re1 = a / c; ` ` ` `int` `re2 = b / c; ` ` ` ` ` `// if it is a perfect cuberoot and divides a and b ` ` ` `if` `((re1 * re1 * re2 == a) && (re2 * re2 * re1 == b)) ` ` ` `return` `true` `; ` ` ` `else` ` ` `return` `false` `; ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `A = 60, B = 450; ` ` ` ` ` `if` `(possibleToReach(A, B)) ` ` ` `cout << ` `"yes"` `; ` ` ` `else` ` ` `cout << ` `"no"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to determine if ` `// A and B can be reached starting ` `// from 1, 1 following the given ` `// steps. ` `class` `GFG { ` ` ` ` ` `// function to check is it is ` ` ` `// possible to reach A and B ` ` ` `// starting from 1 and 1 ` ` ` `static` `boolean` `possibleToReach(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` ` ` `// find the cuberoot of the number ` ` ` `int` `c = (` `int` `)Math.cbrt(a * b); ` ` ` ` ` `// divide the number by cuberoot ` ` ` `int` `re1 = a / c; ` ` ` `int` `re2 = b / c; ` ` ` ` ` `// if it is a perfect cuberoot and ` ` ` `// divides a and b ` ` ` `if` `((re1 * re1 * re2 == a) && ` ` ` `(re2 * re2 * re1 == b)) ` ` ` `return` `true` `; ` ` ` `else` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `A = ` `60` `, B = ` `450` `; ` ` ` ` ` `if` `(possibleToReach(A, B)) ` ` ` `System.out.println(` `"yes"` `); ` ` ` `else` ` ` `System.out.println(` `"no"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to determine if ` `# A and B can be reached starting ` `# from 1, 1 following the given steps. ` `import` `numpy as np ` ` ` `# function to check is it is possible to ` `# reach A and B starting from 1 and 1 ` `def` `possibleToReach(a, b): ` ` ` ` ` `# find the cuberoot of the number ` ` ` `c ` `=` `np.cbrt(a ` `*` `b) ` ` ` ` ` `# divide the number by cuberoot ` ` ` `re1 ` `=` `a ` `/` `/` `c ` ` ` `re2 ` `=` `b ` `/` `/` `c ` ` ` ` ` `# if it is a perfect cuberoot and ` ` ` `# divides a and b ` ` ` `if` `((re1 ` `*` `re1 ` `*` `re2 ` `=` `=` `a) ` `and` ` ` `(re2 ` `*` `re2 ` `*` `re1 ` `=` `=` `b)): ` ` ` `return` `True` ` ` `else` `: ` ` ` `return` `False` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `A ` `=` `60` ` ` `B ` `=` `450` ` ` ` ` `if` `(possibleToReach(A, B)): ` ` ` `print` `(` `"yes"` `) ` ` ` `else` `: ` ` ` `print` `(` `"no"` `) ` ` ` `# This code is contributed by ita_c ` |

*chevron_right*

*filter_none*

## C#

`// C# program to determine if ` `// A and B can be reached starting ` `// from 1, 1 following the given ` `// steps. ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` ` ` `// function to check is it is ` ` ` `// possible to reach A and B ` ` ` `// starting from 1 and 1 ` ` ` `public` `static` `bool` `possibleToReach(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` ` ` `// find the cuberoot of the number ` ` ` `int` `c = (` `int` `)Math.Pow(a * b, (` `double` `) 1 / 3); ` ` ` `// divide the number by cuberoot ` ` ` `int` `re1 = a / c; ` ` ` `int` `re2 = b / c; ` ` ` ` ` `// if it is a perfect cuberoot ` ` ` `// and divides a and b ` ` ` `if` `((re1 * re1 * re2 == a) && ` ` ` `(re2 * re2 * re1 == b)) ` ` ` `return` `true` `; ` ` ` `else` ` ` `return` `false` `; ` ` ` `} ` ` ` `// Driver Code ` ` ` `static` `public` `void` `Main (String []args) ` ` ` `{ ` ` ` ` ` `int` `A = 60, B = 450; ` ` ` ` ` `if` `(possibleToReach(A, B)) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ajit. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to determine if ` `// A and B can be reached starting ` `// from 1, 1 following the given steps. ` ` ` `// function to check is it is ` `// possible to reach A and B ` `// starting from 1 and 1 ` `function` `possibleToReach(` `$a` `, ` `$b` `) ` `{ ` ` ` ` ` `// find the cuberoot ` ` ` `// of the number ` ` ` `$c` `=(` `$a` `* ` `$b` `); ` ` ` ` ` `// divide the number ` ` ` `// by cuberoot ` ` ` `$re1` `= ` `$a` `/ ` `$c` `; ` ` ` `$re2` `= ` `$b` `/ ` `$c` `; ` ` ` ` ` `// if it is a perfect cuberoot ` ` ` `// and divides a and b ` ` ` `if` `((` `$re1` `* ` `$re1` `* ` `$re2` `== ` `$a` `) && ` ` ` `(` `$re2` `* ` `$re2` `* ` `$re1` `== ` `$b` `)) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `-1; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$A` `= 60; ` ` ` `$B` `= 450; ` ` ` `if` `(possibleToReach(` `$A` `, ` `$B` `)) ` ` ` `echo` `"yes"` `; ` ` ` `else` ` ` `echo` `"no"` `; ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

Output:

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Minimum step to reach one
- Find the number of ways to reach Kth step in stair case
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Check if elements of array can be made equal by multiplying given prime numbers
- Find the number of consecutive zero at the end after multiplying n numbers
- Maximize the sum of array by multiplying prefix of array with -1
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Check if N can be obtained from 1 by repetitively multiplying by 10 or 20
- Min operations to reduce N to 1 by multiplying by A or dividing by B
- Min operations to reduce N by multiplying by any number or taking square root
- Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1
- Maximum subsequence sum possible by multiplying each element by its index
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Count the pairs in an array such that the difference between them and their indices is equal
- Make A, B and C equal by adding total value N to them
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.