Find Sum of 5th Powers of First N Natural Numbers in PHP

AmitDiwan
Updated on 02-Jul-2020 07:03:38

207 Views

To find the sum of the 5th powers of first n natural numbers, the code is as follows −Example Live DemoOutputThe sum of fifth powers of the first n natural numbers is 85648386825A function named ‘sum_of_fifth_pow’ is defined, and an initial sum value is defined as 0. The fifth power of every natural number up to a certain range is computed and added to the initial sum value. Outside the function, a value is defined and the function is called by passing this value as parameter. Relevant message is displayed on the console.

What is Byzantine Fault Tolerance

Prasanna Kotamraju
Updated on 02-Jul-2020 07:03:25

2K+ Views

Satoshi Nakamoto created Bitcoin in 2008, and he made the network very strong as a distributed, peer to peer model which is maintained without any intermediaries. Since then many digital currencies are created, which follow the same system where all nodes are sharing same information (same copy of Block chain) and any node can communicate with any other node safely across the network, knowing that they are displaying same data.Byzantine Fault Tolerance (BFT) is one of the most difficult challenges faced by the Block chain technology. All the participants of the cryptocurrency network need to agree, or give consensus regularly ... Read More

Find Average of First N Even Natural Numbers in PHP

AmitDiwan
Updated on 02-Jul-2020 07:02:20

842 Views

To find the average of the first n natural numbers that are even, the code is as follows −Example Live DemoOutputThe average of the first n natural numbers that are even is 12A function named ‘even_nums_avg’ is defined that adds up all the numbers up to a given limit, and divides it by total numbers of values. This is the average of the first few natural numbers. A value is defined, and the function is called by passing this value, which is actually the limit up to which the average of numbers needs to be found. Relevant message is displayed on ... Read More

Sum of Cubes of Odd Natural Numbers in PHP

AmitDiwan
Updated on 02-Jul-2020 07:01:25

297 Views

To find the sum of cubes of natural numbers that are odd, the code is as follows −Example Live DemoOutputThe sum of cubes of first 8 natural numbers that are even is 3968A function named ‘sum_of_cubes_even’ is defined that takes a limit on the natural number up to which the cube of every number needs to be found and each on them need to be added up. A ‘for’ loop is iterated over, and every number is cubed and added to an initial sum that was initially 0. The function is called by passing a number that is the limit as ... Read More

Check If All Digits of a Number Divide It in PHP

AmitDiwan
Updated on 02-Jul-2020 07:00:08

243 Views

To check if all digits of a number divide it in PHP, the code is as follows −Example Live Demo

Check if Total Number of Divisors is Even or Odd in PHP

AmitDiwan
Updated on 02-Jul-2020 06:58:31

233 Views

To check if the total number of divisors of a number is even or odd, the code is as follows −Example Live DemoOutputIt is an odd numberA function named ‘divisor_count’ is defined that gives the number of divisors of a given number that is passed as a parameter to the function. Now, each of these divisors is checked to see if it can be completely divided by 2, if yes, it is an even divisor, and otherwise, it is an odd divisor. Relevant message is displayed on the console.

Array Range Queries for Elements with Frequency Same as Value in C

Arnab Chakraborty
Updated on 02-Jul-2020 06:56:57

295 Views

Here we will see one interesting problem. We have one array with N elements. We have to perform one query Q as follows −The Q(start, end) indicates that number of times a number ‘p’ is occurred exactly ‘p’ number of times from start to end.So if the array is like: {1, 5, 2, 3, 1, 3, 5, 7, 3, 9, 8}, and queries are −Q(1, 8) − Here the 1 is present once, and 3 is present 3 times. So the answer is 2Q(0, 2) − Here the 1 is present once. So the answer is 1Algorithmquery(s, e) −Begin   ... Read More

Alternative for CONCAT in MySQL

AmitDiwan
Updated on 02-Jul-2020 06:55:49

963 Views

Yes, an alternative is CONCAT_WS(). Let us first create a table −mysql> create table DemoTable    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(100)    ); Query OK, 0 rows affected (0.74 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable(StudentName) values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentName) values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentName) values('Bob'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | ... Read More

Split Comma-Delimited String into Array in PHP

AmitDiwan
Updated on 02-Jul-2020 06:54:26

2K+ Views

To split a given comma delimited string into an array of values, the PHP code is as follows −Example Live DemoOutputThe array is Array (    [0] => 456    [1] => 789    [2] => 23    [3] => 4    [4] => 019 ) The array is Array (    [0] => 00    [1] => 876    [2] => 5432    [3] => 1234    [4] => 0 )A string of numbers is defined and the ‘preg_split’ function is used to separate the numbers based on the occurance of ‘/’ or ‘.’. The split array is now printed. Another method to split the given number in the form of a string is to use the explode function. It splits the array based on the occurance of a comma.

Remove Non-Alphanumeric Characters from String in PHP

AmitDiwan
Updated on 02-Jul-2020 06:53:14

1K+ Views

To remove non-alphanumeric characters from string, the code is as follows −Example Live DemoOutputThe non-alphanumeric characters removed gives the string as ThisissampleonlyThe function ‘preg_replace’ is used to remove alphanumeric characters from the string. A regular expression is used to filter out the alphanumeric characters. The string is previously defined and the function ‘preg_replace’ is called on this and the reformed string is displayed on the console.Example Live DemoOutputThe non-alphanumeric characters removed gives the string as Thisisasample_onlyThe only difference here is that a different regular expression is used. It means the same as the previous regular expression, but written in a different fashion.Read More

Advertisements