This is about complex three number - c

Thats one wrong with my code and I dont have any idea about this wrong
Please be attention to that I can just use from for and while and if
The question is:
Write a code that gets the natural number n then tries to find x,y,z (natural numbers) in some way:
n=x+y+z
Then if the following is true of x, y, z, print these three numbers in the output, otherwise print the Not Found statement:
x = y ^ 2 + z ^ 2
(x or y or z) = i + (i + 1) + (i + 2)
Where i is a natural number.
Be it. Then if the following is true of x, y, z, print these three numbers in the output, otherwise print the Not Found statement:
x = y ^ 2 + z ^ 2
(x or y or z) = i + (i + 1) + (i + 2)
Where i is a natural number.
(Note that the input n is such that the int variable is sufficient and does not overflow.)
Input
The input contains a line in which a natural number is given.
Output
The output must either consist of three lines, each integer x, y, and z, respectively, from small to large, or the expression Not Found.
Example
Sample Input 1
48
Copy
Sample output 1
2 6 40
Copy
Sample input 2
5
Copy
Sample output 2
Not found
#include <stdio.h>
int main() {
int z,x,y,n;
scanf("%u",&n);
for(y=1;y<(n/3);y++) {
for(z=y;z<=((2*n)/3);z++) {
(x=(n-(y+z)));
if(x==((y*y)+(z*z))) {
if(((((y-3)%3)!=0)||(y==3))&&((((z-3)%3)!=0)||(z==3))&&((((x-3)%3)!=0)||(x==3))) {
continue;
}
printf("%d\n",y);
printf("%d\n",z);
printf("%d",x); return 0;
}
}
}
printf("Not found");
return 0;
}

This syntax (if(x==((yy)+(zz))) is wrong. It should be if (x == ((y*y)+(z*z))) or use pow function from math.h library like this (if(x == (pow(y,2)+pow(z,2)))
int main()
{
int z, x, y, n;
scanf("%u", &n);
for (y = 1; y < (n / 3); y++)
{
for (z = y; z <= ((2 * n) / 3); z++)
{
(x = (n - (y + z)));
if (x == ((y*y)+(z*z)))
{
if (((((y - 3) % 3) != 0) || (y == 3)) && ((((z - 3) % 3) != 0) || (z == 3)) && ((((x - 3) % 3) != 0) || (x == 3)))
{
continue;
}
printf("%d\n", y);
printf("%d\n", z);
printf("%d", x);
return 0;
}
}
}
printf("Not found");
return 0;
}

Related

Can someone explain to me how the modulo character works to output a leading zero

This is the original question:
Create a function that displays all different combination of two digits between 00
and 99, listed by ascending order.
This was how a fellow student did it. It compiles. So all the first single digit outputs have to have a leading zero. I dont understand how he achieved it. Can someone explain to me this a%10 +0 part and how that works to print leading zeros? Thank you in advance.
void ft_print_comb2(void)
{
int x;
int y;
enter code here
x = 0;
while (x < 99)
{
y = x + 1;
while (y < 100)
{
output_format(x, y);
printlines(x, y);
y++;
}
x++;
}
}
void output_format(int x, int y)
{
if (!((x == 99 && y == 99) || (x == 0 && y == 1)))
{
write(1, ", ", 2);
}
}
void printlines(int x, int y)
{
char xmodule;
char xdiv;
char ymodule;
char ydiv;
xmodule = (x % 10 + '0');
xdiv = (x / 10 + '0');
ymodule = (y % 10 + '0');
ydiv = (y / 10 + '0');
write(1, &xdiv, 1);
write(1, &xmodule, 1);
write(1, " ", 1);
write(1, &ydiv, 1);
write(1, &ymodule, 1);
}
For numbers between 0 and 100, x%10 + '0' is the ASCII value of the lowest digit of x, and x/10 + '0' is the ASCII value[1] of the tens digit (or 0 if the number is less than 10). '0' is the ASCII value that represents the character 0, and x%10 is the remainder when dividing x by 10. That's the trick used by your friend's code.
[footnote 1] (or more accurately, the character code from the character set of the execution environment which may or may not be ASCII).
However, the code written is quite complicated, and formatting numbers for output (including leading zeros) is provided by the standard library. Loops that range between two numbers can be conveniently described using a for loop. The function write used by the program is POSIX, and not part of the C standard library.
Here's a complete version using printf:
#include <stdio.h>
int main(int argc, char *argv[]) {
for (int x = 0; x < 100; x++) {
for (int y = x+1; y < 100; y++) {
if (y!=1) printf(", "); // skip comma for the first pair output.
printf("%02d %02d", x, y);
}
}
printf("\n");
return 0;
}

Understanding calling one function inside another C

I'd like to ask the following misunderstandings of C language, which I see I'm having.
I'm sorry if the code is not properly indented, I tried as much as I could but there are not so many guides on the internet.
The program asked given a starting number 'val' and a Even-Odd or Odd-Even alternating sequence (which stops whenever this rules is violated) to print the greater prime number with 'val'.
I tried with two functions and the main: one to control the GCD between two given numbers and the other to keep tracks of the greatest one, but I think I miss something in the code or in the conception of C function,
Because when compiled it returns me 0 or great number which I'm not entering.
One example to understand what I should do:
If my sequence was 10, 7, 8, 23 and my val was 3, I had to print 23, because it is the greatest integer prime with 3.
Here's the code :
#include <stdio.h>
int mcd(int a, int b)
{ // Gcd function
if (a == 0)
return b;
else
return mcd(b % a, b);
}
int valuta(int val, int h) // Valuing Max function
{
int temp = 0;
if (mcd(val, h) == 1 && h > temp)
temp = h;
return temp;
}
int main()
{
int val, d, x, y, z, t, contatore = 1;
scanf("%d", &val);
scanf("%d%d", &x, &y);
if (x > y && mcd(val, x) == 1)
{ // Two options
t = x;
}
else if (y > x && mcd(val, y) == 1)
{
t = y;
}
if ((x % 2 == 0 && y % 2 == 0) || (x % 2 == 1 && y % 2 == 1))
{ // Bad case
if (x > y && mcd(val, x) == 1)
{
t = x;
contatore = 0;
}
else if (y > x && mcd(val, y) == 1)
{
t = y;
contatore = 0;
}
}
else
{
while (contatore == 1)
{
scanf("%d", &z);
t = valuta(val, z);
if (x % 2 == 0 && z % 2 == 0)
{ // Even- Odd - Even
scanf("%d", &d);
t = valuta(val, d);
if (d % 2 == 0)
{
contatore = 0;
}
else
{
contatore = 0;
}
}
if (x % 2 == 1 && z % 2 == 1)
{ //Odd- Even- Odd
scanf("%d", &d);
t = valuta(val, d);
if (d % 2 == 1)
{
contatore = 0;
}
else
{
contatore = 0;
}
}
}
}
printf("%d\n", t);
return 0;
}
PS. Is there any way to reduce the number of lines of code or to reduce the effort in coding? I mean, a straightforward solution will be helpful.
Your valuta() function is flawed in that it needs to return the maximum qualifying value so far but has no knowledge of the previous maximum - temp is always zero. The following takes the previous maximum as an argument:
int valuta(int val, int h, int previous )
{
return ( mcd(val, h) == 1 && h > previous ) ? h : previous ;
}
And is called from main() thus:
t = valuta( val, x, t ) ;
The test mcd(val, h) == 1 is flawed, because mcd() only ever returns the value of parameter b which is not modified in the recursion, so will never return 1, unless the argument b is 1. Since I have no real idea what mcd() is intended to do, I cannot tell you how to fix it. It appear to be a broken implementation of Euclid's greatest common divisor algorithm, which correctly implemented would be:
int mcd(int a, int b)
{
if(b == 0)
return a;
else
return mcd(b, a % b);
}
But I cannot see how that relates to:
"[...] he greatest integer prime with 3 [...]
The odd/even even/odd sequence handling can be drastically simplified to the extent that it is shorter and simpler than your method (as requested) - and so that it works!
The following is a clearer starting point, but may not be a solution since it is unclear what it is it is supposed to do.
#include <stdio.h>
#include <stdbool.h>
int mcd(int a, int b)
{
if(b == 0)
return a;
else
return mcd(b, a % b);
}
int valuta(int val, int h, int previous )
{
return ( mcd(val, h) && h > previous ) ? h : previous ;
}
int main()
{
int val, x, t ;
printf( "Enter value:") ;
scanf("%d", &val);
typedef enum
{
EVEN = 0,
ODD = 1,
UNDEFINED
} eOddEven ;
eOddEven expect = UNDEFINED ;
bool sequence_valid = true ;
printf( "Enter sequence in odd/even or even/odd order (break sequence to exit):\n") ;
while( sequence_valid )
{
scanf("%d", &x);
if( expect == UNDEFINED )
{
// Sequence order determined by first value
expect = (x & 1) == 0 ? EVEN : ODD ;
}
else
{
// Switch expected odd/even
expect = (expect == ODD) ? EVEN : ODD ;
// Is new value in the expected sequence?
sequence_valid = (expect == ((x & 1) == 0 ? EVEN : ODD)) ;
}
// If the sequence is valid...
if( sequence_valid )
{
// Test if input is largest qualifying value
t = valuta( val, x, t ) ;
}
}
// Result
printf("Result: %d\n", t);
return 0;
}

Number doesn't print in expected way

This is my program on 'Sin Calculator'
#include <stdio.h>
#include <math.h>
#define PI 3.1416
void fraction(double x, int y);
void print(double sx, int x, int y, int s);
void scal();
int main(){
scal();
return 0;
}
void fraction(double x, int y){
int b = 100, a;
a = x * 100;
while ((a % 2 != 0) || (a % 5 != 0)){
if (a % 2 == 0){
a /= 2;
b /= 2;
}
else if (a % 5 == 0){
a /= 5;
b /= 5;
}
}
print(x, a, b, y);
}
void print(double sx, int x, int y, int s){
printf (" Sin(%d) = %d/%d || %.2lf\n",s,x,y,sx);
scal();
}
void scal(){
double sine, sinx;
int x, a, b;
printf ("\n Sin(X) : ");
scanf ("%d",&x);
sinx = x * (PI / 180);
sine = sin(sinx);
fraction(sine, x);
}
I don't get any errors. But when i run it, though the variable 'a' of fraction function can be divided by 5 or 2, it doesn't do it. As a result I get the whole value of 'a'. For example, the value of Sin30 degrees is 0.50, so multiplying it with 100 makes 'a' 50. 50 is dividable by 2 and 5. But in fraction function, it seems that 'a' doesn't get divided there. As a result in 'print' function, i get '50/100' instead of '1/2'. Why is this happening? And also when i enter somethin like Sin23, the program doesn't finish. It stops. What's the problem?
while ((a % 2 != 0) || (a % 5 != 0))
this test fails for the value of 50 or any integer that is multiple of both 2 an 5. Therefore the while loop will not be entered and no division will occur, so a conserves its value.
maybe your intent was the inverse of the condition:
while ((a % 2 == 0) || (a % 5 == 0))

Point operations on an elliptic curve in a prime field in c

I am trying to write a program to perform point operations on a elliptic curve in a prime field I am using the standard formulaes for point additions and doubling in my code and these operations are performed by functions that are called but I am getting output for certain points but not all so please help me to fix the problem that are present in this code.
structure point_multiply(int x, int y, int k )
{
int xk;
int yk,m;
xk=x;
yk=y;
m=1;
int xL,yL,s,e;
e=findInverse((2*yk),211);
if((((3*(xk*xk))*e)% 211)>0)
{s = (((3*(xk*xk))*e)% 211);
}
else
s=(((3*(xk*xk))*e)% 211)+ 211;
if((((s*s)- (2*xk)) % 211)>0)
{xL=(((s*s)- (2*xk)) % 211);
}
else
xL=(((s*s)- (2*xk)) % 211) + 211;
if(((-yk+ s*(xk-xL)) % 211) > 0)
yL=(-yk+ s*(xk-xL)) % 211;
else
yL=(-yk+ s*(xk-xL)) % 211 + 211;
xk=xL;
yk=yL;
m=m+1;
while(k>m)
{
sn=point_addition(xk,yk,x,y);
xk=sn.a;
yk=sn.b;
m++;
}
s1.a=xk;
s1.b=yk;
return s1;
}
structure point_addition(int x1, int y1, int x2, int y2)
{
int s,xL,yL;
if((x1-x2)!=0)
{
if ( x1 == 0 && y1 == 0 )
{
xL = x2;
yL = y2;
s7.a=xL;
s7.b=yL;
return s7;
}
if ( x2 == 0 && y2 == 0 )
{
xL = x1;
yL = y1;
s7.a=xL;
s7.b=yL;
return s7;
}
if ( y1 == -y2 )
{
xL = yL = 0;
s7.a=xL;
s7.b=yL;
return s7;
}
l=findInverse((x1-x2),211);
if ((((y1-y2)*l) % 211)>=0)
s=((((y1-y2)*l) % 211));
else
s=(((y1-y2)*l) % 211) + 211;
if ((((s*s)-(x1+x2)) % 211)>0)
xL= (((s*s)-(x1+x2)) % 211) ;
else
xL= (((s*s)-(x1+x2)) % 211) + 211;
if(((-y1+s*(x1-xL)))>=0)
yL= ((-y1+s*(x1-xL)) % 211);
else
yL= ((-y1+s*(x1-xL)) % 211) + 211;
}
else
{
xL= 0 ;
yL= 0;
}
s7.a= xL;
s7.b= yL;
return s7 ;
}
int findInverse(int a, int b)
{
int x[3];
int y[3];
int quotient = a / b;
int remainder = a % b;
x[0] = 0;
y[0] = 1;
x[1] = 1;
y[1] = quotient * -1;
int i = 2;
for (; (b % (a%b)) != 0; i++)
{
a = b;
b = remainder;
quotient = a / b;
remainder = a % b;
x[i % 3] = (quotient * -1 * x[(i - 1) % 3]) + x[(i - 2) % 3];
y[i % 3] = (quotient * -1 * y[(i - 1) % 3]) + y[(i - 2) % 3];
}
//x[i — 1 % 3] is inverse of a
//y[i — 1 % 3] is inverse of b
if(x[(i - 1) % 3]<0)
return x[(i - 1) % 3]+211;
else
//x[i — 1 % 3] is inverse of a
//y[i — 1 % 3] is inverse of b
return x[(i - 1) % 3];
}
Edited and added main c code which uses these function to perform elliptic curve cryptography
int main()
{
int y,z=0,x=2,i[200],j[200],h=0,g,k;
while(x<200)
{
y=sqrt((x*x*x)-4);
z=modulo(y,211);
if(z!=0)
{
i[h]=x;
j[h]=z;
s[h].a=i[h];
s[h].b=j[h];
s[h+1].a=i[h];
s[h+1].b=(211 - j[h]);
printf("\nh=%d X= %d Y= %d \nh=%d X= %d Y= %d",h,s[h].a,s[h].b,h+1,s[h+1].a,s[h+1].b);
h=h+2;
}
x++;
}
printf("The total no of points we have on our elliptic curve for cryptography is %d",h-1);
x=5;
y=11;
printf("\n %d %d\n",x,y );
printf("\nEnter A number between 0 and the private key");
scanf("%d",&k);
s2=point_multiply(x,y,k);
printf("\n The public key is \n %d %d \n ",s2.a,s2.b );
printf("Enter a RANDOM number to generate the cipher texts");
scanf("\n%d",&g);
s3= point_multiply(x,y,g);
s4=point_multiply(s2.a,s2.b,g );
label:
printf("\n Enter a number to send");
scanf("%d",&h);
s6=point_addition(s4.a,s4.b,s[h].a,s[h].b);
printf("The points to be sent are X= %d Y=%d",s[h].a,s[h].b);
printf(" \n X= %d Y=%d\n X = %d Y= %d ",s3.a,s3.b,s6.a,s6.b);
//RECIEVER
s8=point_multiply(s3.a,s3.b,k);
s9=point_addition((s8.a) ,-((s8.b)%211),s6.a,s6.b);
printf(" The decrypted points are \n %d %d",s9.a,s9.b);
printf("\n If you have more no to send press 1 else press 0");
scanf("\n %d", &x1);
if(x1==1)
goto label;
else
return 0;
}
s1, s2, s3 etc are structures which hold a 2 integers which act as x and y co-ordinates
I am getting output by entering k=3,g=4, h=5 and many other cases mostly with small numbers but not for larger numbers. What could be wrong with the code?
Further edit: I guess that normal square root method is not applicable to find square roots of a modular no?.. Please tell me how to find the modular square root of a no?

Runtime Error of too much memory used?

#include <stdio.h>
int main()
{
int X = 200;
float Y = 1500;
printf("Enter your initial Balance and the Amount to be Withdrawn. Note the Values should lie between 0 and 2000");
scanf("%d", "%e", &X, &Y);
if ((0 < X < 2000) && (0 < Y < 2000)) {
if ((X < Y) && (X % 5 == 0)) {
Y = Y - X;
Y = Y - 0.5;
} else {
printf("%f", Y);
}
printf("%f", Y);
} else {
printf("The Input is Wrong");
}
return 0;
}
The Code basically asks for some number X. Subtracts it from Y and an additional amount 0.5 from Y. We have to give the as Y.
The Code is giving Runtime Error which could possibly be due to More Memeory than allowed being used.
Can anyone give any tips on how to reduce memory usage or see if there is an error in the program?
scanf("%d,%e", &X, &Y);
(apart from the scanf() with the double format string, which has been handled by others)
if ((0 < X < 2000) && (0 < Y < 2000)) {
That does not work this way is C. You could try:
if (X > 0 && X < 2000 && Y > 0 && Y < 2000) {
Also note that you don't need the extra parentheses.
The same for the other line
if ((X < Y) && (X % 5 == 0)) {
Which could be:
if (X < Y && X % 5 == 0) {
Sometimes the rules of precedence are not that bad at all...
I don't know if this is homework...
... but this example might help clarify a few things:
#include <stdio.h>
int
main(int args, char *argv[])
{
int new_balance, old_balance;
float withdrawal;
/* Get input */
printf("Enter your initial Balance and the Amount to be Withdrawn.\n");
printf("Note the values should lie between 0 and 2000\n");
while (scanf("%d %f", &old_balance, &withdrawal) != 2) {
printf ("please enter two valid floating point numbers\n");
}
/* Validate input */
if ( (old_balance < 0.0) || (old_balance > 2000.0) ) {
printf ("error: balance(%d): must be between 0.0 and 2000.0\n",
old_balance);
return 1;
}
if ( (withdrawal < 0.0) || (withdrawal > 2000.0) ) {
printf ("error: withdrawal(%f): must be between 0.0 and 2000.0\n",
withdrawal);
return 1;
}
/* Compute balance */
new_balance = old_balance - withdrawal;
/* Print results */
printf ("Withdrawal: %f; old balance: %d, new balance: %d.\n",
withdrawal, old_balance, new_balance);
return 0;
}
I'm not at all sure what the requirement was with the "0.5" stuff, so I left it out. My guess is that you wanted to "round up to the nearest dollar". In which case "%" is definitely not the way to do it.
The original program might have compiled - but it almost certainly wasn't "correct".
And the original program, as far as I could tell, should have run just about anywhere - I didn't see anything likely to cause an "out of memory condition".
'Hope that helps .. at least a little...

Resources