How to store large 20 million digit number in php? [duplicate] - php

This question already has answers here:
Working with large numbers in PHP
(8 answers)
Closed 3 years ago.
I searched and I found that I can use number_format function in php. But it can't even store 1 million digits . I am getting output as inf.
Can someone help me please. If it's not possible in php then can it be done in C++ or Java ?
Update: I am using GMP but when I run script browser sent me response as "this site can't be reached, connection was reset". I disabled max execution time but it gives me same error.
What should I do? Do I need to do any other changes in configuration?

There is bigint choice in phpmyadmin
Laravel has that too, and you can do it in terminal
bigint is great but rarely used
Updated
you can use float and set value limit
In this picture I used float and defined the value limit to 20
Hope that helps

Related

Int casting gives wrong number on WAMP [duplicate]

This question already has answers here:
Working with large numbers in PHP
(8 answers)
What's the maximum value for an int in PHP?
(8 answers)
Closed 1 year ago.
I have some legacy code on a Windows machine running PHP 5.6 that we will be dismissing but I need to do some migrations from.
I have trivial bit of code that is quite mistifying to me.
I've simplified it for this question, but the behavior is the same.
$a = "3109584483";
$b = (int) $a;
var_dump($a);
var_dump($b);
And this is the output:
string(10) "3109584483"
int(2147483647)
I have looked at all the similar questions about this, but they all mention
Casting a float (not my case)
Getting the result in a non-10 base (I tried converting 2147483647 from and to common bases like HEX, OCT and BIN, but I can't seem to find a match that would justify the result)
What is the reason of this wrong casting?
Thanks in advance for your help!

Get Whole Number for Power of 100 with PHP ( Question with PHP POW function ) [duplicate]

This question already has answers here:
Working with large numbers in PHP
(8 answers)
Closed 2 years ago.
That is my code below to get "100 power of 2 in PHP",
echo pow(2,100);
I would get the result as 1.2676506002282E+30, However, I wish to get a whole integer of the result.
How should I do it with PHP?
if by accurate, you want to have the exact value of 2 to the power of 100 that is equal to 1,267,650,600,228,229,401,496,703,205,376,
you can simply use
echo number_format(pow(2,100));
Update: like #Olivier said in comments, number_format does not return the exact results in some cases, so be careful in using that.
I guess by "accurate" you mean you want to get an exact integer. You will have to use additional library for working with big integers. GMP for example will work for what you need. It's is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers.
Here is an example for the specific question:
<?php
$pow = gmp_pow("2", 100);
echo gmp_strval($pow);
?>
It will not work directly. You will have to first open php.ini and uncomment the "extension=gmp" line.
And that's not the only solution. There are more libraries available as variants for doing that.

Javascript and PHP timestamps different, giving different time [duplicate]

This question already has answers here:
Timestamp between Javascript and PHP
(3 answers)
Closed 9 years ago.
I'm getting different values for timestamp using javascript and PHP when code is executed almost at the same time.
using Javascript Date.getTime(), i get
1375228800000
for timestamp while at the same time PHP reports
1375233890
as timestamp from a call to time().
The first ten digits are close but the timestamp from javascript has extra 3 digits (000) which I think is causing PHP to return the date as 1996-08-05 17:08:40 instead of 2013-08-31.
I'm running Javascript and PHP on the same machine.
Why is Javascript adding extra digits (000) and how can I solve this problem? Please help.
JavaScript measures time in milliseconds, not in seconds. Just divide by 1,000.

Large int changed by Ajax [duplicate]

This question already has answers here:
Warning: Massive error in json_encode()
(3 answers)
Closed 8 years ago.
I have a weird (and rather frightening) problem with Ajax.
I'm returning 3 large integers from PHP back to Javascript (jQuery):
9849933840800076
9717106838244944
9261288452893495
But recieve:
9849933840800076
9717106838244944
9261288452893496
Notice the last digit in the third integer - WTF ?!?
- I'm logging everything, and am certain it's not my code doing it!
And returning them as strings instead does solve the problem, but still...
It would be nice to get an explanation - and some sleep : )
My fault, sorry. (and thank you, Pekka)
JS "looses precision" on integers larger than 2^53 (+/- 9007199254740992)
- Back to the wriggleroom..
in these kind of cases you can always return the numbers as strings and convert them in the js

Construct a Magic Square by a given number of rows [duplicate]

This question already has answers here:
How to create a magic square in PHP?
(3 answers)
Closed 6 years ago.
I'm trying to build an application in simple PHP but could be done in any language.
I want the user to enter number of rows and the output will be the magic square of it.
For example, for $rows=3:
8-1-6
3-5-7
4-9-2
Notice that in every row,column,diagonal the sum of numbers is equal to 15.
There is a method for constructing this with an odd number of rows. I DONT want that. I want the program to ACTUALLY calculate numbers, row-sums, column-sums and diagonal-sums, doesn't matter if the input is odd or even.
Do you have any idea how to go about doing this? Right now i'm drowning in a sea of loops.
HELP!
Sorry, after a further search I found a JAVA applet that does it:
link text
It's pretty sweet thinking about the extensive mechanism working behind this seemingly simple algorithm.

Categories