473,471 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Display prime numbers

1 New Member
Write a program to load in two integers from the user. Display all the prime numbers between the two integers.

i'm a beginner. i'm problem doing this. can anyone teach me ?

thanks a lot.
Sep 15 '07 #1
13 2474
Ganon11
3,652 Recognized Expert Specialist
Sure. One of the first tasks in teaching is assessing what the student (that's you) knows, regarding the problem. So, what do you know about prime numbers? What approaches have you considered taking to solve this problem? What ideas do you have?
Sep 15 '07 #2
JosAH
11,448 Recognized Expert MVP
Sure. One of the first tasks in teaching is assessing what the student (that's you) knows, regarding the problem. So, what do you know about prime numbers? What approaches have you considered taking to solve this problem? What ideas do you have?
mathematician: 3 is prime, 5 is prime, 7 is prime so by complete induction all
odd numbers are prime;

physicist: 3 is prime, 5 is prime, 7 is prime, 9, erm, sample data error, 11 is
prime, 13 is prime; yeah the math is ok;

engineer: 2 is prime, 4 is prime, 6 is prime, 8 is prime, yep, all odd numbers
are prime alright.

politician: what's an odd number?

philosopher: what's prime?

kind regards,

Jos ;-)
Sep 15 '07 #3
Nepomuk
3,112 Recognized Expert Specialist
mathematician: 3 is prime, 5 is prime, 7 is prime so by complete induction all
odd numbers are prime;

physicist: 3 is prime, 5 is prime, 7 is prime, 9, erm, sample data error, 11 is
prime, 13 is prime; yeah the math is ok;

engineer: 2 is prime, 4 is prime, 6 is prime, 8 is prime, yep, all odd numbers
are prime alright.

politician: what's an odd number?

philosopher: what's prime?

kind regards,

Jos ;-)
Computer Scientist: 0 is prime, 1 is prime, memory out of stack error.

Motivator: Every number can be prime, if it wants to!

Priest: I believe, that all numbers were created equally!
(Oh sorry, no religious content allowed... ^^)

Greetings,
Nepomuk
Sep 16 '07 #4
Ganon11
3,652 Recognized Expert Specialist
Priest: I believe, that all numbers were created equally!
Isn't this really Martin Luther King, Jr.?
Sep 16 '07 #5
Nepomuk
3,112 Recognized Expert Specialist
Isn't this really Martin Luther King, Jr.?
Fine by me ^^

Then the priest says: "All numbers are equal before god." ;-)

Greetings,
Nepomuk
Sep 16 '07 #6
Nepomuk
3,112 Recognized Expert Specialist
Some more:

George Orwell: All numbers are prime, but some are more prime than others.

Psychologist: 3 is prime, 5 is prime, 7 is prime, 9 is prime but has an identity crisis, 11 is prime...

Trade Unionist: We will fight for the same right for all odd numbers!

Lawyer: 9 is prime and I'll sue anyone who says otherwise!

Deep Thought: 42!

Greetings,
Nepomuk
Sep 16 '07 #7
sateesht
41 New Member
Hi,

Prime Number is a Number which have only two factors 1 and itself.


Cheers,
Sateesh.
Sep 17 '07 #8
kreagan
153 New Member
Write a program to load in two integers from the user. Display all the prime numbers between the two integers.

i'm a beginner. i'm problem doing this. can anyone teach me ?

thanks a lot.
You guys are way too funny.

Anyways, finding the prime numbers via brute force (though the most obvious way to solve the problem) is inefficient. When I say brute force, I mean given N, I divide N via all numbers greater than 1 and less than N.

I would read a mathematical theorem: "Fermat's Little Theorem". This theorum helps determine if a number is prime or not. Be aware that this theorem will not always find the prime numbers but give a probability that a number is or isn't prime.

Fermat's Little Theorem in Scheme
Sep 17 '07 #9
Nepomuk
3,112 Recognized Expert Specialist
You guys are way too funny.

Anyways, finding the prime numbers via brute force (though the most obvious way to solve the problem) is inefficient. When I say brute force, I mean given N, I divide N via all numbers greater than 1 and less than N.

I would read a mathematical theorem: "Fermat's Little Theorem". This theorum helps determine if a number is prime or not. Be aware that this theorem will not always find the prime numbers but give a probability that a number is or isn't prime.

Fermat's Little Theorem in Scheme
Well, the "brute force" method is certainly more accurate, and you don't have to check all numbers from 1 to N, but only from 2 (or if you only check every second number: from 3) to sqrt(N). Saves a lot of time...

Greetings,
Nepomuk

PS.: I wrote a little method, that does exactly that and it calculated all Primes between 1 and 10000000 in only a few minutes. So it's not that inefficient... ^^
Sep 17 '07 #10
JosAH
11,448 Recognized Expert MVP
You guys are way too funny.

Anyways, finding the prime numbers via brute force (though the most obvious way to solve the problem) is inefficient. When I say brute force, I mean given N, I divide N via all numbers greater than 1 and less than N.

I would read a mathematical theorem: "Fermat's Little Theorem". This theorum helps determine if a number is prime or not. Be aware that this theorem will not always find the prime numbers but give a probability that a number is or isn't prime.

Fermat's Little Theorem in Scheme
For any number less than 2^64 every locally smart method does fine; Fermat's
little trick doesn't buy you much if you want certainty; it just has nice theoretical
applications; algorithmically it can't do much more for you than being a stochastic
test.

kind regards,

Jos
Sep 17 '07 #11
kreagan
153 New Member
For any number less than 2^64 every locally smart method does fine;
Good Point

Fermat's
little trick doesn't buy you much if you want certainty; it just has nice theoretical
applications; algorithmically it can't do much more for you than being a stochastic
test.
If the test fails, the number is guarrenteed to be a non-prime number. By using Fermat's Little Theorem as a preliminary check, then throughly checking if the number is indeed a prime (using a locally smart method like you said), you increase speed and guareentee accuracy.

As for the Fermat's Test, every source I have read states it is " adequate for practical purposes."
Sep 17 '07 #12
JosAH
11,448 Recognized Expert MVP
As for the Fermat's Test, every source I have read states it is " adequate for practical purposes."
It is in theory but it isn't in practice; you do have the sources available of all
the core classes; have a look at the BigInteger.isProbablePrime() method;
it's much better, it's a combination of the Miller-Rabin test and the Lucas test
if I'm not mistaken. Fermat's 'little' theorem is a nice theoretical thingy.

kind regards,

Jos
Sep 17 '07 #13
kreagan
153 New Member
It is in theory but it isn't in practice; you do have the sources available of all
the core classes; have a look at the BigInteger.isProbablePrime() method;
it's much better, it's a combination of the Miller-Rabin test and the Lucas test
if I'm not mistaken. Fermat's 'little' theorem is a nice theoretical thingy.

kind regards,

Jos
Awesome, thanks a lot!
Sep 17 '07 #14

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: 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......
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...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.