hi guys,
sorry... this might see a stupid question to you all as i am still a new to c++.
but i like to ask if i wanna to find the power. wat symbol do i use. as i know we use the following symbols for
addition +
substraction -
multiply *
division /
and so on. thus i like to ask wat symbol we actually use for power.
thnx and rgds,
sianz
6 8494
Hi,
Unfortunately, there is no symbol to raise a number to a power. You have to #include <cmath> and use its pow() function. If you're squaring a value, it's probably best to just use the * operator and multiply the value by itself.
Hope this helps.
ok
thanx alot.
now actully i done the program
but the program seems like got some bugs. like for example 17^23 mod 23 = 17 but i got -6. den 21^17 mod 61 = 29 but i got -59. den if i do 2^3 mod 5 =3 which i get the rite answer. the problem is like if i use big number to do, the output i got is wrong but if i use small number, i can get the correct input. can anyone help? -
#include <stdio.h>
-
#include <math.h>
-
using namespace std;
-
int main(void)
-
{
-
int integer1; //first number to be input
-
int integer2; //2nd number to be input
-
int integer3; //modular to be input
-
int integer4; //do the power function
-
int sum; //varible of thing to be stalled in
-
-
printf( "pls enter number: \n" ); //promt for number
-
scanf("%d", &integer1);
-
-
printf( "pls enter power: \n" ); //promt for power
-
scanf("%d", &integer2);
-
-
printf( "pls enter modular: \n" ); //promt for modular
-
scanf("%d", &integer3);
-
-
integer4 = pow(integer1,integer2);
-
//scanf("%d", &integer4);
-
-
sum = integer4 % integer3;
-
printf("ans is %d\n", sum); //print sum
-
return 0; }
-
How large is 17^23, or 23^17?
Hint: The largest number an int can carry is 2,147,483,647.
actually i wan to calculate 17^ 23 mod 23 and 21^17 mod 61 etc... so if the largest int can carry, does it mean i have to use others insted of int?
Banfa 9,065
Expert Mod 8TB
No if you are using modulus division you can use the identity
(a * b) mod c = ((a mod c) * (b mod c)) mod c
If it is a power calculation
(a ^ b) mod c = ((a mod c) ^ b) mod c
At first this doesn't seem useful because in your examples c > a so a mod c == a but if instead of doing the ^ as a single function call you treat it as multiple multiplication (which you can since you are using integers so b is always an integer but we are constraining it to be always positive too) then you can do something like this.
(a ^ b) mod c = ((a mod c) ^ b) mod c
if b == 3
(a ^ b) mod c = (a * a * a) mod c
but
(a * b) mod c = ((a mod c) * (b mod c)) mod c
and
a * b * c = (a * b) * c
so
(a ^ b) mod c = ((((a mod c) * (a mod c)) mod c) * (a mod c)) mod c
Basically you will have to do the power calculation yourself using a loop and at every interim result (iteration) you apply the modulus to keep the values in range of your integers.
This should work as long as c-1 < sqrt(MAX_INT)
Of course another option is to use 64 bit integers that mamny platforms support today.
Also, use <cstdio> and <cmath> rather than <stdio.h> and <math.h>, respectively. The .h style for C standard libraries is deprecated as of 1998 or so. Not a huge deal, just a nit to pick.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: John Graat |
last post by:
Hi all,
I've built the STLport-462 library on AIX-4.3.3 using gcc-3.3.2.
No errors during compilation. However, during linking the following
error occurs:
ld: 0711-317 ERROR: Undefined symbol:...
|
by: ±èµ¿±Õ |
last post by:
Tell me why the symbol "_" use in the Library?
For example,
char *_itoa( int value, char *string, int radix );
But We use function "itoa(value,string,radix) "only.
I want to know the...
|
by: JDeats |
last post by:
Just spent some time browsing around here:
http://msdn.microsoft.com/Longhorn/
I can see the benefits from WinFS (as long as we tag all in-coming
data this should be nice, tagging everything...
|
by: AS |
last post by:
Hi,
I'm trying to use some power management features with windows 2000
Professional and I have visual studio .net 2003 installed with the latest
service packs and everything... I want to write...
|
by: fake ID |
last post by:
Since you can't search for these symbols used in asp.net "<%#" or '<%='
I thought i'd post this to make things a little easier to find.
Potential search word combinations:
-lessthan Percentage...
|
by: Xah Lee |
last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully
Functional Notations
Xah Lee, 2006-03-15
Let me summarize: The LISP notation, is a functional notation, and is
not a...
|
by: Prasad |
last post by:
HI,
I am a beginner in VC++..
I am trying to write a Win32 console application in visual studio..
I am using following header files..
#include <STRING>
using namespace std;
#include...
|
by: bonnielym84 |
last post by:
Im new here..didnt noe whether is this the rite way to post my problem..Really need help here..i've been stucked in this error from last wk..My problem is like this..Im using VC++ 6.0 to compile my C...
|
by: ink |
last post by:
Hi
I am developing on a Symbol MC70 devices and in the past they have always
realised an SDK for development including Symbol.Camera Wrapper.
However it seems that for the MC35 they are not, or...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |