**CODE:- **

import java.util.*; class PalindromeExample2 { public static void main(String args[]) { String original, reverse = ""; // Objects of String class Scanner in = new Scanner(System.in); System.out.println("Enter a string/number to check if it is a palindrome"); original = in.nextLine(); int length = original.length(); for ( int i = length - 1; i >= 0; i-- ) reverse = reverse + original.charAt(i); if (original.equals(reverse)) System.out.println("Entered string/number is a palindrome."); else System.out.println("Entered string/number isn't a palindrome."); } }

**OUTPUT:- **

3553 is Palindrome.

]]>

**CODE:- **

import java.util.Scanner; public class Palindrome { public static void main(String args[]) { String a, b = ""; Scanner s = new Scanner(System.in); System.out.print("Enter the string you want to check:"); a = s.nextLine(); int n = a.length(); for(int i = n - 1; i >= 0; i--) { b = b + a.charAt(i); } if(a.equalsIgnoreCase(b)) { System.out.println("The string is palindrome."); } else { System.out.println("The string is not palindrome."); } } }

**OUTPUT:-**

Enter the string you want to check:NeveroddorEVen The string is palindrome.

]]>

**CODE:- **

public class Main { public static void main(String[] args) { int num = 29; boolean flag = false; for (int i = 2; i <= num / 2; ++i) { // condition for nonprime number if (num % i == 0) { flag = true; break; } } if (!flag) System.out.println(num + " is a prime number."); else System.out.println(num + " is not a prime number."); } }

**OUTPUT:-**

29 is a prime number.

]]>

**CODE:-**

import java.util.Scanner; public class FibonacciCalculator { public static void main(String args[]) { //input to print Fibonacci series upto how many numbers System.out.println("Enter number upto which Fibonacci series to print: "); int number = new Scanner(System.in).nextInt(); System.out.println("Fibonacci series upto " + number +" numbers : "); //printing Fibonacci series upto number for(int i=1; i<=number; i++) { System.out.print(fibonacci2(i) +" "); } } public static int fibonacci(int number) { if(number == 1 || number == 2) { return 1; } return fibonacci(number-1) + fibonacci(number -2); //tail recursion } public static int fibonacci2(int number) { if(number == 1 || number == 2) { return 1; } int fibo1=1, fibo2=1, fibonacci=1; for(int i= 3; i<= number; i++) { //Fibonacci number is sum of previous two Fibonacci number fibonacci = fibo1 + fibo2; fibo1 = fibo2; fibo2 = fibonacci; } return fibonacci; //Fibonacci number } }

**OUTPUT:- **

**Enter number upto which Fibonacci series to print:**

12

**Fibonacci series upto 12 numbers : **

1 1 2 3 5 8 13 21 34 55 89 144

]]>

let text = ""; const today = new Date(); const someday = new Date(); someday.setFullYear(2100, 0, 14); if (someday > today) { text = "Today is before January 14, 2100."; } else { text = "Today is after January 14, 2100."; }]]>

Canvas draws 2D graphics, on the fly (with JavaScript).

SVG is XML-based, which means that every element is available within the SVG DOM. You can attach JavaScript event handlers for an element.

In SVG, each drawn shape is remembered as an object. If attributes of an SVG object are changed, the browser can automatically re-render the shape.

Canvas is rendered pixel by pixel. In canvas, once the graphic is drawn, it is forgotten by the browser. If its position should be changed, the entire scene needs to be redrawn, including any objects that might have been covered by the graphic.

]]>The easiest way to think of an assertion is to liken it to a **raise-if** statement (or to be more accurate a raise-if-not statement). An expression is tested, and if the result comes up false, an exception is raised.

assert Expression

def KelvinToFahrenheit(Temperature): assert (Temperature >= 0),"Colder than absolute zero!" return ((Temperature-273)*1.8)+32 print KelvinToFahrenheit(273) print int(KelvinToFahrenheit(505.78)) print KelvinToFahrenheit(-5) When the above code is executed, it]]>

Now, if we look at the code

if (data >= 128) sum += data;

we can find that the meaning of this particular `if... else...`

branch is to add something when a condition is satisfied. This type of branch can be easily transformed into a **conditional move** statement, which would be compiled into a conditional move instruction: `cmovl`

, in an `x86`

system. The branch and thus the potential branch prediction penalty is removed.

In `C`

, thus `C++`

, the statement, which would compile directly (without any optimization) into the conditional move instruction in `x86`

, is the ternary operator `... ? ... : ...`

. So we rewrite the above statement into an equivalent one:

sum += data >=128 ? data : 0;

While maintaining readability, we can check the speedup factor.

On an Intel Core i7-2600K @ 3.4 GHz and Visual Studio 2010 Release Mode, the benchmark is:

**x86**

Scenario | Time (seconds) |
---|---|

Branching - Random data | 8.885 |

Branching - Sorted data | 1.528 |

Branchless - Random data | 3.716 |

Branchless - Sorted data | 3.71 |

**x64**

Scenario | Time (seconds) |
---|---|

Branching - Random data | 11.302 |

Branching - Sorted data | 1.830 |

Branchless - Random data | 2.736 |

Branchless - Sorted data | 2.737 |

The result is robust in multiple tests. We get a great speedup when the branch result is unpredictable, but we suffer a little bit when it is predictable. In fact, when using a conditional move, the performance is the same regardless of the data pattern.

Now let's look more closely by investigating the `x86`

assembly they generate.

For simplicity, we use two functions `max1`

and `max2`

.

`max1`

uses the conditional branch `if... else ...`

:

int max1(int a, int b) { if (a > b) return a; else return b; }

`max2`

uses the ternary operator `... ? ... : ...`

:

int max2(int a, int b) { return a > b ? a : b; }

`max2`

uses much less code due to the usage of instruction `cmovge`

. But the real gain is that `max2`

does not involve branch jumps, `jmp`

, which would have a significant performance penalty if the predicted result is not right.

]]>

Starting with the original loop:

for (unsigned i = 0; i < 100000; ++i) { for (unsigned j = 0; j < arraySize; ++j) { if (data >= 128) sum += data; } }

With loop interchange, we can safely change this loop to:

for (unsigned j = 0; j < arraySize; ++j) { for (unsigned i = 0; i < 100000; ++i) { if (data >= 128) sum += data; } }

Then, you can see that the if conditional is constant throughout the execution of the i loop, so you can hoist the if out:

for (unsigned j = 0; j < arraySize; ++j) { if (data >= 128) { for (unsigned i = 0; i < 100000; ++i) { sum += data; } } }

Then, you see that the inner loop can be collapsed into one single expression, assuming the floating-point model allows it (/FP: fast is thrown, for example)

for (unsigned j = 0; j < arraySize; ++j) { if (data >= 128) { sum += data * 100000; } }

That one is 100,000 times faster than before.

]]>

import java.util.Arrays; import java.util.Random; public class Main { public static void main(String[] args) { // Generate data int arraySize = 32768; int data[] = new int; Random rnd = new Random(0); for (int c = 0; c < arraySize; ++c) data = rnd.nextInt() % 256; // !!! With this, the next loop runs faster Arrays.sort(data); // Test long start = System.nanoTime(); long sum = 0; for (int i = 0; i < 100000; ++i) { for (int c = 0; c < arraySize; ++c) { // Primary loop if (data >= 128) sum += data; } } System.out.println((System.nanoTime() - start) / 1000000000.0); System.out.println("sum = " + sum); } }

With a similar but less extreme result.

My first thought was that sorting brings the data into the cache, but then I thought how silly that was because the array was just generated.

- What is going on?
- Why is processing a sorted array faster than processing an unsorted array?

The code is summing up some independent terms, so the order should not matter.

]]>