/    Sign up×
Community /Pin to ProfileBookmark

interger problem

Hi all.

I am trying to solve a problem from project euler, for the purpose of learning ? I’m not asking here for the answer or logic, I got the latter but it seems that I have a problem in outputting the integer since it exceeds the limit of variable limit in PHP (as I know).

[code=php]
<?php

$a = 0;
$b = 1;
$c = 0;
$even_count = 0;

for( $i = 0 ; $i < 4000000 ; $i++ ) {
$c = $a + $b;
$a = $b;
$b = $c;
if( $c % 2 == 0 ) {
$even_count += $c;
}
}

echo “Sum of all even-valued terms: ” . $even_count;

?>
[/code]

The code is all about the sum of even numbers in the fibbonaci sequence which do not exceed four million.

Whenver I run the program the output as expected is

[CODE]Sum of all even-valued terms: INF[/CODE]

I can’t figure out how to output such big number. Any help? Thanks.

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@zimonyiOct 12.2010 — What is the result if you call var_dump() on your $even_count variable?

My simple guess is that you have indeed exceeded the limit on your platform for handling numerical values and that if you have a 32BIT operating system you are out of luck. Perhaps with a 64BIT operating system it would be fine.

I checked the PHP manual but I cannot find any object/function that would allow you to store the numerical value as something else and work with it to get around your problem.

Archie
Copy linkTweet thisAlerts:
@NogDogOct 12.2010 — You could use the [url=http://www.php.net/manual/en/book.bc.php]BCMath functions[/url], or the PEAR [url=http://pear.php.net/package/Math_BigInteger]Math_BigInteger package[/url].
Copy linkTweet thisAlerts:
@zimonyiOct 12.2010 — You could use the [url=http://www.php.net/manual/en/book.bc.php]BCMath functions[/url], or the PEAR [url=http://pear.php.net/package/Math_BigInteger]Math_BigInteger package[/url].[/QUOTE]

Yeah, what NogDog said.

Great that you found them, I knew they should exist but did not know what they might be called. Good to know for future reference.

Archie
Copy linkTweet thisAlerts:
@kingdmauthorOct 13.2010 — What is the result if you call var_dump() on your $even_count variable?

My simple guess is that you have indeed exceeded the limit on your platform for handling numerical values and that if you have a 32BIT operating system you are out of luck. Perhaps with a 64BIT operating system it would be fine.

Thanks for your reply, I checked the PHP manual but I cannot find any object/function that would allow you to store the numerical value as something else and work with it to get around your problem.

Archie[/QUOTE]


Whenever I call var_dump() on $even_count variable it outputs float(INF). Yeah you're right, I'm using 32-bit operating system.

You could use the [url=http://www.php.net/manual/en/book.bc.php]BCMath functions[/url], or the PEAR [url=http://pear.php.net/package/Math_BigInteger]Math_BigInteger package[/url].[/QUOTE]

Thanks once more for your reply NogDog, I checked the link you gave me but I can't seem to have an idea how to start using it. Can you give me some samples or simple approach regarding this? Thanks so much.
Copy linkTweet thisAlerts:
@zimonyiOct 13.2010 — Whenever I call var_dump() on $even_count variable it outputs float(INF). Yeah you're right, I'm using 32-bit operating system.



Thanks once more for your reply NogDog, I checked the link you gave me but I can't seem to have an idea how to start using it. Can you give me some samples or simple approach regarding this? Thanks so much.[/QUOTE]


As long as you have the package installed (see the Installation section on the link NogDog gave) you just need to exchange your

[CODE]$even_count += $c;[/CODE]

with

[CODE]$even_count = bcadd($even_count, $c);[/CODE]

Archie
Copy linkTweet thisAlerts:
@NogDogOct 13.2010 — [code=php]
$a = 0;
$b = 1;
$c = 0;
$even_count = 0;
for ($i = 0; $i < 4000000; $i++) {
$c = bcadd($a, $b);
$a = $b;
$b = $c;
if (bcmod($c, 2) == 0) {
$even_count = bcadd($even_count, $c);
}
}
echo "Sum of all even-valued terms: " . $even_count;
[/code]

And be prepared to wait awhile, and probably have to use set_time_limit() to allow it to run long enough. (120 seconds was not enough on my PC.) ?
Copy linkTweet thisAlerts:
@kingdmauthorOct 14.2010 — Thanks Archie and NogDog for your reply.

Upon reading the BCMath and your replies I finally get the idea how does this things work, however I can't make it run due to some execution time limit.

@NogDog

I wonder where will I set the execution time to greater value, I had looked into the php.ini but the only BCMath related thing I could get is in here

[CODE]
[bcmath]
; Number of decimal digits for all bcmath functions.
; http://php.net/bcmath.scale
bcmath.scale = 0
[/CODE]


Sorry for being quite redundant in asking, its just that its not my usual hobby to manipulate the php.ini configurations ?
Copy linkTweet thisAlerts:
@kingdmauthorOct 14.2010 — Sorry for my earlier post, after doing some research and trials I found out how to use the set_time_limit(). I added set_time_limit(1000) on top of my php code. But still I got the fatal error for maximum execution time. What is the maximum seconds I could set? Is the problem lies in my 1gb ram laptop? ?
Copy linkTweet thisAlerts:
@zimonyiOct 14.2010 — Sorry for my earlier post, after doing some research and trials I found out how to use the set_time_limit(). I added set_time_limit(1000) on top of my php code. But still I got the fatal error for maximum execution time. What is the maximum seconds I could set? Is the problem lies in my 1gb ram laptop? ?[/QUOTE]

See set_time_limit() for more details.

Archie
Copy linkTweet thisAlerts:
@NogDogOct 14.2010 — You can set it to zero (0), which means no time limit. However, before doing that, try setting the maximum for $i in the loop condition to a much smaller number to test your code before letting it loose for 4 million iterations so you can find out if the code is reasonable. If it takes 1/1000th of a second per iteration (purely hypothetically, maybe more, maybe less), you'll need at least 4000 seconds -- a bit over an hour -- for it to run.
×

Success!

Help @kingdm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...