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

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.

Related

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.

How to determine a word is English or any other language [duplicate]

This question already has answers here:
Detect language from string in PHP
(18 answers)
Closed 9 years ago.
I am developing a small library automation software and I need to determine a word is in English or Turkish. An example scenario is like this:
User enters a book title.
Determine it's Turkish or English.
Set the languge combobox to the respective language to help user fill the form.
A friend of mine suggested me "connect to Google Translate and use it" which seems reasonable but an algorithm without connecting an external service or database will be more appropriate for me. (I also search the Turkish/English specific characters like ç,ş,İ/w,x to decide) Therefore I am searching an algorithm to do this job maybe based on letter frequencies or something like it. Anything available in literature? Thanks, in advance. (I use php, mysql if it's important)
If the sample you're testing is that small (a single word or phrase) then simple heuristics like letter frequency aren't going to be very useful, as the English phrase "Jazz Quizzes" would probably fit the profile of many languages more readily than English.
You might be able to use frequency of bigraphs and trigraphs (2- and 3-letter combinations), as English and Turkish are sufficiently unrelated as to have combinations which only occur in one.
More likely, however, you are going to have to use a database of actual words from the two languages. In that case, you are probably best off using a third party API or database, rather than going to all the effort building your own corpuses, implementing the statistical algorithms, etc.
As per comment.
please check:
Detect language from string in PHP
or:
http://wiki.apache.org/solr/LanguageDetection
Solr can give you language with probability (for example this sentence is 90% English or 10% Turkish)

PHP string length [duplicate]

This question already has answers here:
PHP String Length Without strlen()
(3 answers)
Closed 3 years ago.
PHP coding standards say:
... PHP holds the length property of each string, and that it
shouldn't be calculated with strlen(). Write your functions in a such
a way so that they'll take advantage of the length property, both
for efficiency and in order for them to be binary-safe. ...
How can I access this length property? Or do I misunderstand it?
As Ignacio mentioned in another post:
They're talking about the C function, not the PHP function. The C
function will stop counting after the first \0, but PHP strings can
contain \0 elsewhere other than the end.
That document is most likely about how to extend PHP's engine, rather than programming in PHP itself.
These coding standards are for not intended for web sites developpers using PHP, but for the developpers of the PHP engine itself.
PHP is developped using the C language, and the strlen function to avoid is the C one, not the PHP one.
It's not a "property" in the sense that it would be in other languages (like C#).
You cannot do:
myString.Length;
Instead you would need to do:
strlen(myString);

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

How to get absolutely unique and five letters string using only A-Z a-z 0-9 in php? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP: How to generate a random, unique, alphanumeric string?
I'm creating the image hosting website. I need to create a unique and five letters string (case sensitive) for an image, like an example -> imgur.com/srM0U
I have seen many examples, but they are not unique or not case sensitive.
Generated string I will use for an image filename, I think imgur uses a unique strings.
Please help with a piece of code
http://kevin.vanzonneveld.net/techblog/article/create_short_ids_with_php_like_youtube_or_tinyurl/
The main thing to keep in mind is that the way most short-URL systems work is that they save the long form URL into a database, with an auto-incremented ID. That ID is then converted to an alphanumeric short code by a script similar to the one above.
The reason I point this out is I think you're approaching the problem backwards- trying to assign an alphanumeric shortcode first. It's much easier to have unique numeric values (hence, auto-increment) and then approach encoding it from there.
Sorry for the poor formatting, sent from my phone.

Categories