Dados dos enteros A y N , nuestra tarea es encontrar el N-ésimo número natural que no sea divisible por A.
Ejemplos:
Entrada: A = 4, N = 12
Salida: 15
Explicación:
La serie a partir de 1 excluyendo los múltiplos de A sería 1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17 y el término 12 que no es divisible por 4 es 15.Entrada: A = 3, N = 20
Salida: 29
Explicación:
La serie que comienza en 1 excluyendo los múltiplos de A sería 1, 2, 4, 5, 7, 8, 10, 11 y así sucesivamente y el N-ésimo número que es no divisible por 3 es 29.
Acercarse:
Para resolver el problema mencionado anteriormente, debemos observar que hay un espacio después de cada número entero (A – 1) a medida que se omiten los múltiplos de A. Para encontrar que el número N se encuentra en qué conjunto, dividimos N por (A – 1) y lo almacenamos en una variable, digamos cociente . Ahora, multiplicando A con esa variable y sumando el resto obtendremos la respuesta resultante.
If A = 4, N = 7: quotient = 7 / 3 = 2 remainder = 7 % 3 = 1 So the answer is (A * quotient) + remainder = 4 * 2 + 1 = 9
Pero si el resto es 0 significa que es el último elemento del conjunto. Entendámoslo con un ejemplo.
If A = 4, N = 6: quotient = 6 / 3 = 2 remainder = 6 % 3 = 0 So the answer is (A * quotient) - 1 = 4 * 2 - 1 = 7
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ code to Find the Nth number which // is not divisible by A from the series #include<bits/stdc++.h> using namespace std; void findNum(int n, int k) { // Find the quotient and the remainder // when k is divided by n-1 int q = k / (n - 1); int r = k % (n - 1); int a; // If the remainder is not 0 // multiply n by q and subtract 1 // if remainder is 0 then // multiply n by q // and add the remainder if(r != 0) a = (n * q) + r; else a = (n * q) - 1; // Print the answer cout << a; } // Driver code int main() { int A = 4, N = 6; findNum(A, N); return 0; } // This code is contributed by PratikBasu
Java
// Java code to Find the Nth number which // is not divisible by A from the series class GFG { static void findNum(int n, int k) { // Find the quotient and the remainder // when k is divided by n-1 int q = k / (n - 1); int r = k % (n - 1); int a = 0; // If the remainder is not 0 // multiply n by q and subtract 1 // if remainder is 0 then // multiply n by q // and add the remainder if (r != 0) a = (n * q) + r; else a = (n * q) - 1; // Print the answer System.out.println(a); } // Driver code public static void main(String[] args) { int A = 4; int N = 6; findNum(A, N); } } // This code is contributed by 29AjayKumar
Python3
# Python3 code to Find the Nth number which # is not divisible by A from the series def findNum(n, k): # Find the quotient and the remainder # when k is divided by n-1 q = k//(n-1) r = k % (n-1) # if the remainder is not 0 # multiply n by q and subtract 1 # if remainder is 0 then # multiply n by q # and add the remainder if(r != 0): a = (n * q)+r else: a = (n * q)-1 # print the answer print(a) # driver code A = 4 N = 6 findNum(A, N)
C#
// C# code to find the Nth number which // is not divisible by A from the series using System; class GFG{ static void findNum(int n, int k) { // Find the quotient and the remainder // when k is divided by n-1 int q = k / (n - 1); int r = k % (n - 1); int a = 0; // If the remainder is not 0 // multiply n by q and subtract 1 // if remainder is 0 then // multiply n by q // and add the remainder if (r != 0) a = (n * q) + r; else a = (n * q) - 1; // Print the answer Console.WriteLine(a); } // Driver code public static void Main(String[] args) { int A = 4; int N = 6; findNum(A, N); } } // This code is contributed by amal kumar choubey
Javascript
<script> // Javascript code to Find the Nth number which // is not divisible by A from the series function findNum(n, k) { // Find the quotient and the remainder // when k is divided by n-1 let q = parseInt(k / (n - 1)); let r = k % (n - 1); let a; // If the remainder is not 0 // multiply n by q and subtract 1 // if remainder is 0 then // multiply n by q // and add the remainder if (r != 0) a = (n * q) + r; else a = (n * q) - 1; // Print the answer document.write(a); } // Driver code let A = 4, N = 6; findNum(A, N); // This code is contributed by rishavmahato348 </script>
7
Publicación traducida automáticamente
Artículo escrito por dipesh99kumar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA