برنامه محاسبه pow(x,n) — راهنمای کاربردی
در این مطلب روش نوشتن برنامه محاسبه pow(x,n) (عدد x به توان n) بیان شده و پیادهسازی آن در زبانهای برنامهنویسی گوناگون، شامل «سیپلاسپلاس» (++C)، «جاوا» (Java)، «پایتون» (Python)، «سیشارپ» (#C) و «پیاچپی» (PHP) انجام شده است. دو عدد صحیح x و n داده شده است. هدف نوشتن تابعی است که xn را محاسبه کند. فرض میشود که x و n اعدادی کوچک هستند و سرریز اتفاق نمیافتد. مثال زیر، به درک بهتر مطلب کمک میکند.
Input : x = 2, n = 3 Output : 8 Input : x = 7, n = 2 Output : 49
راهکار اول
در پیادهسازیهای ارائه شده در زیر، مسئله به زیر مسائلی با اندازه y/2 تقسیم میشود و زیر مسائل به صورت بازگشتی فراخوانی میشوند.
پیادهسازی راهکار اول در ++C
// C++ program to calculate pow(x,n)
#include<iostream>
using namespace std;
class gfg
{
/* Function to calculate x raised to the power y */
public:
int power(int x, unsigned int y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
};
/* Driver code */
int main()
{
gfg g;
int x = 2;
unsigned int y = 3;
cout << g.power(x, y);
return 0;
}
// This code is contributed by SoM15242
پیادهسازی راهکار اول در C
#include<stdio.h>
/* Function to calculate x raised to the power y */
int power(int x, unsigned int y)
{
if (y == 0)
return 1;
else if (y%2 == 0)
return power(x, y/2)*power(x, y/2);
else
return x*power(x, y/2)*power(x, y/2);
}
/* Program to test function power */
int main()
{
int x = 2;
unsigned int y = 3;
printf("%d", power(x, y));
return 0;
پیادهسازی راهکار اول در جاوا
class GFG {
/* Function to calculate x raised to the power y */
static int power(int x, int y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
/* Program to test function power */
public static void main(String[] args)
{
int x = 2;
int y = 3;
System.out.printf("%d", power(x, y));
}
}
// This code is contributed by Smitha Dinesh Semwal
پیادهسازی راهکار اول در پایتون ۳
# Python3 program to calculate pow(x,n)
# Function to calculate x
# raised to the power y
def power(x, y):
if (y == 0): return 1
elif (int(y % 2) == 0):
return (power(x, int(y / 2)) *
power(x, int(y / 2)))
else:
return (x * power(x, int(y / 2)) *
power(x, int(y / 2)))
# Driver Code
x = 2; y = 3
print(power(x, y))
# This code is contributed by Smitha Dinesh Semwal.
پیادهسازی راهکار اول در #C
using System;
public class GFG {
// Function to calculate x raised to the power y
static int power(int x, int y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
// Program to test function power
public static void Main()
{
int x = 2;
int y = 3;
Console.Write(power(x, y));
}
}
// This code is contributed by shiv_bhakt.
پیادهسازی راهکار اول در PHP
<?php
// Function to calculate x
// raised to the power y
function power($x, $y)
{
if ($y == 0)
return 1;
else if ($y % 2 == 0)
return power($x, (int)$y / 2) *
power($x, (int)$y / 2);
else
return $x * power($x, (int)$y / 2) *
power($x, (int)$y / 2);
}
// Driver Code
$x = 2;
$y = 3;
echo power($x, $y);
// This code is contributed by ajit
?>
خروجی قطعه کد بالا، به صورت زیر است.
۸
پیچیدگی زمانی روش بالا از درجه O(n) و پیچیدگی فضایی آن از درجه O(1) است. در این روش، از پارادایم تقسیم و حل (Divide and Conquer) استفاده شده است.
راهکار دوم
راهکار بالا را میتوان با تنها یک بار محاسبه power(x, y/2) ، با درجه O(logn) بهینهسازی کرد.
/* Function to calculate x raised to the power y in O(logn)*/
int power(int x, unsigned int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}
راهکار سوم
در ادامه، تابع توان (pow) به نوعی بسط داده شده است که برای اعداد منفی y و اعداد شناور x نیز کار کند.
پیادهسازی راهکار سوم در ++C
/* Extended version of power function
that can work for float x and negative y*/
#include <bits/stdc++.h>
using namespace std;
float power(float x, int y)
{
float temp;
if(y == 0)
return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
{
if(y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
// Driver Code
int main()
{
float x = 2;
int y = -3;
cout << power(x, y);
return 0;
}
// This is code is contributed
// by rathbhupendra
پیادهسازی راهکار سوم در C
/* Extended version of power function that can work
for float x and negative y*/
#include<stdio.h>
float power(float x, int y)
{
float temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
{
if(y > 0)
return x*temp*temp;
else
return (temp*temp)/x;
}
}
/* Program to test function power */
int main()
{
float x = 2;
int y = -3;
printf("%f", power(x, y));
return 0;
}
پیادهسازی راهکار سوم در جاوا
/* Java code for extended version of power function
that can work for float x and negative y */
class GFG {
static float power(float x, int y)
{
float temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
{
if(y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
/* Program to test function power */
public static void main(String[] args)
{
float x = 2;
int y = -3;
System.out.printf("%f", power(x, y));
}
}
// This code is contributed by Smitha Dinesh Semwal.
پیادهسازی راهکار سوم در پایتون ۳
# Python3 code for extended version
# of power function that can work
# for float x and negative y
def power(x, y):
if(y == 0): return 1
temp = power(x, int(y / 2))
if (y % 2 == 0):
return temp * temp
else:
if(y > 0): return x * temp * temp
else: return (temp * temp) / x
# Driver Code
x, y = 2, -3
print('%.6f' %(power(x, y)))
# This code is contributed by Smitha Dinesh Semwal.
پیادهسازی راهکار سوم در #C
// C# code for extended version of power function
// that can work for float x and negative y
using System;
public class GFG{
static float power(float x, int y)
{
float temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y % 2 == 0)
return temp * temp;
else
{
if(y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
// Program to test function power
public static void Main()
{
float x = 2;
int y = -3;
Console.Write(power(x, y));
}
}
// This code is contributed by shiv_bhakt.
پیادهسازی راهکار سوم در PHP
<?php
// Extended version of power
// function that can work
// for float x and negative y
function power($x, $y)
{
$temp;
if( $y == 0)
return 1;
$temp = power($x, $y / 2);
if ($y % 2 == 0)
return $temp * $temp;
else
{
if($y > 0)
return $x *
$temp * $temp;
else
return ($temp *
$temp) / $x;
}
}
// Driver Code
$x = 2;
$y = -3;
echo power($x, $y);
// This code is contributed by ajit
?>
خروجی قطعه کد بالا به صورت زیر است.
۰٫۱۲۵۰۰۰
اگر نوشته بالا برای شما مفید بوده است، آموزشهای زیر نیز به شما پیشنهاد میشوند:
- مجموعه آموزشهای برنامه نویسی
- آموزش ساختمان دادهها
- مجموعه آموزشهای ساختمان داده و طراحی الگوریتم
- رنگآمیزی گراف به روش حریصانه — به زبان ساده
- الگوریتم دایجسترا (Dijkstra) — از صفر تا صد
- الگوریتم پریم — به زبان ساده
- متن کاوی (Text Mining) — به زبان ساده
مجموعه: برنامه نویسی, مهندسی کامپیوتر برچسب ها: code to calculate power, برنامه محاسبه توان, کد محاسبه توان, محاسبه توان در #C, محاسبه توان در PHP, محاسبه توان در جاوا




(No Ratings Yet)