Varun Jalandery
BAN USER/**
* Below code demonstrates the Newton's Method for computing the
* square root.
* Programming Language Used: Go
* @author Varun Jalandery <varun.jalandery@gmail.com>
*/
package main
import (
"fmt"
"math"
)
/**
* This method computes the initial seed for computing the
* square root for faster convergence.
* Uses Babylonian's Method
*/
func GetInitialEstimate(x float64) (z float64) {
p := 0
for ; x >= 100 ; {
x = x / 100
p++
}
z = math.Pow(10, float64 (p))
if x >= 10 {
z = 6 * z
} else {
z = 2 * z
}
return
}
/**
* This method computes the square root of the number
* Uses Newton's Method
*/
func Sqrt(x float64) (z float64) {
if x > 0 {
const PRECISION = 0.00001
seed := GetInitialEstimate(x)
var z2 float64 = 0
z = (seed + (x / seed)) / 2
for ; math.Abs(z - z2) > PRECISION ; {
z2 = z
z = (z + (x / z)) / 2
}
}
return
}
//Executing the program
func main() {
fmt.Println(Sqrt(100));
}
/**
* Below code demonstrates the Newton's Method for computing the
* square root.
* Programming Language Used: Go
* @author Varun Jalandery <varun.jalandery@gmail.com>
*/
package main
import (
"fmt"
"math"
)
/**
* This method computes the initial seed for computing the
* square root for faster convergence.
* Uses Babylonian's Method
*/
func GetInitialEstimate(x float64) (z float64) {
p := 0
for ; x >= 100 ; {
x = x / 100
p++
}
z = math.Pow(10, float64 (p))
if x >= 10 {
z = 6 * z
} else {
z = 2 * z
}
return
}
/**
* This method computes the square root of the number
* Uses Newton's Method
*/
func Sqrt(x float64) (z float64) {
if x > 0 {
const PRECISION = 0.00001
seed := GetInitialEstimate(x)
var z2 float64 = 0
fmt.Println(seed)
z = (seed + (x / seed)) / 2
for ; math.Abs(z - z2) > PRECISION ; {
z2 = z
z = (z + (x / z)) / 2
}
}
return
}
//Executing the program
func main() {
fmt.Println(Sqrt(100));
}
/**
* Below code demonstrates the Newton's Method for computing the
* square root.
* Programming Language Used: Go
*/
package main
import (
"fmt"
"math"
)
/**
* This method computes the initial seed for computing the
* square root for faster convergence.
* Uses Babylonian's Method
*/
func GetInitialEstimate(x float64) (z float64) {
p := 0
for ; x >= 100 ; {
x = x / 100
p++
}
z = math.Pow(10, float64 (p))
if x >= 10 {
z = 6 * z
} else {
z = 2 * z
}
return
}
/**
* This method computes the square root of the number
* Uses Newton's Method
*/
func Sqrt(x float64) (z float64) {
if x > 0 {
const PRECISION = 0.00001
seed := GetInitialEstimate(x)
var z2 float64 = 0
fmt.Println(seed)
z = (seed + (x / seed)) / 2
for ; math.Abs(z - z2) > PRECISION ; {
z2 = z
z = (z + (x / z)) / 2
}
}
return
}
//Executing the program
func main() {
fmt.Println(Sqrt(100));
}
/**
* Programming Language Used : PHP
*
* This method would return the two numbers which form the minimum sum.
*
* @author Varun Jalandery <varun.jalandery@gmail.com>
*
* @param array of numbers
* @return array of two numbers which form the minimum sum
*/
function getTwoMinimumSumFormingIntegers($array)
{
$numberOne = '';
$numberTwo = '';
sort($array); //sorting the array in ascending order
$i = 0;
while (isset($array[$i])) {
$numberOne .= $array[$i++];
if (isset($array[$i])) {
$numberTwo .= $array[$i++];
}
}
return array(
0 => (int) $numberOne,
1 => (int) $numberTwo,
);
}
/**
* Progamming Language Used : PHP
* Algorithm Used : Kadane's algorithm
*
* This method computes the maximum sum of subarray from the given array.
*
* @return int sum of sub array
*
* @author Varun Jalandery <varun.jalandery@gmail.com>
*/
function maximumSumOfSubArray(array $numbers)
{
// Initialize variables here
$maxSoFar = 0;
$maxEndingHere = 0;
for ($i = 0; $i < count($numbers); $i++) {
$maxEndingHere += (int) $numbers[$i];
if ($maxEndingHere < 0) {
$maxEndingHere = 0;
} else {
$maxSoFar = $maxEndingHere;
}
if ($maxSoFar < $maxEndingHere) {
$maxSoFar = $maxEndingHere;
}
}
return $maxSoFar;
}
/**
* Progamming Language Used : PHP
* Algorithm Used : Kadane's algorithm
*
* This method computes the maximum sum of subarray from the given array.
*
* @return int sum of sub array
*
* @author Varun Jalandery <varun.jalandery@gmail.com>
*/
function maximumSumOfSubArray(array $numbers)
{
// Initialize variables here
$maxSoFar = 0;
$maxEndingHere = 0;
for ($i = 0; $i < count($numbers); $i++) {
$maxEndingHere += (int) $numbers[$i];
if ($maxEndingHere < 0) {
$maxEndingHere = 0;
} else {
$maxSoFar = $maxEndingHere;
}
if ($maxSoFar < $maxEndingHere) {
$maxSoFar = $maxEndingHere;
}
}
return $maxSoFar;
}
- Varun Jalandery November 29, 2013/**
* Programming Language Used : PHP
*
* This method would return the two numbers which form the minimum sum.
*
* @author Varun Jalandery <varun.jalandery@gmail.com>
*
* @param array of numbers
* @return array of two numbers which form the minimum sum
*/
function getTwoMinimumSumFormingIntegers($array)
{
$numberOne = '';
$numberTwo = '';
sort($array); //sorting the array in ascending order
$i = 0;
while (isset($array[$i])) {
$numberOne .= $array[$i++];
$numberTwo .= $array[$i++];
}
return array(
0 => (int) $numberOne,
1 => (int) $numberTwo,
);
}
- Varun Jalandery November 28, 2013/**
* Programming Language used : PHP
* This method abbreviates the words in the $sentence.
* It would affect only words with alphabets only.
* e.g. "Human" will become "H3n". All non-alphabetic characters will be
* kept as it is.
*
* @author Varun Jalandery <varun.jalandery@gmail.com>
*
* @param string $sentence
* @return string abbreviated sentence
*/
function abbreviateSentence($sentence)
{
$result = '';
$length = strlen($sentence);
$isWordStarted = false;
$wordInnerLength = 0;
for ($i = 0; $i < $length - 1; $i++) {
if (preg_match('/^[A-Za-z]$/', $sentence[$i])) {
if ($isWordStarted) {
$wordInnerLength += 1;
} else {
$isWordStarted = true;
$result .= $sentence[$i];
}
} else {
if ($isWordStarted) {
if ($wordInnerLength - 1 > 0) {
$result .= (string) ($wordInnerLength - 1);
}
$result .= $sentence[$i - 1] . $sentence[$i];
$wordInnerLength = 0;
$isWordStarted = false;
} else {
$result .= $sentence[$i];
}
}
}
//handled the boundary case separately, otherwise for every iteration it would
//be required to test whether its a boundary case or not.
if (preg_match('/^[A-Za-z]$/', $sentence[$length - 1])) {
if ($isWordStarted) {
if ($wordInnerLength > 0) {
$result .= (string) ($wordInnerLength);
}
}
} else {
if ($isWordStarted) {
if ($wordInnerLength - 1 > 0) {
$result .= (string) ($wordInnerLength - 1) . $sentence[$length - 2];
}
}
}
$result .= $sentence[$length - 1];
return $result;
}
/**
* Programming Language used : PHP
* This method abbreviates the words in the $sentence.
* It would affect only words with alphabets only.
* e.g. "Human" will become "H3n". All non-alphabetic characters will be
* kept as it is.
*
* @author Varun Jalandery <varun.jalandery@gmail.com>
*
* @param string $sentence
* @return string abbreviated sentence
*/
function abbreviateSentence($sentence)
{
$result = '';
$length = strlen($sentence);
$isWordStarted = false;
$wordInnerLength = 0;
for ($i = 0; $i < $length - 1; $i++) {
if (preg_match('/^[A-Za-z]$/', $sentence[$i])) {
if ($isWordStarted) {
$wordInnerLength += 1;
} else {
$isWordStarted = true;
$result .= $sentence[$i];
}
} else {
if ($isWordStarted) {
if ($wordInnerLength - 1 > 0) {
$result .= (string) ($wordInnerLength - 1);
}
$result .= $sentence[$i - 1] . $sentence[$i];
$wordInnerLength = 0;
$isWordStarted = false;
} else {
$result .= $sentence[$i];
}
}
}
if ($isWordStarted) {
if ($wordInnerLength > 0) {
$result .= (string) ($wordInnerLength);
}
}
$result .= $sentence[$i];
return $result;
}
- Varun Jalandery November 25, 2013There is one mistake in the comments section. "Human" would become H3n and not H4n.
- Varun Jalandery November 24, 2013/**
* Programming Language used : PHP
* This method abbreviates the words in the $sentence.
* It would affect only words with alphabets only.
* e.g. "Human" will become "H4n". All non-alphabetic characters will be
* kept as it is.
*
* @author Varun Jalandery <varun.jalandery@gmail.com>
*
* @param string $sentence
* @return string abbreviated sentence
*/
function abbreviateSentence($sentence) {
$result = '';
$length = strlen($sentence);
$isWordStarted = false;
$wordInnerLength = 0;
for ($i = 0; $i < $length; $i++) {
if (preg_match('/^[A-Za-z]$/', $sentence[$i])) {
if ($isWordStarted) {
$wordInnerLength += 1;
} else {
$isWordStarted = true;
$result .= $sentence[$i];
}
} else {
if ($isWordStarted) {
$result .= (string) ($wordInnerLength - 1) . $sentence[$i - 1] . $sentence[$i];
$wordInnerLength = 0;
$isWordStarted = false;
} else {
$result .= $sentence[$i];
}
}
}
return $result;
}
- Varun Jalandery November 24, 2013
- Varun Jalandery March 27, 2014