Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7546539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:13:28+00:00 2026-05-30T09:13:28+00:00

I got the solution; however, I feel like the code is pretty awful. This

  • 0

I got the solution; however, I feel like the code is pretty awful. This is within my first 50 hours using any programming language…please bear with me.

The Problem:

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.

My Solution:

<?php

//if number is odd, returns false
function setOddsZero($n) {
    $test = ($n&1); //0 = even, 1 = odd
    if($test == 1) {
        return false;
    } else {
        $n = $n;
    }
}


$numbers=array(1,);
for($i>0; $i<=100; $i++) {
$numbers[$i] += (($numbers[$i-2])+($numbers[$i-1]));
  if (($numbers[$i]) >= 4000000) {
      echo $total;
      die;
  } else {
      if((setOddsZero($numbers[$i]))===false) {
          $total += 0;
      }else {
          $total += $numbers[$i];
      }
  }
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:13:30+00:00Added an answer on May 30, 2026 at 9:13 am
    $fibos = array(1,2);
    $sum_of_evens = 0;
    
    while ($fibos[1] < 4000000)
    {
        $fibos []= array_shift($fibos) + $fibos[0];
        $sum_of_evens += ($fibos[1] & 1 == 0) ? $fibos[1] : 0;
    }
    
    echo $sum_of_evens;
    

    Less stack-pushy-shify (and thus more efficient) approach, as suggested by meze:

    $prevprev = 1;
    $prev = 2;
    $sum_of_evens = 0;
    
    while ($prev < 4000000)
    {
        list($prevprev, $prev) = array($prev, ($prevprev + $prev));
        $sum_of_evens += ($prev & 1 == 0) ? $prev : 0;
    }
    

    Edit: Modified the code to use & 1 instead of % 2, cf. this thread at devshed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a working solution, however I'm pretty sure there is a less resource-intensive
I always got this error guys on my site.Anybody got a solution. Stacktrace at
Update: We are still using XP at work and I got my solution working,
I've got some code executing in a while(fscanf != EOF) loop. However, even when
I've got a pretty simple problem that has a solution I'm not able to
I've got three C# ASP.NET MVC2 projects within a single solution. Names have been
I've got a predicate taking a single argument. When there is a solution, this
I've got a large solution that I'm using TFS (and MSBuild) to... well... build.
I've got a Wix situation that seems like its solution might be the combination
So I've got this nice little MVVM solution, and things work great. I've got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.