Thread: prime number finder.

  1. #1
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48

    prime number finder.

    well the other day i noticed that the diferance between 2 perfect squares is a prime number, so i made a program that does the math for you.


    Code:
    #include<iostream>
    using namespace std;
    int counter = 1;
    int main()
    {
    int num1;
    
      do
      {
    
        cout << "enter a number: ";
        cin >> num1;
        int num2 =  num1 + 1;
        int square1 = num1 * num1;
        int square2 = num2 * num2;
        int prime = square2 - square1;
        cout << "your prime number is: " << prime << endl;
          
      }
      while (counter = 1);
    
      cin.get();
      return 0;
    }
    yeah i couldnt get a "yes/no would you like to do it again" thing to work, so i just made it loop like that.

    also, dont try to enter a letter, it spazzes out.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    5^2 = 25
    4^2 = 16

    25 - 16 = 9, which is not a prime.

    I regret to inform you that your theorem doesn't hold, but I genuinely applaud your interest in number theory. I suggest getting a book on the subject or taking a course in it. It's good stuff.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    damnit, i just came to inform you of that, heh.

    i've been testing it all morning and just decided to work with the small numbers, damn small numbers make me feal stupid lol

    i tested up to getting the answer 65. here are my results. number i put in)number i got(numbers its devisible by
    4)9(3
    7)15(3,5
    10)21(7
    12)25(5
    17)35(5,7
    22)45(5,9-15,3
    24)49(7
    27)55(5,11
    32)65(5,13

    it seems that its almost true, except for being devisible by the prime numbers themselves, and - in the of 22, a number previouly gotten.

    i still have work. thanks for wasting your time to read that
    Last edited by Aalmaron; 03-05-2004 at 05:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  3. prime number.
    By tdoctasuess in forum C Programming
    Replies: 13
    Last Post: 05-13-2004, 08:03 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. Replies: 3
    Last Post: 01-14-2003, 10:34 PM