function GCD($x, $y)
{
    if ($x % $y === 0) {
        return $y;
    }
    return GCD($y, $x % $y);
}
Example #2
0
/**
 * @param $n
 * @param $k
 * @return float
 */
function C($n, $k)
{
    $numerator = 1;
    $denominator = 1;
    if ($k > $n / 2) {
        $k = $n - $k;
    }
    for ($i = $k; $i; $i--) {
        $toMul = $n - $k + $i;
        $toDiv = $i;
        $g = GCD($toMul, $toDiv);
        $toMul /= $g;
        $toDiv /= $g;
        $g = GCD($numerator, $toDiv);
        $numerator /= $g;
        $toDiv /= $g;
        $g = GCD($toMul, $denominator);
        $toMul /= $g;
        $toDiv /= $g;
        $numerator *= $toMul;
        $denominator *= $toDiv;
    }
    return $numerator / $denominator;
}
Example #3
0
 /**
  * Image View
  *
  * Display's an uploaded image & it's comments
  *
  * @access	public
  * @return	void
  */
 function image()
 {
     // Retrieve the image if it exists or redirect to gallery
     $image = $this->gallery->get_gallery_item(array('gallery_slug' => $this->uri->segment(3, '')));
     if (!$image) {
         $this->session->set_flashdata('message', 'The image you requested was not found');
         redirect('gallery');
     }
     // Format Timezone
     $image->date = $this->ClanCMS->timezone($image->date);
     // Aspect ratio
     if ($image->width && $image->height) {
         // Determine GCD
         function GCD($a, $b)
         {
             while ($b != 0) {
                 $remainder = $a % $b;
                 $a = $b;
                 $b = $remainder;
             }
             return abs($a);
         }
         // Compute AR
         $a = $image->width;
         $b = $image->height;
         $gcd = GCD($a, $b);
         $a = $a / $gcd;
         $b = $b / $gcd;
         $image->ratio = $a . ":" . $b;
     }
     // Retrieve Uploader's avatar
     $user = $this->users->get_user(array('user_name' => $image->uploader));
     $image->avatar = $user->user_avatar;
     $image->uploader_id = $user->user_id;
     // Retrieve uploader's group
     if ($group = $this->users->get_group(array('group_id' => $user->group_id))) {
         // Group exist's assign user group
         $image->group = $group->group_title;
     } else {
         // Group doesn't exist, assign user group
         $image->group = '';
     }
     // Retrieve our forms
     $desc = $this->input->post('add_desc');
     $comment = $this->input->post('add_comment');
     // Someone is updating the description
     if ($desc && $this->user->logged_in()) {
         // Set form validation rules
         $this->form_validation->set_rules('desc', 'Description', 'trim|required');
         // Form validation passed & checked if gallery allows comments, so continue
         if (!$this->form_validation->run() == FALSE) {
             // Set up our data
             $data = array('desc' => $this->input->post('desc'));
             // Insert the comment into the database
             $this->gallery->edit_desc($data, $image->gallery_id);
             // Alert the user
             $this->session->set_flashdata('message', 'Your description has been edited!');
             // Redirect the user
             redirect('gallery/image/' . $image->gallery_slug);
         }
     }
     // Check if add comment has been posted and check if the user is logged in
     if ($comment && $this->user->logged_in()) {
         // Set form validation rules
         $this->form_validation->set_rules('comment', 'Comment', 'trim|required');
         // Form validation passed & checked if gallery allows comments, so continue
         if (!$this->form_validation->run() == FALSE) {
             // Add to comments count
             $count = $image->comments;
             $count = $count + 1;
             // Set up the data
             $data = array('comments' => $count);
             //  Update comment count
             $this->gallery->edit_desc($data, $image->gallery_id);
             // Set up our data
             $data = array('gallery_id' => $image->gallery_id, 'user_id' => $this->session->userdata('user_id'), 'comment_title' => $this->input->post('comment'), 'comment_date' => mdate('%Y-%m-%d %H:%i:%s', now()));
             // Insert the comment into the database
             $this->gallery->insert_comment($data);
             // Alert the user
             $this->session->set_flashdata('message', 'Your comment has been posted!');
             // Redirect the user
             redirect('gallery/image/' . $image->gallery_slug);
         }
     }
     // Retrieve the current page
     $page = $this->uri->segment(5, '');
     // Check if page exists
     if ($page == '') {
         // Page doesn't exist, assign it
         $page = 1;
     }
     //Set up the variables
     $per_page = 15;
     $total_results = $this->gallery->count_comments(array('gallery_id' => $image->gallery_id));
     $offset = ($page - 1) * $per_page;
     $pages->total_pages = 0;
     // Create the pages
     for ($i = 1; $i < $total_results / $per_page + 1; $i++) {
         // Itterate pages
         $pages->total_pages++;
     }
     // Check if there are no results
     if ($total_results == 0) {
         // Assign total pages
         $pages->total_pages = 1;
     }
     // Set up pages
     $pages->current_page = $page;
     $pages->pages_left = 9;
     $pages->first = (bool) ($pages->current_page > 5);
     $pages->previous = (bool) ($pages->current_page > '1');
     $pages->next = (bool) ($pages->current_page != $pages->total_pages);
     $pages->before = array();
     $pages->after = array();
     // Check if the current page is towards the end
     if ($pages->current_page + 5 < $pages->total_pages) {
         // Current page is not towards the end, assign start
         $start = $pages->current_page - 4;
     } else {
         // Current page is towards the end, assign start
         $start = $pages->current_page - $pages->pages_left + ($pages->total_pages - $pages->current_page);
     }
     // Assign end
     $end = $pages->current_page + 1;
     // Loop through pages before the current page
     for ($page = $start; $page < $pages->current_page; $page++) {
         // Check if the page is vaild
         if ($page > 0) {
             // Page is valid, add it the pages before, increment pages left
             $pages->before = array_merge($pages->before, array($page));
             $pages->pages_left--;
         }
     }
     // Loop through pages after the current page
     for ($page = $end; $pages->pages_left > 0 && $page <= $pages->total_pages; $page++) {
         // Add the page to pages after, increment pages left
         $pages->after = array_merge($pages->after, array($page));
         $pages->pages_left--;
     }
     // Set up pages
     $pages->last = (bool) ($pages->total_pages - 5 > $pages->current_page);
     $comments = $this->gallery->get_comments($per_page, $offset, array('gallery_id' => $image->gallery_id));
     // Check if comments exist
     if ($comments) {
         // Comments exist, loop through each comment
         foreach ($comments as $comment) {
             // Retrieve the user
             if ($user = $this->users->get_user(array('user_id' => $comment->user_id))) {
                 // User exists, assign comment author & comment avatar
                 $comment->author = $user->user_name;
                 $comment->avatar = $user->user_avatar;
             } else {
                 // User doesn't exist, assign comment author & comment avatar
                 $comment->author = '';
                 $comment->avatar = '';
             }
             // Format and assign the comment date
             $comment->date = $this->ClanCMS->timezone($comment->comment_date);
             // Do not count uploader comments
             if ($comment->author == $image->uploader) {
                 // Assign 0 for count
                 $comment->count = 0;
             } else {
                 // Give a count point
                 $comment->count = 1;
             }
             // Create array to hold comment points
             $count[] = $comment->count;
         }
         // Assign comment points
         $image->comments = array_sum($count);
     }
     // Fetch active user
     $user = $this->users->get_user(array('user_id' => $this->session->userdata('user_id')));
     // Query tracking table
     if ($user) {
         // set up data
         $data = array('controller_name' => $this->uri->segment(1), 'controller_method' => $this->uri->segment(2), 'controller_item_id' => $this->uri->segment(3), 'user_id' => $user->user_id);
         // Check user against tracker
         $track = $this->tracker->check($data);
         if (!$track) {
             // Object is new to user
             $this->tracker->track($data);
         }
     }
     // Count views
     if ($user && !$track) {
         // Hot update view count
         $views = $image->views + 1;
         $image->views = $views;
         $this->db->where('gallery_id', $image->gallery_id)->update('gallery', array('views' => $image->views));
     }
     // Add FB share link
     $image->fblink = 'http://www.facebook.com/sharer.php?s=100&amp;p[title]=' . BASE_URL . 'gallery/image/' . $image->gallery_slug . '&amp;p[summary]=' . $image->desc . '&amp;p[url]=' . BASE_URL . '&amp;p[images][0]=' . IMAGES . 'gallery/thumbs/' . $image->image;
     // Create references
     $this->data->image =& $image;
     $this->data->comments =& $comments;
     $this->data->pages =& $pages;
     $this->data->user =& $user;
     // Load the gallery view
     $this->load->view(THEME . 'image', $this->data);
 }