Problem Solution: Special Pythagorean triplet(projecteuler.net)

Future Technology

public class SpecialPythagoreanTriplet {

public static void main(String[] args) {
// TODO Auto-generated method stub

long sum = 0, total = 1000;

for (double a = 1; a <= total / 3; a++) {
for (double b = 1; b <= total / 2; b++) {

double c = total - a - b;
double e = Math.pow(a, 2);
double f = Math.pow(b, 2);
double g = Math.pow(c, 2);

sum = (long) (a * b * c);

if (Math.pow(a, 2) + Math.pow(b, 2) == Math.pow(c, 2)) {
System.out.println("a: " + a + " b: " + b + " c: " + c);
System.out.println("e: " + e + " f: " + f + " g: " + g);
System.out.println("Sum: " + sum);
}
}
}

}

}

View original post

Problem Solution: Special Pythagorean triplet(projecteuler.net)

public class SpecialPythagoreanTriplet {

public static void main(String[] args) {
// TODO Auto-generated method stub

long sum = 0, total = 1000;

for (double a = 1; a <= total / 3; a++) {
for (double b = 1; b <= total / 2; b++) {

double c = total - a - b;
double e = Math.pow(a, 2);
double f = Math.pow(b, 2);
double g = Math.pow(c, 2);

sum = (long) (a * b * c);

if (Math.pow(a, 2) + Math.pow(b, 2) == Math.pow(c, 2)) {
System.out.println("a: " + a + " b: " + b + " c: " + c);
System.out.println("e: " + e + " f: " + f + " g: " + g);
System.out.println("Sum: " + sum);
}
}
}

}

}

Constants php

Syntax

You can define a constant by using the define()-function or by using the const keyword outside a class definition as of PHP 5.3.0. Once a constant is defined, it can never be changed or undefined.

Note: Constants and (global) variables are in a different namespace. This implies that for example TRUE and $TRUE are generally different.

Example #1 Defining Constants

<?php
define
("CONSTANT""Hello world.");
echo 
CONSTANT// outputs "Hello world."
echo Constant// outputs "Constant" and issues a notice.
?>


Example #2 Defining Constants using the const keyword

<?php
// Works as of PHP 5.3.0
const CONSTANT 'Hello World';

echo CONSTANT;
?>

Conditional stylesheets

<!--[if lte IE 8]><link rel="stylesheet" href="lte-ie-8.css"><![endif]--> <!--[if lte IE 7]><link rel="stylesheet" href="lte-ie-7.css"><![endif]--> <!--[if lte IE 6]><link rel="stylesheet" href="lte-ie-6.css"><![endif]-->
**************************************************************************
/* Main stylesheet */ .foo { color: black; } /* lte-ie-8.css, for IE8 and older */ .foo { color: green; } /* lte-ie-7.css, for IE7 and older */ .foo { color: blue; } /* lte-ie-6.css, for IE6 and older */ .foo { color: red; }
**************************************************************************
<!--[if lt IE 7]><html><![endif]-->
<!--[if IE 7]>   <html><![endif]--> <!--[if IE 8]>   <html><![endif]--> <!--[if gt IE 8]><!--><html><!--<![endif]-->
**************************************************************************
.foo { color: black; }
.ie8 .foo { color: green; } /* IE8 */ .ie7 .foo { color: blue; } /* IE7 */ .ie6 .foo { color: red; } /* IE6 and IE5 (but who cares, right?) */
**************************************************************************
.foo {
  color: black;   color: green\9; /* IE8 and older, but there’s more… */   *color: blue; /* IE7 and older */   _color: red; /* IE6 and older */ }
**************************************************************************
 
 
 

Adding a ‘More’ link on Drupal

Main topic described: Block system, Render arrays, Menu system
Main function described: drupal_set_title()

With this last addition to the module, we will pull together what you have learned about the block system, the menu system, and render arrays, and throw in a workaround for a minor bug in the current version of Drupal 7.

It may have occurred to you that access to this page belongs not in the ‘Navigation’ menu, but through a ‘More’ link at the bottom of the ‘Current posts’ block. That was the plan all along, but we started with a menu link to show how it works.

If you looked at the Default theme implementations referenced back in Generating block content, you may have noticed the theme_more_link theme hook. We’ll use that, along with a theme hook suggestion, to theme the ‘More’ link for our block.

Child render elements

We’ll start by converting the call to theme_item_list into a render array as we did in the page function. We’ll also make it a child of block[‘content’] to allow for the ‘More’ link as a sibling. Here’s the revised code for the last section of current_posts_block_view:

<?php
else {
//Pass data through theme function.
$block[‘content’][‘posts’] = array(
‘#theme’ => ‘item_list__current_posts__block’,
‘#items’ => $items,
);
?>

Remember not to include the PHP markers in your code.

We move the item list code to $block[‘content’][‘posts’], making it a child element. You can name the child element whatever you like, as long as it doesn’t start with a hash mark (#). We convert the call to theme_item_list into a render array, then add the __current_posts theme hook suggestion. This we follow with a second suggestion in case a themer wants to render the lists in the block and page differently.

Here’s new code for the ‘More’ link to add directly after the last listing:

<?php
//Add a link to the page for more entries.
$block[‘content’][‘more’] = array(
‘#theme’ => ‘more_link__current_posts’,
‘#url’ => ‘current_posts’,
‘#title’ => t(‘See the full list of current posts.’),
);
}
?>

Here we make the ‘More’ link a sibling to the posts array and provide the two parameters this theme hook requires, the path and title. The title provides text which will appear as a tooltip when the mouse hovers over the link. We give this theme hook a suggestion as well.

Edit current_posts_menu()

At this point, we have links to the page both in our block and in the ‘Navigation’ menu. We don’t want a link in the menu, so we’ll edit current_posts_menu() to take it off. All you need to do is change the type attribute for the page item. Delete MENU_NORMAL_ITEM and replace it with MENU_CALLBACK. This type provides a path and attributes only, with no menu link. Should you have your module enabled, you’ll need to disable then re-enable it for this change to take effect.

Page title fix

Enable your module and follow the ‘More’ link in the block, then note the page title. ‘Home’? Not what we had in mind. This is caused by a bug in the first version of Drupal 7. (If you see the ‘Current posts’ title, you are using a later version that is not in release at the time of this writing.) There is a patch in the works, but we need to code for the current release. Even when the bug is fixed, we can’t be certain that our module won’t be used on an installation that has not been updated.

The workaround is to use the function drupal_set_title(). Add the following to the beginning of your _current_posts_page() function:

<?php
drupal_set_title(‘Current posts’);
?>

With this addition, you can be sure the correct title will appear on the page, no matter which version of Drupal 7 your module is installed in.

Check

Check your module functionality one final time. If you have been following along, the last thing to check is the corrected page title, ‘Current posts’. If you have problems getting your code additions to appear, try clearing the caches or disabling then re-enabling your module.

View the code

You can view all the code for the .module file here: current_posts.module

See also