473,513 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perfect Squares

Q-1
Can sm1 suggest a program to check whether a given no is perfect
square or not??
Q-2
Also sm1 suggest a program to check whether a given no is perfect
power or not??
* PEfect power is a no which can be expressed some power of a any noo.

Its better if it is very efficient!

Jun 16 '07 #1
4 8090
Also (and obviously) use of lib-funcs like pow() and sqrt() is not
allowed

Jun 16 '07 #2

"The 1" <ju**********@gmail.comha scritto nel messaggio
news:11**********************@a26g2000pre.googlegr oups.com...
Q-1
Can sm1 suggest a program to check whether a given no is perfect
square or not??
Check wheter there is some integer such as (i * i == n).
If you have problems with your code, post it here.
Q-2
Also sm1 suggest a program to check whether a given no is perfect
power or not??
* PEfect power is a no which can be expressed some power of a any noo.
C is based on English, so if you want to write the former it would
be a good idea to learn the latter.
Its better if it is very efficient!
On a modern computer, even the least efficient way to do that takes
several orders of magnitude shorter time than I/O operations, so
unless you have to check for zilion numbers, it doesn't matter. And
modern compilers are often able to optimize it, anyway.
Jun 16 '07 #3
The 1 said:
Q-1
Can sm1 suggest a program to check whether a given no is perfect
square or not??
I'm not sure what you mean by "sm1" or "no" (in the context). I'm
guessing at "someone" and "number", but I'm not certain that my guesses
are correct.

The important thing about homework is to try doing it for yourself.
That's a vital part of the learning process.

To start you off:

The term "perfect square" suggests that this is an exercise in integers.
The highest value one can portably represent in a native unsigned
integer type is 4294967295, which is just a shade short of being a
perfect square itself. To be able to represent this, use unsigned long
int. The highest perfect square that can be portably represented in an
unsigned long int is 4294836225.

The lowest perfect square you can represent in an unsigned long int is 0
(or, if you don't count that, 1).

So you have lower and upper bounds on your problem.

Your next step is to capture a value from the user, and ensure it is
within the range 0 (or 1) to 4294836225. If it isn't, it might yet be a
perfect square, but not one that you can handle easily within the
context of a simple homework exercise, so you can reject it as being
outside the supported range.

Now you need to find the integer that is closest to the square root of
the number. You can find this via binary search.

Now multiply that number by itself. If it is equal to the input number,
the answer is "yes, this is a perfect square". Otherwise, the answer is
"no, this is not a perfect square".
Q-2
Also sm1 suggest a program to check whether a given no is perfect
power or not??
I suggest you finish Question 1 before attempting Question 2.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 16 '07 #4

"The 1" <ju**********@gmail.comha scritto nel messaggio
news:11**********************@a26g2000pre.googlegr oups.com...
Q-1
Can sm1 suggest a program to check whether a given no is perfect
square or not??
Q-2
Also sm1 suggest a program to check whether a given no is perfect
power or not??
* PEfect power is a no which can be expressed some power of a any noo.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
struct Power { int Is; long Base, Expon; } is_power(long n)
{
struct Power result;
result.Is = 1;
result.Base = n;
result.Expon = 1;
return result;
}

int main(int argc, char *argv[])
{
long n;
char *end;
struct Power is;
if (argc < 2) {
fputs("Provide a number, please...\n", stderr);
exit(EXIT_FAILURE);
}
n = strtol(argv[1], &end, 10);
if (*end != '\0')
printf("Whatever garbage you mean by \"%s\" will be "
"ignored.\n", end);
if (errno == ERANGE)
printf("The number you wrote is too large or too small, "
"let's pretend you wrote %ld.\n", n);
is = is_power(n);
if (is.Is)
printf("%ld is a perfect power as it equals %ld**%ld.\n"
n, is.Base, is.Expon);
else
printf("%ld is not a perfect power.");
return 0;
}
Jun 17 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5473
by: venkat | last post by:
Hi, I want to solve linear least sqaure problem( min||c-Ax||2 subject to Bx=d ). How do I do it in python. lapack has a routine for doing this (DGGLSE). Can I access this from python? TIA,...
6
3642
by: binger | last post by:
Okay, Superbowl is only a few days away, but I thought it would be cool to set up an online registration for a football squares pool. My thoughts are to have people go into anywhere on the 10x10...
4
1442
by: Dino M. Buljubasic | last post by:
This might be a silly question but I have never seen this before. I suddenly got some blue squares on the left side (the place where break points are shown if any exist). Anybody knows what...
1
1331
by: John Bailo | last post by:
For some reason, blue squares with rounded corners started appearing on the left side of VS.NET -- in the same place were the brown circles that represent breakpoints are. What do these...
10
4605
by: wazzup | last post by:
C++ is new to me and I'm trying solve this problem but get stuck. Please help me out. Below is the task and after that is what i got so far. Create a program to print nested squares Input:...
8
2196
evilmonkey
by: evilmonkey | last post by:
I am using swing to nest squares with in squares (256 times) and that part is working fine, but what i want to do is change the color of each line so that the red green and blue components each...
1
1290
by: jyohere | last post by:
I have a problem to solve.I want to draw squares and lines connecting them.The number of squares is only known at runtime.How can it be done.if someone has some code like this,pls help.I am able to...
1
4007
by: J.SanTanA | last post by:
Hello All! Any one know where can I find some php code (or "php-able" code) implementing least squares method? Thx in advance and pls excuse my poor eng
4
5221
by: Evelien | last post by:
Dear python-users, I am trying to do a non-linear least squares fitting. Maybe trying is not the best word, as I already succeeded in that. At the moment I am using leastSquaresFit from...
0
7260
marktang
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,...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7537
jinu1996
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...
0
7525
tracyyun
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...
1
5086
isladogs
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...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.