Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,267 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,487 people online right now. Registration is fast and FREE... Join Now!




Help! Prime Number

 
Reply to this topicStart new topic

Help! Prime Number

kavi
3 Nov, 2006 - 10:27 AM
Post #1

New D.I.C Head
*

Joined: 3 Nov, 2006
Posts: 2


My Contributions
I need help!!

I Can not invent the algorithm of this programm. Can someone help me? smile.gif

input number "n" and the programm shows all prime number who is lesser than number "n" and whom can enounce like 2^k -1

This post has been edited by kavi: 3 Nov, 2006 - 10:38 AM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Help! Prime Number
3 Nov, 2006 - 10:37 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Post the code you have completed so far and someone will be happy to help you.
User is offlineProfile CardPM
+Quote Post

kavi
RE: Help! Prime Number
3 Nov, 2006 - 10:47 AM
Post #3

New D.I.C Head
*

Joined: 3 Nov, 2006
Posts: 2


My Contributions
CODE
#include <iostream>
int x (int n,int m); //deklaree visus mainiigos
using namespace std;

int main()
{
    int n, m=0;
    char exit;
    do
    {
         cout << "\nInput number, \nthe programm will show \nall prime number who is \nlesserthan your input number \nand which can enounce like 2^k -1 ";
         cin  >> n;
         cout << "\nYour input number is : "  << n << endl;
        
         ................................. <<< I can`t  invent what be writen in here
        
         cout << "\nPrime number wich is lesser than your nummber and \nwich enounce like 2^k -1 is  : "  << m << endl;
         cout << "\nRepeat the programm <y/n> " << endl;
         cin  >> exit;
    }
while (exit=='y');

return 0;
}

User is offlineProfile CardPM
+Quote Post

Dark_Nexus
RE: Help! Prime Number
3 Nov, 2006 - 02:13 PM
Post #4

or something bad...real bad.
Group Icon

Joined: 2 May, 2004
Posts: 1,309



Thanked: 3 times
Dream Kudos: 625
My Contributions
a prime number is a number which is divisible only by itself and one

thus the following algorithm, should verify whether a number is prime or not

CODE


bool isPrime(int num)
{
     for (int i = 2;i < num;++i)
     {
          if (num % i == 0)
               return false;
     }

     return true;
}

User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Help! Prime Number
4 Nov, 2006 - 03:17 AM
Post #5

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE
a prime number is a number which is divisible only by itself and one

or you can write the function using the exact definition that Dark_Nexus gave:
CODE

int isprime(int x)
{
   int i=2;
   while(x%i)i++;

   return (x==i);
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:31PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month