math problems
Author |
Message |
TokenHerbz
|
Posted: Mon Oct 10, 2005 12:48 am Post subject: math problems |
|
|
I want to make a code that asks the user for 2 numbers, and inbetween these numbers it will print out numbers which are only able to divide by its self, and 1 evenly...
Ex, 1,2,3,5,7,11 ( i think )
code: |
var num1, num2: int
var n1, n2: int
put "please enter 1 number"
get num1
put "please enter another number"
get num2
if num1 < num2 then
n1 := num1
n2 := num2
elsif num1 > num2 then
n1 := num2
n2 := num1
else
put "Bah, your numbers are the same"
end if
for i: 1 .. n2
if i >= n1 then
if i / i = 1 then %%% <-- heres where i need the math help?
put i
end if
end if
end for
|
Help me please |
|
|
|
|
|
Sponsor Sponsor
|
|
|
zylum
|
Posted: Mon Oct 10, 2005 1:14 am Post subject: (No subject) |
|
|
so you want to print the primes in a range of numbers?
code: | var a, b : int
put "what are the numbers"
get a, b
var primes : array 1 .. b of boolean
for i : 1 .. b
primes (i) := true
end for
for i : 2 .. b
for j : i * 2 .. b by i
primes (j) := false
end for
end for
for i : 1 .. b
if primes (i) then
put i, " ", primes (i)
end if
end for |
|
|
|
|
|
|
Drakain Zeil
|
Posted: Mon Oct 10, 2005 9:58 am Post subject: (No subject) |
|
|
I ran through a for loop with i as a count.
if i was not divisable by the number,(use mod), then I would output.
You only need to run the loop from: a..(b/2)+1
And a decent range for prime check is (a/3)-1..(b/2)+1 |
|
|
|
|
|
GlobeTrotter
|
Posted: Mon Oct 10, 2005 7:25 pm Post subject: (No subject) |
|
|
Drakain Zeil wrote: I ran through a for loop with i as a count.
if i was not divisable by the number,(use mod), then I would output.
You only need to run the loop from: a..(b/2)+1
And a decent range for prime check is (a/3)-1..(b/2)+1
Actually to check for a given prime, you only need to check if the number is divisible by any prime between 0 and the root of the number. |
|
|
|
|
|
jamonathin
|
Posted: Mon Oct 10, 2005 7:38 pm Post subject: (No subject) |
|
|
Drakain Zeil wrote:
Actually to check for a given prime, you only need to check if the number is divisible by any prime between 0 and the root of the number.
But if we're looking for a prime number, how would be divide it by primes to find out if it's prime? |
|
|
|
|
|
Cervantes
|
Posted: Mon Oct 10, 2005 8:04 pm Post subject: (No subject) |
|
|
jamonathin wrote:
But if we're looking for a prime number, how would be divide it by primes to find out if it's prime?
Start from the bottom up. |
|
|
|
|
|
|
|