Jump to content

help Even Fibonacci numbers


Recommended Posts

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, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

 

Dim $numeroAnterior = 1
Dim $numeroSuperior = 1
Dim $Sumatotal = 0
Dim $sumaPares = 0
Dim $ParesTotal = 0
Dim $i = 1

While $i < 91
$Sumatotal = $numeroAnterior + $numeroSuperior ; 2     3      5
$numeroAnterior = $numeroSuperior ; 1        2       3
$numeroSuperior = $Sumatotal  ; 2 ;       3          2
$sumaPares = Mod($Sumatotal,2)
If ($sumaPares = 0) Then
   $ParesTotal = $Sumatotal + $ParesTotal
EndIf


$i = $i + 1
Wend
MsgBox(0,"prueba",$ParesTotal)

The problem I have: If I put While $i < 4000000 negative number shows the Msgbox,but if I put a number less than 91 the program works correctly

 

Help me please

Link to comment
Share on other sites

Hi.

Welcome to the forum!

read up the variable types and there ranges in the help file.

This might help you to understand, what's going on:

dim $test=99999999999999

$type=VarGetType($test)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $type = ' & $type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

$test-=99999999999998 ; variable is calculated with: Var Type remains
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $test = ' & $test & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
$type=VarGetType($test)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $type = ' & $type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

$test=1 ; Variable value is *RE*assigned: Var Type is dynamically changed
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $test = ' & $test & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
$type=VarGetType($test)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $type = ' & $type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

 

Thank friend!

 

I am very new to programming, the code you entered could not understand

variable type:  Dim,global and Local?

 

Global $numeroAnterior = 1
Global  $numeroSuperior = 1
Global  $numeroGuardar1 = 1
Global  $numeroGuardar2 = 1
Global $Sumatotal = 0
Global  $sumaPares = 0
Global  $ParesTotal = 0
Global  $i = 1

While $i < 100
$Sumatotal = $numeroAnterior + $numeroSuperior ; 2     3      5
$numeroAnterior = $numeroSuperior ; 1        2       3
$numeroSuperior = $Sumatotal  ; 2 ;       3          2
$sumaPares = Mod($Sumatotal,2)
If ($sumaPares = 0) Then
    $ParesTotal = $Sumatotal + $ParesTotal
EndIf


$i = $i + 1

Wend
MsgBox(0,"prueba",$ParesTotal)

But still does not work

 

Ranges:  Int32,Int64 etc etc... But I do not understand

 

I am very new to programming and do not understand much

Link to comment
Share on other sites

Maybe something like this with absolute values?  I think the numbers get too large so the answers will start to render in scientific notation format.  Perhaps you can reformat:

$fibNum1=0
$fibNum2=1

for $a=1 to 999
    $fib=Abs($fibNum1)+Abs($fibNum2)
    $fibNum1=Abs($fibNum2)
    $fibNum2=Abs($fib)
    ConsoleWrite(@crlf&$fib&@crlf)
Next

Also worth mentioning that if you run it more than 1475 times it appears the numbers will get too large for AutoIt.

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

On 3/22/2016 at 10:40 AM, hackmin said:

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

The logic in your method is faulty. You try to calculate the first 4 million terms in the series, which is something quite different from the wording in the problem.

Local $numeroAnterior = 1
Local $numeroSuperior = 1
Local $Sumatotal = 0
Local $sumaPares = 0
Local $ParesTotal = 0

While $numeroSuperior <= 4000000
    If ($sumaPares = 0) Then
       $ParesTotal = $Sumatotal + $ParesTotal
    EndIf
    $Sumatotal = $numeroAnterior + $numeroSuperior ; 2     3      5
    $numeroAnterior = $numeroSuperior ; 1        2       3
    $numeroSuperior = $Sumatotal  ; 2 ;       3          2
    $sumaPares = Mod($Sumatotal,2)
Wend
MsgBox(0,"prueba",$ParesTotal)

 

Edited by czardas
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...