Sunday, October 6, 2024
Google search engine
HomeData Modelling & AIFind the value of the function Y = (X^6 + X^2 +...

Find the value of the function Y = (X^6 + X^2 + 9894845) % 971

Given a function, Y = (X^6 + X^2 + 9894845) % 971 for a given value. The task is to find the value of the function.

Examples: 

Input: x = 5
Output: 469

Input: x = 654654
Output: 450

Explanation: 

Y = (X^6 + X^2 + 9894845) % 971. 
If we break down the equation we get Y = (X^6)%971 + (X^2)%971 +(9894845)%971 
and we can reduce the equation to Y=(X^6)%971 + (X^2)%971 + 355.

Below is the required implementation: 
 

C++




// CPP implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// computing (a^b)%c
long long int modpow(long long int base, long long int exp, long long int modulus) {
base %= modulus;
long long int result = 1;
while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
}
return result;
}
 
// Driver code
int main(){
    long long int n = 654654, mod = 971;
    cout<<(((modpow(n, 6, mod)+modpow(n, 2, mod))% mod + 355)% mod);
 
    return 0;
}
// This code is contributed by Sanjit_Prasad


Java




// Java implementation of above approach
 
class GFG
{
 
// computing (a^b)%c
static long modpow(long base, long exp, long modulus)
{
    base %= modulus;
    long result = 1;
    while (exp > 0) {
        if ((exp & 1)>0) result = (result * base) % modulus;
            base = (base * base) % modulus;
            exp >>= 1;
    }
    return result;
}
 
    public static void main(String[] args)
    {
        long n = 654654;
        long mod = 971;
        System.out.println(((modpow(n, 6, mod)+modpow(n, 2, mod))% mod + 355)% mod);
    }
}
// This code is contributed by mits;


Python3




# Python implementation of above approach
 
n = 654654
mod = 971
print(((pow(n, 6, mod)+pow(n, 2, mod))% mod + 355)% mod)


C#




// C# implementation of above approach
using System;
class GFG
{
 
// computing (a^b)%c
static long modpow(long base1, long exp, long modulus)
{
    base1 %= modulus;
    long result = 1;
    while (exp > 0) {
        if ((exp & 1)>0) result = (result * base1) % modulus;
            base1 = (base1 * base1) % modulus;
            exp >>= 1;
    }
    return result;
}
 
    public static void Main()
    {
        long n = 654654;
        long mod = 971;
        Console.WriteLine(((modpow(n, 6, mod)+modpow(n, 2, mod))% mod + 355)% mod);
    }
}
// This code is contributed by mits;


PHP




<?php
// PHP implementation of above approach
 
// computing (a^b)%c
function modpow($base, $exp, $modulus)
{
    $base %= $modulus;
    $result = 1;
    while ($exp > 0)
    {
        if ($exp & 1) $result = ($result * $base) %
                                        $modulus;
        $base = ($base * $base) % $modulus;
        $exp >>= 1;
    }
    return $result;
}
 
// Driver code
$n = 654654;
$mod = 971;
echo (((modpow($n, 6, $mod) +
        modpow($n, 2, $mod)) %
        $mod + 355) % $mod);
 
// This code is contributed by mits
?>


Javascript




<script>
 
// JavaScript implementation of above approach
 
// computing (a^b)%c
function modpow(base, exp, modulus)
{
    base %= modulus;
    let result = 1;
    while (exp > 0) {
        if ((exp & 1)>0) result = (result * base) % modulus;
            base = (base * base) % modulus;
            exp >>= 1;
    }
    return result;
}
 
// driver code
 
     let n = 654654;
        let mod = 971;
        document.write(((modpow(n, 6, mod)+
        modpow(n, 2, mod))% mod + 355)% mod);
   
</script>


Output: 

450

 

Time Complexity:O(1), since there is no loop or recursion.

Auxiliary Space: O(1), since no extra space has been taken.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
22 Aug, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments