8 Replies - 364 Views - Last Post: 20 February 2013 - 06:35 AM Rate Topic: -----

#1 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

How to display numbers that input number is divisible with?

Posted 17 February 2013 - 12:37 PM

Hello, for the first time I have a concrete question about my program.I need to write a program which after inputting a number will provide the answer whether it is prime number and if it isnt, the numbers that the input number is divisible with.
Example:
4 is divisible with 1,2 and 4.
My code so far:
#include<iostream>
#include<conio.h>  
using namespace std;
int main()  
       {
int P,count=0;
cout<<"Sisestage arv:\n"; /*Asks for user input*/
cin>>P;                    /* User input P*/
for(int a=1;a<=P;a++)
{
if(P%a==0)
{
count++;
}
if(count==2)
      {
     cout<<"Algarv.\n";  /* Provided option when the number is prime number*/
      }
    else
     {
       cout<<" Ei ole algarv \n"; /* This is where I am supposed to provide the numbers input is divisible with*/
     }
getch();
} 
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to display numbers that input number is divisible with?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3097
  • View blog
  • Posts: 10,883
  • Joined: 08-August 08

Re: How to display numbers that input number is divisible with?

Posted 17 February 2013 - 01:52 PM

if(P%a==0) and a is not equal to 1 or P then it's not prime. Why are you increasing count?
Was This Post Helpful? 0
  • +
  • -

#3 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

Re: How to display numbers that input number is divisible with?

Posted 17 February 2013 - 02:15 PM

View PostCTphpnwb, on 17 February 2013 - 01:52 PM, said:

if(P%a==0) and a is not equal to 1 or P then it's not prime. Why are you increasing count?

You need it for it to be mathematically correct,try to run it without count.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3097
  • View blog
  • Posts: 10,883
  • Joined: 08-August 08

Re: How to display numbers that input number is divisible with?

Posted 17 February 2013 - 02:21 PM

Huh? What do you mean by mathematically correct?

A brute force method of determining if a number is prime is to check if it is devisable by any number smaller than it but greater than 1.
Was This Post Helpful? 0
  • +
  • -

#5 needhelpest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 12-January 13

Re: How to display numbers that input number is divisible with?

Posted 17 February 2013 - 04:21 PM

View PostCTphpnwb, on 17 February 2013 - 02:21 PM, said:

Huh? What do you mean by mathematically correct?

A brute force method of determining if a number is prime is to check if it is devisable by any number smaller than it but greater than 1.

Could you provide an example code to me , because when I tried it, it just made the program to give me out prime number result only.Also, this has not been connected to the question in the title in anyway to my understanding.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3097
  • View blog
  • Posts: 10,883
  • Joined: 08-August 08

Re: How to display numbers that input number is divisible with?

Posted 17 February 2013 - 04:37 PM

Here's an example:
15 % 2 == 1, so it could be prime. Keep going.
15 % 3 == 0, so it cannot b prime. Output 3 and 15/3 as numbers it is divisible by and stop.

and another:

7 % 2 == 1, so it could be prime. Keep going.
7 % 3 == 1, so it could be prime. Keep going.
7 % 4 == 3, so it could be prime. Keep going.
7 % 5 == 2, so it could be prime. Keep going.
7 % 6 == 1, but there are no more numbers less than 7, so it must be prime.
Was This Post Helpful? 0
  • +
  • -

#7 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: How to display numbers that input number is divisible with?

Posted 18 February 2013 - 09:28 PM

Write a function which will get the factors of the input number and print them. If there are no factors the number is prime... so print it.
Was This Post Helpful? 0
  • +
  • -

#8 adgjlsfhk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 08-February 13

Re: How to display numbers that input number is divisible with?

Posted 19 February 2013 - 09:35 PM

Something to double the speed of your program, no number has a factor greater than half of it's value. Therefore, you can cut off your if loop much earlier. (esp for large numbers)
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,461
  • Joined: 05-May 12

Re: How to display numbers that input number is divisible with?

Posted 20 February 2013 - 06:35 AM

If you are only looking for the prime factors, then "square root of the value" is sufficient.

If you are looking for all the factors, then "half the value" is the correct limit.

Remember, though to always list the number itself, otherwise you'll be missing a factor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1