for loop help
Author |
Message |
coldmine16
|
Posted: Fri Aug 11, 2006 10:14 am Post subject: for loop help |
|
|
if you have a for loop for example counts to 100 how would you take out all the even terms ? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Fri Aug 11, 2006 10:46 am Post subject: (No subject) |
|
|
The simplest way would be:
code: |
for i : 1 .. 100 by 2
put i
end for
|
|
|
|
|
|
|
Wolf_Destiny
|
Posted: Fri Aug 11, 2006 11:29 am Post subject: (No subject) |
|
|
Or if you feel like making slightly more complicated code, which works in different situations then:
code: | for a : 1..100
if a mod 2 = 0 then
put "It's even"
else
put "It's odd"
end if
end for |
Now this also gives you the option to use it in a loop with a counting variable.
code: | var count : int := 0
loop
count += 1
if count mod 2 = 0 then
put "It's even"
else
put "It's odd"
end if
end loop |
Where it says "mod 2" it's basically looking for what the remainder is after dividing by 2. So if there is no remainder, then it must be even. What works well with this method is that if you kept adding random numbers to "count" then you could always tell if it's even or odd.
But for what you want to do, Cervantes had the simplest way. However you'll want to start with an even number (or 0) to count by even numbers. If you start at one then it counts 1, 3, 5, 7, 9... If you start at 0 then it counts 0, 2, 4, 6, 8... because it just keeps adding to your starting number.
Hope that helps |
|
|
|
|
|
coldmine16
|
Posted: Fri Aug 11, 2006 11:50 am Post subject: (No subject) |
|
|
so im still working on the math site and so far everything is working ofr this question :
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Find the sum of all the even terms in the sequence below one million.
i have written this program and thanx to ur guys help i wus able to find the even terms and add them however i only had it up to 10 the for loop however when i set it to 1 million or even a 1000 the problem that arises is that i have a overflow integer epression i know what ite means i just want to know if there is some way of maybe getting around it i was thinking along the lines of maybe writing the numbers to a file and calculating them from the file something liek that but if there is a better way please help
here is the code if you would like to look at it
code: |
var num1, num2 : int
var total : int := 0
num1 := 0
num2 := 1
for x : 1 .. 1000
num1 := num1 + num2
num2 := num1 + num2
if num1 mod 2 = 0 then
total := num1 + total
end if
if num2 mod 2 = 0 then
total := num2 + total
end if
end for
put total
|
|
|
|
|
|
|
Wolf_Destiny
|
Posted: Fri Aug 11, 2006 12:23 pm Post subject: (No subject) |
|
|
I've modified your code ever so slightly so that you can see the output of the sequence it's making.
code: | var num1, num2 : int
var total : int := 0
num1 := 0
num2 := 1
for x : 1 .. 10 %CHANGED (was 1000)
put num1 %ADDED
num1 := num1 + num2
num2 := num1 + num2
if num1 mod 2 = 0 then
total := num1 + total
end if
if num2 mod 2 = 0 then
total := num2 + total
end if
end for
put "" %ADDED
put total |
Run it and look at the actual sequence you're making. I've just made the program you're attempting, and I needed three variables (not including total) to make the sequence work. Don't worry about the addition right now, worry about the sequence and making num1 display the numbers.
Oh a side note, it says the numbers under 1 million, not the first 1 million numbers of the sequence.
Hope that helps without actually stating what to do. |
|
|
|
|
|
|
|