473,385 Members | 1,780 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,385 software developers and data experts.

Printing Prime numbers

11
Hello.
I need a simple programme to print prime numbers from 1 to 100.
I need it urgently.
Aug 28 '06 #1
5 15847
D_C
293 100+
Well, if it's simple, I guess you had better stay away from Quadratic Field Sieve or some of the faster methods.

First test if the number is even, or divisible by two. Second, for all odd numbers from 3 to floor(square root(n)), n being tested for primality, see if it is divisible. For those, use the modulo operation, '%'. It returns the remainder. If (a % b) = 0, then b divides a, a is divisible by b.

Examples:
11 % 5 = 4 ( 11 = 2*5 + 1)
11 % 3 = 2 ( 11 = 3*3 + 2)

Is 67 prime? sqrt(67) = 8.xx, floor(sqrt(67)) = 8. Test 2, 3, 5, 7
67 % 2 = 1
67 % 3 = 1
67 % 5 = 2
67 % 7 = 4

Therefore, 67 is prime since no possible options divide it. Of course, for a larger number, testing whether it's divisble by 9 may be redundant, but it's faster, I think just to check it, rather than check that 9 is prime (recursion, ugly).
Aug 29 '06 #2
Well, if it's simple, I guess you had better stay away from Quadratic Field Sieve or some of the faster methods.

First test if the number is even, or divisible by two. Second, for all odd numbers from 3 to floor(square root(n)), n being tested for primality, see if it is divisible. For those, use the modulo operation, '%'. It returns the remainder. If (a % b) = 0, then b divides a, a is divisible by b.

Examples:
11 % 5 = 4 ( 11 = 2*5 + 1)
11 % 3 = 2 ( 11 = 3*3 + 2)

Is 67 prime? sqrt(67) = 8.xx, floor(sqrt(67)) = 8. Test 2, 3, 5, 7
67 % 2 = 1
67 % 3 = 1
67 % 5 = 2
67 % 7 = 4

Therefore, 67 is prime since no possible options divide it. Of course, for a larger number, testing whether it's divisble by 9 may be redundant, but it's faster, I think just to check it, rather than check that 9 is prime (recursion, ugly).
Try this code

for(int pp=2;pp<100;pp++) //pp=possible prime
{
for(int pd=2;pd<pp;pd++) //pd=possible divisor
{
if(pp%pd==0)
break;
}
if(pp==pd)
System.out.println(pp);
}
u may declare out of the for loops
Sep 4 '06 #3
Hi,
Please try this code,

int i,j,p;
for(i=2;i<=100;i++)
{
p = 0;
for(j=2;j<=i/2;j++)
{
if(i%j == 0)
{
p = 1;
break;
}
}
if(p == 0)
System.out.println(i);
}




This is exact way.
ex:- no:40, in this number divisible only between 1 to 20, in any case, not divisibly by 21 to 39. So, you must to check the number is prime in off of that number only.

Bye...

Regards,
Suresh
Sep 6 '06 #4
how do i put input during execution in prime number progarm
Oct 4 '06 #5
r035198x
13,262 8TB
how do i put input during execution in prime number progarm
What do you want to input?
Normal methods of input are the Scanner, BufferedReader and swing.JOptionPane.inputDialog
Oct 4 '06 #6

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...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
0
by: AshifToday | last post by:
this was my and my frineds little project in earlier classes, the program seperates the composite and prime numbers in two sections of the screen ===================== /* This program has...
0
by: ETM11871 | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
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 ?
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......
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.