473,505 Members | 15,976 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A Simple C Code to Discuss

hello everyone. hours ago i found a topic about solving the problem:
find out 2 int between certain rang, say 1---100, both the 2 int's sum
and minus should be a int's square.
i write this piece of code and i want you to give some advice. can the
code run quicker? i think there must exist many quicker ones.
/*
this doucument and source code belong to Ethan Mys, oct 30, 2005.
any modification to improve the source code is welcome.
you can also spit at this piece of "shit", because i am a masochist.
E-Mail: et******@gmail.com
*/
#include<stdio.h>

int main()
{
int k=0;
int tb[][6]={{1,9,25,81,121,169},\
{4,16,36,64,100,144,}};
for(k=1;k<=13;k++)
{
int k2=k*k;
int temp=0;int temp2=0;

while((temp2=tb[k%2?0:1][temp])<k2)
{
int res[10];
res[temp]=(k2+temp2)/2;
printf("%3d and %3d ",res[temp],k2-res[temp]);
temp++;
}
printf("\n");
}
getchar();
}

Nov 15 '05 #1
7 1317
ethanmys said:
hello everyone. hours ago i found a topic about solving the problem:
find out 2 int between certain rang, say 1---100, both the 2 int's sum
and minus should be a int's square.
i write this piece of code and i want you to give some advice. can the
code run quicker? i think there must exist many quicker ones.


Here's a much quicker version:

#include <stdio.h>

int main(void)
{
puts("The pair (384, 400) is the smallest positive integer solution.");
return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 15 '05 #2
ethanmys wrote:
hello everyone. hours ago i found a topic about solving the problem:
find out 2 int between certain rang, say 1---100, both the 2 int's sum
and minus should be a int's square.


If I understand you correctly, this is the problem: Find two integers
within the given range, say 1 to 100, where the sum of the two is the
square of an integer, and the difference of the two is also the square
of an integer.

Mathematically,
(x + y) == a * a && (x - y) == b * b

Where 'x' and 'y' are the integers to find, and 'a' and 'b' are the
square roots of the sum and difference respectively.

I fed this into Mathematica:

Reduce[(x + y) == a * a && (x - y) == b * b, {x, y}, Integers]

And got the result:

2 2
a + b 2
(a | b | x | y) \[Element] Integers && x == ------- && y == a - x
2

I then wrote a program to step over possible values of a and b,
calculate the x and y values, and check if they were within a given range.

#include <stdio.h>

int main(void)
{
int a, b, n = 0;
int min = 1;
int max = 100;

for(a = 0; a < 100; a++)
{
for(b = 0; b < 100; b++)
{
int ssq = a * a + b * b;
if(ssq % 2 == 0)
{
int x = ssq / 2;
int y = a * a - x;
if(min <= x && x <= max && min <= y && y <= max)
{
printf("%d,%d\t", x,y);
n++;
}
}
}
if(n)
{
printf("\n");
n = 0;
}
}
return 0;
}

The program gives the following results:

2,2
5,4
8,8 10,6
13,12 17,8
18,18 20,16 26,10
25,24 29,20 37,12
32,32 34,30 40,24 50,14
41,40 45,36 53,28 65,16
50,50 52,48 58,42 68,32 82,18
61,60 65,56 73,48 85,36
72,72 74,70 80,64 90,54
85,84 89,80 97,72
98,98 100,96

Each of these results seem to be correct according to my understanding
of the problem. The sum of each pair is a square number, and the
difference of each pair is also a square number.

--
Simon.
Nov 15 '05 #3
Simon Biber said:
ethanmys wrote:
hello everyone. hours ago i found a topic about solving the problem:
find out 2 int between certain rang, say 1---100, both the 2 int's sum
and minus should be a int's square.


If I understand you correctly,


I didn't, alas. I don't know why but I read more squares into the problem
than was warranted by the question. I have cancelled my article, but of
course many news servers won't honour that.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 15 '05 #4
your code is clear. but i think it will cost O(n^2). what i think is:
there shoud be a O(1) solution to this problem ( don't consider the
input ).

Nov 15 '05 #5
ethanmys wrote
(in article
<11**********************@z14g2000cwz.googlegroups .com>):
your code is clear. but i think it will cost O(n^2). what i think is:
there shoud be a O(1) solution to this problem ( don't consider the
input ).


Sure, a pre-computed lookup table. :-)
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Nov 15 '05 #6
In article <11**********************@g47g2000cwa.googlegroups .com>,
ethanmys <et******@gmail.com> wrote:
hello everyone. hours ago i found a topic about solving the problem:
find out 2 int between certain rang, say 1---100, both the 2 int's sum
and minus should be a int's square. i write this piece of code and i
want you to give some advice. can the code run quicker? i think there
must exist many quicker ones.
Let me translate. You are stating:
Problem 1:
Find all integers x and y between 1 and N such that
x+y and x-y are squares.

However your program does not solve this. It solves the following
problem instead:
Problem 2:
Find all integers x and y such that x+y and x-y are squares
and x+y is at most N.

Aside: Your table of squares is missing 7^2 = 49:
int tb[][6]={{1,9,25,81,121,169},\
{4,16,36,64,100,144,}};


To solve the problems stated above, it's best to do a little
math before writing a program. You are looking for x and
y such that:

x + y = a^2, x - y = b^2.

By solving this system of two equations in the two unknowns
x, and y we get:

x = (a^2 + b^2)/2, y = (a^2 - b^2)/2.

We observe that a and b have to be both even or both odd,
otherwise the fractions shown above will not yield integers.

In view of this, we can print the numbers you are looking
for as follows:

int i, j, I, J, N;

for (i=1; I=i*i, i<=N; i++)
for (j=i+2; J=j*j, j<=N; j+=2)
printf("(%d, %d)\n", (J+I)/2, (J-I)/2);

This solves Problem 1. To solve Problem 2, change the
stopping criteria i<=N and j<=N to I<=N and J<=N.

--
Rouben Rostamian
Nov 15 '05 #7
Yes. Your code is concise and is apt at solving both problem. It
inspires me a lot and really should be included in a C textbook.:)
Thanks

Nov 15 '05 #8

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

Similar topics

1
5956
by: learningGuy | last post by:
Can someone tell me what is wrong with this simple code? I get an exception every time at the myFile.Open() line. I have included the code that I think is needed to for you to answer this below:...
18
2124
by: Ben Hanson | last post by:
I have created an open source Notepad program for Windows in C++ that allows search and replace using regular expressions (and a few other extras). It is located at...
73
4536
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
1
5171
by: Arjen | last post by:
Hi, Im trying to make / looking for a very very simple WYSIWYG editor. Ive made a (ad free) site about dogs but it's attracting way more visitors then I could ever have imagined. I have a forum...
10
1629
by: chandanlinster | last post by:
I was just curious to know whether there is an option within the gcc compiler that lets us produce object code for 16-bit processor, while we are working on 32-bit machines.
14
1785
by: howa | last post by:
void reverse_string(char *str) { if (str == NULL) return; char tmp; size_t len = strlen(str); size_t mid = (int) len / 2; for (size_t i = 0; i < mid; i++) {
4
34777
true911m
by: true911m | last post by:
Here's a little walkthrough to get py2exe up and running. I'm not an expert, so I can't help much with any problems you might have. This is what worked for me. The result here will be to convert...
5
1951
by: Michael | last post by:
Hi All, I have three very simple files as below. When I try and compile these with g++ -ansi -Wall -pedantic -o crap Base.h Other.h I get an error: Base.h:7: internal compiler error:...
30
3472
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
0
1240
by: Guilherme Polo | last post by:
On Sat, Sep 20, 2008 at 4:10 PM, dmitrey <dmitrey15@ukr.netwrote: It is not only the button that doesn't respond, the entire application won't respond if you are blocking tcl from processing...
0
7218
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
7103
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...
1
7021
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...
0
7478
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...
0
4701
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
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...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.