Requesting permission to post the code

  • Thread starter smslca
  • Start date
  • Tags
    Code
In summary: We are calculating the difference of P and P-6 // // // // // // // // // // // // // // // // //
  • #1
smslca
64
0
I have written a C code program for factorization of a given number into two divisors, when the two divisors are having approximately the same number of digits . It cannot check for prime numbers.

With my code limit I can check it only upto 14 digits using long long int. It is working perfectly upto these 14 digits (condition is the two divisors must have app the same no.of.digits). Bigger than 14 digits it is yielding wrong answers.

with the permission of this forum I like to post the code here. So that someone can check its efficiency for large numbers.

If anyone's idea is to use GMP libray, I have already installed gmp using msys, and had written a C code program using gmp. The problem I faced is, when I compiled the program , it compiled perfectly. But when I selected to Run the code, It says "Project is stopped working". The problem is in input and output code lines i.e., I used printf and scanf functions to get o/p and i/p.
To avoid this problem I did not find any best tutorial on GMP. And I did not understand what he is saying about the o/p and i/p functions, in the Documentation of GMP-5.0.1.
If you accept to post the code using gmp , I will post that also.

Thankful for the forum mentors and moderators and users , If accepted to post the code. And for any replies given.
 
Technology news on Phys.org
  • #2
First this thread should be posted in the computer science section.
Second are you having difficulty in checking out prime numbers? If yes then a simple code like this might help (written in c++, just replace cout with printf and remove the '<<' operators etc and adjust the syntax)
Code:
#include<iostream.h>
int main()
{
int num, factors, factorl;
char response;
do{
cout<<"Enter a number larger than 1"<<endl;
cin>>num;
if (num>1)
{
factors=2;
factorl=num-1;
while(num%factors>0)factors++;
while(num%factorl>0)factorl--;
if (factors==num && factorl==1)
{
cout<<"The smallest factor of "<<num<<" is equal to 1"<<endl;
cout<<"The largest factor of "<<num<<" is equal to "<<num<<endl;
cout<<"The number "<<num<<" is prime"<<endl<<endl<<endl;
}
else{
cout<<"The smallest factor of "<<num<<" is equal to "<<factors<<endl;
cout<<"The largest factor of "<<num<<" is equal to "<<factorl<<endl;
cout<<"The number "<<num<<" is composite"<<endl<<endl<<endl;
}}
cout<<"Do you wish to continue?(y/n)"<<endl;
cin>>response;
}while (response=='y'||response=='Y');
return 0;
}

As for 14 digits, all datatypes have their specific range. for unsigned long int it is 0 to 4,294,967,295.
 
  • #3
The code you have posted, for factorization , when giving a number like 97984106041 it is not factoring. It is saying

"the smallest factor of 1970511412 is equal to 2."
and then it is giving its largest factor to the even number.

my code cannot factor 97984106041 because it has factors 257 and 381261113, here 1st factor has 3 decimal digits and other has 9 decimal digits.

but it can factor 27719336191973 = 854149 * 32452577 in just a blink of eye. It actually gives one factor and another is given number/this divisor.

I think you have accepted to post and the code is here.

Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include <time.h>
main()
{
      time_t start,end;
  char szInput [256];
  double dif;
      long long int a,b=0,x=0,y=0,z=0,c,c1,d,e,f,p,n1n2,l=0,p4,p7,p8,p9,disc;
      double t=2,n11,m[100],p6,p5=1,p11=1,x1,x2,p1,p2;
      double sqtr();
      
      FILE *filea1b_3 = fopen("filea1b_3.txt" , "w");//Start of a file named filea1b_3.txt
     
      printf("Enter the value of P : ");// Enter your P value to be factorized. 
                                       //And we must know that the no.of .dgits of two divisors of P must be approximately of equal values. 
                                       //This is our input 
      scanf("%I64u",&p);
      
      printf("So , Given P = %I64u . \n\n\n\n",p);
      fprintf(filea1b_3, "So , Given P = %I64u . \n",p);
      
      time (&start);     // start of the time to calculate time taken to get the answer
      fprintf(filea1b_3, "So , Given P = %I64u . \n",p);
      
      
      p1=sqrt(p);// this is not necessary , It is to check wether given P is a perfect square or not.
      p4=p1;
      p6=p1-p4;
      
      if(p6>0)// If not a perfect square
      {
      for(a=2;(p5&&p11)>0;a++)
      {
                      x++;
                      f=a*a;
                      for(b=1;b<f;b++)
                      {
                                          if((p-b)%a==0)
                                          {
                                                        y++;
                                                        n11=(((p+b)-(sqrt(4*p*b)))/(a*a));
                                                        // this is the equation that calculates the product of N1 and N2 values in
                                                        //  P = ((a*N) + b) = [ ((a*N1)+b1) * ((a*N2)+b2) ]
                                                        
                                                        // Here a , b are integer values taken to run the loops , and b = ( b1 *b2 )
                                                        p4=n11;
                         p5=n11-p4;
                         p6=n11-p5;
                         p7=p6;
                                                        for(c=1;c<=(b/2);c++)
                                                        {
                                                                         if(b%c==0)
                                                                         {
                                                                                        z++;
                                                                                        c1=(b/c);
                         // below is the roots of an equation f(N1) or f(N2). There is no condition one must come first
                         // because as from the equation of P, N1 and N2 are equally interchangeable variables
                         disc=((b+(a*a*p7)-p)*(b+(a*a*p7)-p))-(4*a*c*a*p7*(b/c));/*to find discriminant*/
if(disc>0)/*distinct roots*/
{
x1=(p-b-(a*a*p7)+sqrt(disc))/(2*a*(b/c));
x2=(p-b-(a*a*p7)-sqrt(disc))/(2*a*(b/c));
}
if(disc==0)/*Equal roots*/
{
x1=x2=(p-b-(a*a*p7))/(2*a*c);
}
if(disc<0)
{
x1=(p-b-(a*a*p7))/(2*a*c);/*complex roots*/
x2=sqrt(fabs(disc))/(2*a*c);
}
p4=x1;
                         p5=x1-p4;
                         p6=x1-p5;
                         p8=p6;
                         p4=x2;
                         p11=x2-p4;
                         p6=x2-p11;
                         p9=p6;
                         if((p5==0)||(p11==0)) // this is condition to check wether we got an answer or not 
                         {
                                         n1n2=x1*x2;
                                         d=x+y+z; // No .of. loops taken to get an answer .
      e=(p-b)/a;
      
      // This is the of output of our work 
      
      printf("\n\nNo.of.loops = %I64u\n",d);
      
      
      printf("a = %I64u, b= %I64u, N = %I64u, N1 = %f, N2 = %f, N1N2 = %I64u",a,b,e,x1,x2,n1n2);   
      printf("\n\nb1 = %d and b2 = %d (or) b1 = %d and b2 = %d\n\n",c,c1,c1,c); 
      // Here we will get values of the required values of
      // " a" , "two values of b1 " , " b2 = 0 " , and the integer value of either N1 or N2.
      // So If we got N1 as an Integer ,  Then one of the divisor is (aN1+b1) 
      // If we got N2 as an Integer ,  Then one of the divisor is (aN2+b1) 
      // Here b1 is anyone of the two values we got .
      // So another divisor is ( P / (aN1+b1) ) or (P / (aN2+b1))
      
      fprintf(filea1b_3, "\n\nNo.of.loops = %I64u\n",d);
      fprintf(filea1b_3, "a = %I64u, b= %I64u, N = %I64u, N1 = %f, N2 = %f, N1N2 = %I64u",a,b,e,x1,x2,n1n2);
      fprintf(filea1b_3, "\n\nb1 = %d and b2 = %d (or) b1 = %d and b2 = %d\n\n",c,c1,c1,c); 
      
      gets (szInput);
  time (&end);       // End of the loops and displays the time taken to complete the process
  dif = difftime (end,start);
  printf ("It took you %f seconds to execute the program.\n", dif );
  fprintf (filea1b_3, "It took you %f seconds to execute the program.\n", dif );
      getch();
                                                                                       exit(0); // If we got an answer exit the loops and
                                                                                                // display the answers.
                                                                                       }
                                                                                       }
                                                                                       }
      if((p5==0)||(p11==0))
                         {
                                                                                       exit(0);
                         // always (b1*b2) = b ( mod a ) 
                         // So here I have taken  b = b1*b2 = b+a-1                                                          }
                         b=b+a-1;
                         }
                         }
      }
      }
      }
      else 
      {
      printf(" Given number is a perfect square"); // If given P is a perfect square.
      printf(" P = %f^2 ",p1);
      fprintf(filea1b_3, " Given number is a perfect square");
      fprintf(filea1b_3, " P = %f^2 ",p1);
      }
      getch();
      }



As I said the two divisors should be closer to each other in terms of no.of.digits. This will give one factor and the other will be given number divided by the above obtained factor. It will not work on prime numbers.

here factor is (aN1+b1) if we got N1 as an integer . or (aN2+b1) if we got N2 as an integer.
Here b1 = anyone of the b1 values we got in the answer.

Any replies given will be helpful. thanks.
 
Last edited:
  • #4
1. You have declared a function named sqtr that takes no arguments and returns a double, and is never used.
2. The complete lack of indenting makes your code more difficult to follow than it ought to be.
3. Your variable names are almost all one or two letters, so it is difficult to understand what they're being used for.
4. There is not a single comment anywhere in this code.
5. This line of code might not be doing what you intended.
Code:
for(a=2;(p5&&p11)>0;a++)
The expression in the middle will be true if both p5 and p11 are negative.
 
  • #5
Mark44 said:
1. You have declared a function named sqtr that takes no arguments and returns a double, and is never used.
-- does it matter not using the declared double of sqrt.
Actually I used sqrt at two places.

2. The complete lack of indenting makes your code more difficult to follow than it ought to be.
I am poor in english. so I did not understand what u said. please write in low level english i.e with too simple words.

3. Your variable names are almost all one or two letters, so it is difficult to understand what they're being used for.
I am poor in coding a program also . Also I know only C language and also poor in it.

4. There is not a single comment anywhere in this code.
Is there anything wrong in comments ?

5. This line of code might not be doing what you intended.
Code:
for(a=2;(p5&&p11)>0;a++)
The expression in the middle will be true if both p5 and p11 are negative.
I knew that and Here p5 and p11 are always greater than zero or will be zero. So there is no problem in that code line
 
  • #6
Mark44 said:
1. You have declared a function named sqtr that takes no arguments and returns a double, and is never used.
2. The complete lack of indenting makes your code more difficult to follow than it ought to be.
3. Your variable names are almost all one or two letters, so it is difficult to understand what they're being used for.
4. There is not a single comment anywhere in this code.
5. This line of code might not be doing what you intended.
Code:
for(a=2;(p5&&p11)>0;a++)
The expression in the middle will be true if both p5 and p11 are negative.

smslca said:
-- does it matter not using the declared double of sqrt.
Actually I used sqrt at two places.
For 1, you declared sqtr but used sqrt instead. If you have a variable or function that isn't used, it shouldn't be in your program. sqtr was undoubtedly a typo.
smslca said:
I am poor in english. so I did not understand what u said. please write in low level english i.e with too simple words.
For 2, when I first say your code, all of the lines of code started at the left margin, but now I see that it is indented.
smslca said:
I am poor in coding a program also . Also I know only C language and also poor in it.
For 3 - OK. What I'm saying is that it's usually better to have variable names that suggest what they are being used for. Names like a, b, c, p4, p7, etc. don't help a reader understand what they are being used for.
smslca said:
Is there anything wrong in comments ?
For 4, there aren't any comments at all. By adding comments you can make it easier for someone reading your code to understand what you are doing.
smslca said:
I knew that and Here p5 and p11 are always greater than zero or will be zero. So there is no problem in that code line

OK, but most people would code it like this:
Code:
for(a=2; (p5 > 0) && (p11 > 0); a++)

Having said all this, I think what you have done is an interesting approach to factoring numbers.
 
  • #7
smslca said:
(p5&&p11)>0

I knew that and Here p5 and p11 are always greater than zero or will be zero. So there is no problem in that code line
The expression "p5 && p11" will be equal to:

1 if p5 and p11 are both nonzero.
0 if one of p5 or p11 is zero.

The expression "(x)>0" will be equal to:

0 if x is equal to 0.
1 if x is greater than 0.

So assuming you are COMPLETELY certain p5, p11 will never be negative (in which case why not make them unsigned?) the >0 is redundant. You could just say (p5&&p11) by itself.

---

What EXACTLY is the error message GMP is giving you? And could you post the GMP version of your code which did not work?
 
  • #8
Mark44 said:
For 4, there aren't any comments at all. By adding comments you can make it easier for someone reading your code to understand what you are doing.
Hey pal I have reposted the code with comments. check it out.

Thanks for giving a satisfactory reply.
 
  • #9
Coin said:
The expression "p5 && p11" will be equal to:

1 if p5 and p11 are both nonzero.
0 if one of p5 or p11 is zero.

The expression "(x)>0" will be equal to:

0 if x is equal to 0.
1 if x is greater than 0.

So assuming you are COMPLETELY certain p5, p11 will never be negative (in which case why not make them unsigned?) the >0 is redundant. You could just say (p5&&p11) by itself.

---

What EXACTLY is the error message GMP is giving you? And could you post the GMP version of your code which did not work?

gmp code without the comments. I have written it with toooooo many variables, and also not used these variables not in an order. I have used only some of the declared variables. I have very poor knowledge in coding , I only know how to code using simple variables. I don't know coding in a advanced level.
I am not giving any comments to this gmp code. But one thing I can say is Even though I seem too lengthy, It is just I have split each equation into addition,subtraction,multiplication and division.
It is not executing , because I think there is a problem in printf and scanf parts of gmp.

Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include <time.h>
#include<gmp.h>
int main ()
{
      time_t start,end;
  char szInput [256];
  double dif;
      mpz_t a,a1,a2,a3,a4,a5,a6,a7,a8,a9,l,q,r,r1,r2,r3,r4,r5;
       int x=0,y=0,z=0,d=0;
       mpz_t b,b1,b2,b3,k,k1,k2,b4,b5,b6,b7,b8,b9,b0;
       mpz_set_ui(k,4);
       mpz_t c,c2,c3,c4,c5,c6,c7,c8,c9,q1,q2;
       mpz_t c1;mpz_t e;mpz_t f;mpz_t g;mpz_t p;mpz_t n1n2;mpz_init(l);mpz_t p4,p6;mpz_t p7;mpz_t p8;mpz_t p9;mpz_t disc;
      mpz_t t,n11,m[100],x1,x2,p1,p2,p5,p11;
      mpz_set_ui(p5,1);
      mpz_set_ui(p11,1);
      mpz_set_ui(t,2);  
      mpz_set_ui(k1,2);
      mpz_set_ui(k2,1);
      

      gmp_printf( "Enter the value of P : ");
      gmp_scanf("%d",p);
      gmp_printf( "So , Given P = %d . \n\n\n\n",p);
      time (&start);
      mpz_sqrtrem(p4,p6,p);
      if(p6>0)
      {
      for(mpz_set_ui(a,2);mpz_cmp(p5,0)>0 , mpz_cmp(p11,0)>0;mpz_add_ui(a,a,1))
      {
                      x++;
                      mpz_mul(f,a,a);
                      for(mpz_set_ui(b,1);mpz_cmp(f,b)>0;mpz_add_ui(b,b,1))
                      {
                                         mpz_sub(b1,p,b);
                                         mpz_tdiv_r(r,b1,a);
                                          if(r==0)
                                          {
                                                        y++;
                                                        mpz_mul(b2,p,k);
                                                        mpz_mul(b3,b2,b);
                                                        mpz_sqrtrem(b4,b5,p);
                                                        mpz_add(a1,p,b);
                                                        mpz_sub(a2,a1,b4);
                                                        mpz_tdiv_q(n11,a2,f);
                                                        mpz_tdiv_q(b5,b,k1);
                                                        for(mpz_set_ui(c,1);mpz_cmp(c,b5)<=0;mpz_add_ui(c,c,1))
                                                        {
                                                        mpz_tdiv_qr(c1,b6,b,c);
                                                                       if(b6==0)
                                                                         {
                                                                                        z++;
                      mpz_mul(c2,f,n11);
                         mpz_add(c3,b,c2);
                         mpz_sub(c4,c3,p);
                         mpz_mul(c5,c4,c4);
                         mpz_mul(c6,k,c1);
                         mpz_mul(c7,c2,c6);
                         mpz_sub(disc,c5,c7);
                          mpz_sqrtrem(c8,c9,disc);
                     mpz_sub(a3,p,c3);
                     mpz_add(a4,a3,c8);
                     mpz_sub(a5,a3,c8);
                     mpz_mul(a6,k1,a);
                     mpz_mul(a7,a6,c1);
                     mpz_mul(a8,a6,c);

if(disc>0)/*distinct roots*/
{
                     mpz_tdiv_qr(q1,p5,a4,a7);
                     mpz_tdiv_qr(q2,p11,a5,a7);
}
if(disc==0)/*Equal roots*/
{
                     mpz_tdiv_qr(q1,p5,a4,a8);
                     mpz_tdiv_qr(q2,p11,a3,a8);
}
if(disc<0)
{
                     mpz_tdiv_qr(q1,p5,a4,a8);
                     mpz_tdiv_qr(q2,p11,a3,a8);

//x1=(p-b-(a*a*p7))/(2*a*c);/*complex roots*////
//x2=sqrt(fabs(disc))/(2*a*c);//
}
                         if((p5==0)||(p11==0))
                         {
                                              mpz_mul(n1n2,q1,q2);
                                         d=x+y+z;
                                         mpz_sub(a9,p,b);
                                         mpz_tdiv_q(e,a9,a);
      gmp_printf( "\n\nNo.of.loops = %d\n",d);
      gmp_printf( "a = %d, b= %Zd, N = %d, N1 = %d, N2 = %d, N1N2 = %d",a,b,e,q1,q2,n1n2);
      gmp_printf( "\n\nb1 = %d and b2 = %d (or) b1 = %d and b2 = %d\n\n",c,c1,c1,c); 

      gets (szInput);
  time (&end);
  dif = difftime (end,start);
  gmp_printf ("It took you %f seconds to execute the program.\n", dif );
  
      getch();
                                                                                       exit(0);
                                                                                       }
                                                                                       }
                                                                                       }
      if((p5==0)||(p11==0))
                         {
                                                                                       exit(0);
                                                                                       }
                         mpz_sub(c9,a,k2);
                         mpz_add(b,b,c9);
                         }
                         }
      }
      }
      else 
      {
      gmp_printf( " Given number is a perfect square");
      gmp_printf( " P = %f^2 ",p1);
      
      }
      getch();
      }
 
  • #10
Why are you using gmp_printf and gmp_scanf? Why not use printf and scanf?

I've never heard of gmp_printf or gmp_scanf, but I suspect that you are using gmp_scanf incorrectly in this line and possibly others.
Code:
mpz_t p;
gmp_scanf("%d",p);

There are probably two things wrong with this code.
1. The second argument to gmp_scanf should probably be an address; e.g., &p.
2. The %d format specifier in the control string in gmp_scanf might not agree with the type of p (which is mpz_t - I don't know what type that is). The format specifier in scanf has to agree with the type of variable you are inputting to, or you will definitely have problems.
 
  • #11
Taking a cursory look at the docs for gmp_scanf

http://www.gnu.org/software/gmp/manual/html_node/Formatted-Input-Strings.html

It appears Mark44 is dead on, %d is for normal ints and behaves just as it does in normal scanf, and there are special % codes for the GNU mp types.

It looks like what you want to do is

gmp_scanf("%Z", &p);
 
Last edited by a moderator:
  • #12
A pal named rpenner in sci forum, had rewritten the code, may be now you can understand what I intended to do in the code with his code.
Code:
#include <stdio.h>
#ifdef windows
#include <conio.h>
#endif
#include <math.h>
#ifdef windows
#include <process.h>
#endif
#include <time.h>
#ifndef windows
#include <stdlib.h>
#endif

#define INPUT_BUFFER_SIZE 256

#ifndef windows
int
#endif
main()
{
  time_t start_time;
  char ignored_input_buffer [INPUT_BUFFER_SIZE];
  long long int input_number;
  long long int trial_square;
  long long int floor_misc, floor_n11;
  double trial_diff, trial_square_root;
  double left_difference=1, right_difference=1;
  double n11;

  FILE *output_fp = fopen("filea1b_3.txt" , "w"); //Start of a file named filea1b_3.txt

  printf("Enter the value of P : "); // Enter your P value to be factorized. 
  //And we must know that the no.of .dgits of two divisors of P must be approximately of equal values. 
  //This is our input 
  scanf("%I64lld", &input_number);

  printf("So , Given P = %I64lld . \n\n\n\n", input_number);
// warning: ISO C does not support the 'I' scanf flag
// warning: ISO C does not support the 'I' printf flag

  fprintf(output_fp, "So , Given P = %I64lld . \n", input_number);

  time (&start_time);     // start of the time to calculate time taken to get the answer
  fprintf(output_fp, "So , Given P = %I64lld . \n", input_number);


  // This dependence on doubles and squareroot breaks for
  // 9223372030926249000 which is not a perfect square.
  trial_square_root=sqrt((double)input_number); // this is not necessary , It is to check wether given P is a perfect square or not.
  trial_square = trial_square_root;
  trial_square *= trial_square; // Bug -- this line was missing
  trial_diff=input_number-trial_square;

  if(trial_diff>0)// If not a perfect square
  {
    long long int trial_center;
    long long int loop_counter_one = 0;
    long long int loop_counter_two = 0;
    long long int loop_counter_three = 0;
    for(trial_center = 2; left_difference && right_difference; trial_center ++)
    {
      long long int ballast;
      loop_counter_one ++;
      trial_square = trial_center * trial_center;
      for(ballast = 1; ballast<trial_square; ballast ++)
      {
        if((input_number-ballast)%trial_center == 0)
        {
          long long int ballast_left; // One factor of ballast
          loop_counter_two++;
          n11 = (((input_number + ballast)-(sqrt(4.0 * input_number * ballast)))/(trial_square));
          // this is the equation that calculates the product of N1 and N2 values in
          //  P = ((trial_center*N) + ballast) = [ ((trial_center*N1)+b1) * ((trial_center*N2)+b2) ]

          // Here trial_center , ballast are integer values taken to run the loops , and ballast = ( b1 *b2 )

          floor_misc = n11;
          left_difference=n11-floor_misc;
          floor_n11=n11-left_difference;

          for(ballast_left=1; ballast_left<=(ballast/2); ballast_left++)
          {
            if(ballast%ballast_left==0)
            {
              long long int ballast_right=(ballast/ballast_left); // Other factor
              long long int discriminant;
              long long int floor_x1, floor_x2;
              double x1, x2;
              loop_counter_three++;
              // below is the roots of an equation f(N1) or f(N2). There is no condition one must come first
              // because as from the equation of P, N1 and N2 are equally interchangeable variables
              discriminant=((ballast+(trial_square*floor_n11)-input_number)*(ballast+(trial_square*floor_n11)-input_number))-(4*trial_square*ballast_left*floor_n11*ballast_right); /*to find discriminant*/
              if(discriminant>0)/*distinct roots*/
              {
                x1=(input_number-ballast-(trial_square*floor_n11)+sqrt((double)discriminant))/(2*trial_center*ballast_right);
                x2=(input_number-ballast-(trial_square*floor_n11)-sqrt((double)discriminant))/(2*trial_center*ballast_right);
              }
              else if(discriminant==0)/*Equal roots*/
              {
                x1=x2=(input_number-ballast-(trial_square*floor_n11))/(2*trial_center*ballast_left);
              }
              else /* if(discriminant<0) */
              {
                x1=(input_number-ballast-(trial_square*floor_n11))/(2*trial_center*ballast_left); /*complex roots*/
                x2=sqrt(fabs((double)discriminant))/(2*trial_center*ballast_left);
              }

              floor_misc=x1;
              left_difference=x1-floor_misc;
              floor_x1=x1-left_difference;

              floor_misc=x2;
              right_difference=x2-floor_misc;
              floor_x2=x2-right_difference;

              if((left_difference==0)||(right_difference==0)) // this is condition to check wether we got an answer or not 
              {
                long long int n1n2 = x1*x2;
                // No .of. loops taken to get an answer .
                long long int total_loop_count = loop_counter_one
                  + loop_counter_two
                  + loop_counter_three;
                time_t end_time;
                double elapsed_time_seconds;
                long long int n_for_output =(input_number-ballast)/trial_center;

                // This is the of output of our work 

                printf("\n\nNo.of.loops = %I64lld\n", total_loop_count);


                printf("trial_center = %I64lld, ballast= %I64lld, N = %I64lld, N1 = %f, N2 = %f, N1N2 = %I64lld\n", trial_center, ballast, n_for_output, x1, x2, n1n2);   
                printf("\n\nb1 = %I64lld and b2 = %I64lld (or) b1 = %I64lld and b2 = %I64lld\n\n", ballast_left, ballast_right, ballast_right, ballast_left); 
                // Here we will get values of the required values of
                // " trial_center" , "two values of b1 " , " b2 = 0 " , and the integer value of either N1 or N2.
                // So If we got N1 as an Integer ,  Then one of the divisor is (trial_center N1+b1) 
                // If we got N2 as an Integer ,  Then one of the divisor is (trial_center N2+b1) 
                // Here b1 is anyone of the two values we got .
                // So another divisor is ( P / (trial_center N1+b1) ) or (P / (trial_center N2+b1))

                fprintf(output_fp, "\n\nNo.of.loops = %I64lld\n", total_loop_count);
                fprintf(output_fp, "trial_center = %I64lld, ballast= %I64lld, N = %I64lld, N1 = %f, N2 = %f, N1N2 = %I64lld\n", trial_center, ballast, n_for_output, x1, x2, n1n2);
                fprintf(output_fp, "\n\nb1 = %I64lld and b2 = %I64lld (or) b1 = %I64lld and b2 = %I64lld\n\n", ballast_left, ballast_right, ballast_right, ballast_left); 

                fgets (ignored_input_buffer, INPUT_BUFFER_SIZE, stdin);
                time (&end_time);       // End of the loops and displays the time taken to complete the process
                elapsed_time_seconds = difftime (end_time, start_time);
                printf ("It took you %f seconds to execute the program.\n", elapsed_time_seconds );
                fprintf (output_fp, "It took you %f seconds to execute the program.\n", elapsed_time_seconds );
#ifdef windows
                getch();
#endif
                exit(0); // If we got an answer exit the loops and
                // display the answers.
              }
            }
          }
          if((left_difference==0)||(right_difference==0))
          {
            exit(0);
            // always (b1*b2) = ballast ( mod trial_center ) 
            // So here I have taken  ballast = b1*b2 = ballast+trial_center-1
            ballast=ballast+trial_center-1;
          }
        }
      }
    }
  }
  else 
  {
    printf(" Given number is a perfect square\n"); // If given P is a perfect square.
    printf(" P = %f^2 \n", trial_square_root);
    fprintf(output_fp, " Given number is a perfect square\n");
    fprintf(output_fp, " P = %f^2\n", trial_square_root);
  }
#ifdef windows
  getch();
#endif
  exit(0);
#ifndef windows
  return 0;
#endif
getch();
}

Here trial_center = a ; ballast = b; and all others are same.
 
  • #13
Coin said:
Taking a cursory look at the docs for gmp_scanf

http://www.gnu.org/software/gmp/manual/html_node/Formatted-Input-Strings.html

It appears Mark44 is dead on, %d is for normal ints and behaves just as it does in normal scanf, and there are special % codes for the GNU mp types.

It looks like what you want to do is

gmp_scanf("%Z", &p);

Here I tried changing %d anywhere in the code to %Zd (i.e at gmp_print and gmp_scanf);
But it is still showing " Project4.exe has stopped working" , when I tried to run the code.
It is compiling perfectly when I hit the "compile" option.

I have tried this scanf , printf, gmp_pf/sf, %d, %Zd in a factorial ways (i.e I did every arrangement I can do with those six terms). I am still getting the same error . When hit run option, It is opening up a command window, and after two or four seconds , It is showing up an another window displaying " Project4.exe has stopped working". This project4 is where I have created this gmp code file, to link with libgmp.a.

In my guess , Is there any thing wrong in time function I have written in the code ?
 
Last edited by a moderator:
  • #14
can anyone solve these problems ...

The whole problem in not getting an answer to the high values of input lurks in sqrt(x) function.

Since in square root function there is an integer limit for is x =3,899,518,527,799,954 which is a 15 digit value
In our actual equation we have the term sqrt(4.0 *input_number * b)

So 4*input_number*b = 3,899,518,527,799,954
b value ranges from 1 to any number. and 4 is a constant.

At b=1 max input we can give is ((3,899,518,527,799,954-2)/4) = 974,879,631,949,988
But actual number we can use from is 974,879,631,949,983

As we know minimum input can be given is 1.

In between these max and min values of input , the input we can give is dependent on the value of b. and b < a^2.

So in the equation n11 = (((p+b)-(sqrt(4*p*b)))/(a*a));
Where P is the input_number , and a , b values are selected by the computer.

n11 is always restricted to a maimum of 15 decimal digit value , even though double n11 has a 20 decimal digit range
----------------------------------------------------------

we can solve this restriction of n11 value at some level by writing sqrt(4.0 *input_number * b) = 2*sqrt(input_number)*sqrt(b)

Now we can give input_number and b values upto 3,899,518,527,799,954 ., But the limitation of n11 here is caused by the
20 digit limit value of unsigned long long int , which mean n11 value could not have no.of digits more than 20.

so for 2*sqrt(input_number)*sqrt(b) there will be input_number and b values which makes the digits of n11 will not be
not greater than 20.because of equation n11 = (((p+b)-(sqrt(4*p*b)))/(a*a));

--------------------------------------------------------

So this code needs a square root function that does not limit lower than the limit of prescribed data type,
And also the data type which do not have the limit to the input given.

Problem here is even though we have a data type (i.e using GMP) which does not restrict the input value , due to the limitation of the
sqrt(x) , x=3,899,518,527,799,954 ,there will be no use without changing two of them (input and sqrt).
 

Related to Requesting permission to post the code

1. Can I use someone else's code without their permission?

No, it is important to always request permission before using someone else's code. This ensures that you are not infringing on their intellectual property rights and also allows for proper credit and recognition to be given.

2. How do I request permission to post someone else's code?

The best way to request permission is to reach out directly to the owner or creator of the code. This can be done through email or through a messaging platform. Be sure to clearly state your intentions and how you plan to use the code.

3. Do I need to credit the original creator when posting their code?

Yes, it is important to give proper credit to the original creator when using their code. This can be done through comments within the code or by including a citation or reference to their work.

4. Can I modify the code after obtaining permission?

It depends on the terms of the permission granted by the original creator. Some may allow for modifications while others may not. It is important to clarify this with the owner before making any changes to their code.

5. What should I do if I don't hear back from the code's owner after requesting permission?

If you do not hear back from the owner after a reasonable amount of time, it is best to assume that permission has not been granted. It is important to respect the owner's decision and not use their code without their permission.

Similar threads

  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
Replies
17
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
34
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
Back
Top