Hello.
I need a simple programme to print prime numbers from 1 to 100.
I need it urgently.
5 15847
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).
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
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
how do i put input during execution in prime number progarm
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: newstips6706 |
last post by:
1, 2, 3, 5, 7... PRIME Numbers
________________________________
Definitions
What is a PRIME Number ?
|
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......
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |