Answer the question
In order to leave comments, you need to log in
Is it possible to implement the algorithm in PHP in another PL?
Good afternoon!
I continue to study programming (in particular, the PHP language), in order to understand the algorithms, I decided to take this approach for myself:
1. from the book "128 PHP recipes" we take any algorithm
2. I chose the fourth algorithm "Displaying a multiple of N"
To determine the number of a multiple of N, you can take advantage of the possibility of obtaining the remainder of the division. If when dividing a number by N, the remainder is zero, then the number is a multiple. It is only necessary to add one more condition - checking the number for zero, since 0 / N \u003d 0, without a remainder.
$n = 3;
for ($i=0; $i<10; $i++)
{
if ((($i%$n) == 0) AND ($i != 0))
{
echo $i . "<br/>";
}
}
Answer the question
In order to leave comments, you need to log in
How effective will this teaching approach be?To learn a particular language, the profit will most likely be negative, but the language is just a tool, it does not need to be learned, it must be used. And you need to study programming in general. And here the profit is debatable, but if you try to optimize the algorithm using the means of each of the languages, there will be a profit.
$n = 3;
for ($i = 0; $i < 10; $i++)
{
if ((($i % $n) == 0) AND ($i != 0))
{
echo $i . "<br/>";
}
}
Now let's optimize in PHP itself:$n = 3;
for ($i = 0; $i < 10; $i += $n)
{
if ($i == 0) continue;
echo $i . "<br/>";
}
const n = 3;
for(let i = 0; i < 10; i++) {
if((i % n === 0) && (i !== 0)) {
console.log(i + '<br/>');
}
}
Note that if the code is similar, the result is different, in addition to the numbers separated by line breaks. But you can rewrite it in another way:<br/>
const n = 3;
console.log(Array.from({length: 9}, (_, i) => i + 1).filter(i => i % n === 0).join('<br/>'));
fn main() {
const N: i32 = 3;
for i in (1..10).filter(|i| i % N == 0) {
print!("{}<br/>", i);
}
}
Already in the second language we see that not only through if you can weed out unnecessary values. #include <stdio.h>
int main() {
int n = 3;
for(int i = 0; i < 10; i += n) {
if(!i) continue;
printf("%d<br/>", i);
}
return 0;
}
I note that there is no bool type in C, so you can use a condition like (!i) to check for 0 .TXT:
.string "%d<br/>"
.text
.globl main
.type main, @function
main:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $3, -8(%rbp)
movl $0, -4(%rbp)
jmp .LOOP_COND
.LOOP_BODY:
cmpl $0, -4(%rbp)
je .LOOP_NEXT
movl -4(%rbp), %eax
movl %eax, %esi
leaq .TXT(%rip), %rdi
movl $0, %eax
call [email protected]
jmp .LOOP_NEXT
.LOOP_NEXT:
movl -8(%rbp), %eax
addl %eax, -4(%rbp)
.LOOP_COND:
cmpl $9, -4(%rbp)
jle .LOOP_BODY
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
so for the overall development
Well, here it is in JS:
var n = 3;
for(let i=0;i<10;i++){
if(((i%n)==0) && (i!=0)){
console.log(i);
}
}
Understanding the logic of the algorithm will decrease by 50% EXACTLY!
50% - that you will understand a new language.
50% - that you will understand this language (PHP)
50% - that you will understand the logic of the algorithm itself.
That is, the effectiveness of teaching the current language will be: (50+50+50)/2 = 75%
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question