473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Find prime numbers from a given number

Hi, can someone send me the algorithm for finding the products of prime numbers from a given number?

Thank You

Gary.
Oct 24 '06 #1
11 46399
arne
315 Expert 100+
Hi, can someone send me the algorithm for finding the products of prime numbers from a given number?

Thank You

Gary.
One possible algorithm would work like this:

Expand|Select|Wrap|Line Numbers
  1. start with d = 2 and num = given number
  2. repeat
  3.    check if your num is divisible by d
  4.    if yes
  5.         print out d and divide num by d
  6.    if no
  7.         increase d by one
  8. while ( d is smaller than half the given number and num is unequal 1 ) 
  9.  
Oct 24 '06 #2
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. start with d = 2 and num = given number
  2. repeat
  3.    check if your num is divisible by d
  4.    if yes
  5.         print out d and divide num by d
  6.    if no
  7.         increase d by one
  8. while ( d is smaller than half the given number and num is unequal 1 ) 
  9.  
This routine could be optomised to in the following ways to run faster

1. Take account of 2 being the only even prime number, don't bother checking 4, 6, 8 etc.

2. Change while condition to

while ( d is smaller than the square root of the given number and num is unequal 1 )

If the stop condition is (d is greater than the square root of the given number) rather than (num is equal 1) the num will be unequal 1 and will be a prime divisor of the given num. (Try with the input 15)

3. This algorithm tests all numbers, if the given number is very large and has at least 1 large prime divisor then it will be in efficient because it will be testing lots of non-prime numbers that can not possible be prime divisors of the given number.



I wonder but don't know if this may be more efficient

Expand|Select|Wrap|Line Numbers
  1. FUNCTION GetPrimeDivisors( Number )
  2.  
  3.   Test = The Square Root Of Number
  4.  
  5.   WHILE(Test Not Equal To 1)
  6.     CHECK if your number is divisible by Test
  7.  
  8.     IF yes 
  9.       end loop
  10.     ELSE IF no
  11.       test = test - 1
  12.     END IF
  13.   END WHILE
  14.  
  15.   IF Test Equal To 1
  16.     Print Number
  17.   ELSE IF Test Not Equal To 1
  18.     CALL GetPrimeDivisors( Test )
  19.     CALL GetPrimeDivisors( Number/Test )
  20.   END IF
  21. END FUNCTION
  22.  
Hmm think I will just make sure this works
Oct 24 '06 #3
Banfa
9,065 Expert Mod 8TB
Well it does work but I still have no idea how efficient it is or isn't
Oct 24 '06 #4
check a prime number program written in c language ............
<code snipped>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
hi My name is mukesh, i am red tech web devloper, well red tech is a group of IT engineers, For any query related to programming write at us : <email snipped> or <email snipped>. our web site providing a large collection of programs but write now its under construction. plz keep visiting this site.
<web address snipped>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Apr 30 '07 #5
Savage
1,764 Expert 1GB
Hi, mukesh would u please read posting guidelines and especialy the part How to Respond to a Question.


Savage
Apr 30 '07 #6
Savage
1,764 Expert 1GB
Well it does work but I still have no idea how efficient it is or isn't
I do think this will be even more efficient:

Expand|Select|Wrap|Line Numbers
  1. for(first counter starts with num;first counter>=3,which is first prime;firstcounte--)
  2. {
  3.     //Here we need some type of pointer if it's 0 then number is a prime e.g 
  4.       test=0
  5.  
  6.       for(sec counter=2;sec counter<first counter;sec counter++) if moduo of 
  7.       first counter and secound counter is 0, test=1;
  8.  
  9.       if test is 0 output the number;
  10.  
  11. }
Savage
Apr 30 '07 #7
Ganon11
3,652 Expert 2GB
I wonder but don't know if this may be more efficient

Expand|Select|Wrap|Line Numbers
  1. FUNCTION GetPrimeDivisors( Number )
  2.  
  3.   Test = The Square Root Of Number
  4.  
  5.   WHILE(Test Not Equal To 1)
  6.     CHECK if your number is divisible by Test
  7.  
  8.     IF yes 
  9.       end loop
  10.     ELSE IF no
  11.       test = test - 1
  12.     END IF
  13.   END WHILE
  14.  
  15.   IF Test Equal To 1
  16.     Print Number
  17.   ELSE IF Test Not Equal To 1
  18.     CALL GetPrimeDivisors( Test )
  19.     CALL GetPrimeDivisors( Number/Test )
  20.   END IF
  21. END FUNCTION
  22.  
Erm...does it work?

Test starts as the square root of a number, and then you test that Test != 1. The only change you are making to Test is Test -= 1. (actually, you write test = test - 1...same thing functionally). Suppose Number is not a perfect square, like 15. Then the square root of Number is some decimal (here, between 3 and 4), and will never equal 1.

Unless you're depending on the square root operation to truncate Test to an integer...
Apr 30 '07 #8
JosAH
11,448 Expert 8TB
Except for 2 and 3 you only have to test for multiples of 6 plus or minus 1.
Until a potential divisor squared is larger than the number to be tested.

kind regards,

Jos
Apr 30 '07 #9
Banfa
9,065 Expert Mod 8TB
Unless you're depending on the square root operation to truncate Test to an integer...
OK just wait 6 months and _then_ pick at my pseudo code :p

Actually since divisor have to be integers I was assuming integer arithmatic in the routine but I should have stated it.
Apr 30 '07 #10
Banfa
9,065 Expert Mod 8TB
Except for 2 and 3 you only have to test for multiples of 6 plus or minus 1.
Until a potential divisor squared is larger than the number to be tested.
Or alternitively put you can skip every 3rd odd number because it is bound to be divisible by 3, and all even numbers because they are divisible by 2.

The question is can you code that efficiently (to which actually I think the answer is probably yes if you code it in the terms you have already given).
Apr 30 '07 #11
JosAH
11,448 Expert 8TB
Or alternitively put you can skip every 3rd odd number because it is bound to be divisible by 3, and all even numbers because they are divisible by 2.

The question is can you code that efficiently (to which actually I think the answer is probably yes if you code it in the terms you have already given).
Yup, except for 2 and 3 you can do something like this:
Expand|Select|Wrap|Line Numbers
  1. for (int d= 5, inc= 2; d*d <= n; )
  2.    if (n%d == 0) // d is a factor
  3.       n/= d; // if you want to find all factors;
  4.    else {
  5.       d+= inc; // next possible divisor
  6.       inc= 6-inc; // next increment
  7.    }
kind regards,

Jos
Apr 30 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
9
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
4
by: Caffiend | last post by:
Thanks to everyone who helped me out with my early version of the prime number calculator... Here is the complete and updated version including unsigned 64 bit integers...mmmmmm....... Lots of fun...
7
by: Caffiend | last post by:
Well, I've been picking at learning python, got tired of reading, and figured I'd try to replicate my prime number generator I wrote (with much TSDN forum help) in C++. I've hit a stumbling block......
4
by: cnixuser | last post by:
Hello, I am attempting to create a prime number detector that will identify all of the prime numbers within a specified range of numbers. The problem is, for some reason my program is not detecting...
3
by: ace2606 | last post by:
Hi I am a beginner in C++ and would like someone to give me an idea or push me in the right direction of how to solve this question , It is related to classes Design and implement a class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.