Example #1
0
/*
** Calculate and show the exchange rates.
*/
void t_dialog_marketplace::show_exchange_rates()
{
    int i;
    int player_item_cost;
    int numerator;
    int denominator;
    int gcd;
	t_material_array const& funds = m_player_ptr->get_funds();

    if (m_player_selected == false)
        return;

    // Player:market ratio.
    player_item_cost = k_material_value[m_player_item_index];

    /*
    ** Calculate ratios for each market item and display it.
    */
	for (i=0; i<k_material_count; i++)
    {
        // Calculate exchange rate.  Market ratio number * material cost.
        denominator = m_market_efficiency_ratio * k_material_value[i];

        // Find greatest common denominator.
        gcd = greatest_common_divisor ( player_item_cost, denominator );

        // Find ratio  numerator:denominator
        numerator = player_item_cost/gcd;
        denominator /= gcd;
                
        // Market exchange text.  
        if (i == m_player_item_index)
        {
            // Same item, not applicable.
    	    m_market_exchange_text[i]->set_text( k_text_dialog_marketplace_not_applicable );
        }
        else if (i == k_gold)
        {
            // For gold we don't want a ratio, just a price per unit (since the ratios tend to be very high)
			numerator /= denominator;
			denominator = 1;
    	    m_market_exchange_text[i]->set_text( format_string( "%d", numerator ));
        }
        else
        {
            // Show ratio.
    	    m_market_exchange_text[i]->set_text( format_string( "%d / %d", numerator, denominator ) );
        }

        // For the market item, calculate the maximum that can be purchased.
        if (i == m_market_item_index && can_buy())
        {
            m_cost_numerator = numerator;
            m_cost_denominator = denominator;

            m_max_scroll_value = funds[m_player_item_index] / denominator;
	        m_scrollbar->set_limits( 0, m_max_scroll_value );
        }
    }
}
Example #2
0
static int least_common_multiple(int a, int b)
{
  int i;
  int gcd = greatest_common_divisor(a, b);

  if (gcd == 1) return a * b;

  return a * b / gcd;
}
Example #3
0
static int greatest_common_divisor(int a, int b)
{
  int l, s;
  int mod;

  if (a > b) { l = a; s = b; }
  else       { l = b; s = a; }

  mod = l % s;
  if (mod == 0) return s;

  return greatest_common_divisor(mod, s);
}
Example #4
0
static void add_fraction(fraction *a, fraction *b, fraction *result)
{
  int lcm_denominator = 
    least_common_multiple(a->denominator, b->denominator);
  int gcd, a_num, b_num;
  fraction ret;

  ret.denominator = lcm_denominator;
  ret.numerator = (lcm_denominator / a->denominator * a->numerator) + 
                  (lcm_denominator / b->denominator * b->numerator);

  gcd = greatest_common_divisor(ret.denominator, ret.numerator);
  if (gcd != 1) {
    ret.denominator /= gcd;
    ret.numerator /= gcd;
  }

  *result = ret;
}
Example #5
0
int main() {
    freopen(INPUT, "r", stdin);
    freopen(OUTPUT, "w", stdout);
    int N;
    scanf("%d", &N);
    int a, b;
    char temp[9];
    for (b = 1; b <= N; b++) {
        for (a = 0; a <= b; a++) {
            if (greatest_common_divisor(a, b) <= 1) {
                sprintf(temp, "%d/%d\n", a, b);
                strcpy(bkts[50880 * a / b], temp);
            }
        }

    }
    for (a = 0; a <= 50880; a++) {
        if (strlen(bkts[a]) >= 3) {
            printf("%s", bkts[a]);
        }
    }
    exit(0);
}
//	DDCPP/math/bits/DD_lowest_common_multiple.hpp
#ifndef DD_LOWEST_COMMON_MULTIPLE_HPP_INCLUDED_
#	define DD_LOWEST_COMMON_MULTIPLE_HPP_INCLUDED_ 1



#	include "DD_greatest_common_divisor.hpp"



DD_BEGIN_
template <typename IntegerT_>
inline IntegerT_ DD_CONSTEXPR lowest_common_multiple(
	IntegerT_ const& integer_1__,
	IntegerT_ const& integer_2__
) DD_NOEXCEPT_AS(static_cast<IntegerT_>(integer_1__ * integer_2__ / greatest_common_divisor(integer_1__, integer_2__))) {
	return integer_1__ * integer_2__ / greatest_common_divisor(integer_1__, integer_2__);
}



DD_END_



#endif
Example #7
0
unsigned int
least_common_multiple(unsigned int a, unsigned int b) 
{
	return (unsigned int) a * b / greatest_common_divisor(a,b);
}
// --------------------------------------------------------------------------
// t_combat_object_model_base class
// --------------------------------------------------------------------------
t_compound_object_model::t_compound_object_model( t_combat_object_model_24 const& source,
												  double scale )
{
	t_bitmap_group bitmaps;

	m_type = source.get_type();
	if (scale == 1.0)
	{
		bitmaps = t_bitmap_group( source.get_frames() );
	}
	else
	{
		t_bitmap_group_24 frames = source.get_frames();

		frames.offset( source.get_offset() );
		scale_group( frames, bitmaps, scale );
		bitmaps.offset( -source.get_offset() );
	}

	int segment_size;

	m_footprint_size = source.get_footprint_size();
	// determine size of subchunks.  1 for passable objects, as large as possible for
	// others
	if (source.blocks_movement() || source.is_underlay() 
		|| source.get_type() == k_obstacle_special || source.get_type() == k_obstacle_castle_tower)
		segment_size = greatest_common_divisor( m_footprint_size.row,
											    m_footprint_size.column );
	else
		segment_size = 1;

	// handle case where this isn't really compound
	if ((segment_size == m_footprint_size.row && segment_size == m_footprint_size.column)
		|| m_footprint_size.row == 0 || m_footprint_size.column == 0 )
	{
		t_object_segment* segment;

		segment = new t_object_segment( source, segment_size, t_map_point_2d(0,0), source,
			                            bitmaps );
		m_segments.push_back( segment );
		return;
	}

	t_map_point_3d		cell_offset(0,0,0);
	t_screen_rect		image_rect = bitmaps.get_rect();
	t_screen_point      shape_origin;
	t_screen_point		base_point;
	t_screen_point		left_tile_base;
	t_screen_point		right_tile_base;
	t_screen_point      bottom_tile_base;
	t_bitmap_line		line;
	t_bitmap_line_array lines;
	int					i;
	int					center;
	int					width;
	int					image_width = image_rect.width();
	int					half_segment_height;
	int					segment_height;
	t_map_point_2d      left_offset( segment_size, 0 );
	t_map_point_2d      right_offset( 0, segment_size );
	t_map_point_2d      down_offset( segment_size, segment_size );

	// chop image up into segment_size x segment_size chunks
	cell_offset.height = source.get_height();
	for (cell_offset.row = 0; cell_offset.row < m_footprint_size.row;
	     cell_offset.row += segment_size)
	{
		for (cell_offset.column = 0; cell_offset.column < m_footprint_size.column;
		     cell_offset.column += segment_size)
		{
			// find screen coordinate of top of tile
			base_point = get_base_point( cell_offset, scale ) - source.get_offset();
			left_tile_base = get_base_point( cell_offset + left_offset, scale )
				             - source.get_offset();
			right_tile_base = get_base_point( cell_offset + right_offset, scale )
				              - source.get_offset();
			bottom_tile_base = get_base_point( cell_offset + down_offset, scale )
				               - source.get_offset();
			lines.clear();
			shape_origin.x = image_rect.left;
			center = base_point.x - shape_origin.x;
			left_tile_base.x -= image_rect.left;
			right_tile_base.x -= image_rect.left;
			if (cell_offset.row == 0 && cell_offset.column == 0)
			{   // for the topmost tile only, include all the lines above half the height
				shape_origin.y = image_rect.top;
				line.left = 0;
				line.right = image_width;
				for (i = shape_origin.y; i < base_point.y; i++)
					lines.push_back( line );
			}
			else
			{
				shape_origin.y = base_point.y;
			}

			// create top half of mask
			half_segment_height = left_tile_base.y - base_point.y;
			for (i = 0; i < half_segment_height; i++)
			{
				width = 2 * (i + 1) * scale;
				if (cell_offset.column == 0)
					line.left = 0;
				else
					line.left = center - width;
				if (cell_offset.row == 0)
					line.right = image_width;
				else
					line.right = center + width;
				lines.push_back( line );
			}
			// create bottom half of mask
			segment_height = bottom_tile_base.y - base_point.y - half_segment_height;
			for (i = 0; i < segment_height; i++)
			{
				width = 2 * (i + 1) * scale;

				if (cell_offset.row + segment_size != m_footprint_size.row)
					line.left = left_tile_base.x + width;
				else if (cell_offset.column == 0)
					line.left = 0;
				else
					line.left = left_tile_base.x;

				if (cell_offset.column + segment_size != m_footprint_size.column)
					line.right = right_tile_base.x - width;
				else if (cell_offset.row == 0)
					line.right = image_width;
				else
					line.right = right_tile_base.x;

				lines.push_back( line );
			}
			if (cell_offset.row + segment_size == m_footprint_size.row
				|| cell_offset.column + segment_size == m_footprint_size.column)
			{
				if (cell_offset.row + segment_size != m_footprint_size.row)
					line.left = center;
				else if (cell_offset.column == 0)
					line.left = 0;
				else
					line.left = left_tile_base.x;

				if (cell_offset.column + segment_size != m_footprint_size.column)
					line.right = center;
				else if (cell_offset.row == 0)
					line.right = image_width;
				else
					line.right = right_tile_base.x;

				for (i = base_point.y + segment_height + half_segment_height; 
				     i < image_rect.bottom; i++)
					lines.push_back( line );
			}

			t_bitmap_group    segment_bitmaps;
			t_object_segment* segment;

			copy_bitmaps( bitmaps, segment_bitmaps, shape_origin, lines );
			if (segment_bitmaps.get_rect().width() == 0)
				continue;

			segment = new t_object_segment( source, segment_size,
				                            cell_offset << k_battlefield_subcell_shift,
											source, segment_bitmaps );
			base_point.y += cell_offset.height * scale;
			segment->set_offset( -base_point );
			m_segments.push_back( segment );
		}
	}
}
Example #9
0
int gmx_genion(int argc, char *argv[])
{
    const char        *desc[] = {
        "[TT]genion[tt] replaces solvent molecules by monoatomic ions at",
        "the position of the first atoms with the most favorable electrostatic",
        "potential or at random. The potential is calculated on all atoms, using",
        "normal GROMACS particle-based methods (in contrast to other methods",
        "based on solving the Poisson-Boltzmann equation).",
        "The potential is recalculated after every ion insertion.",
        "If specified in the run input file, a reaction field, shift function",
        "or user function can be used. For the user function a table file",
        "can be specified with the option [TT]-table[tt].",
        "The group of solvent molecules should be continuous and all molecules",
        "should have the same number of atoms.",
        "The user should add the ion molecules to the topology file or use",
        "the [TT]-p[tt] option to automatically modify the topology.[PAR]",
        "The ion molecule type, residue and atom names in all force fields",
        "are the capitalized element names without sign. This molecule name",
        "should be given with [TT]-pname[tt] or [TT]-nname[tt], and the",
        "[TT][molecules][tt] section of your topology updated accordingly,",
        "either by hand or with [TT]-p[tt]. Do not use an atom name instead!",
        "[PAR]Ions which can have multiple charge states get the multiplicity",
        "added, without sign, for the uncommon states only.[PAR]",
        "With the option [TT]-pot[tt] the potential can be written as B-factors",
        "in a [TT].pdb[tt] file (for visualisation using e.g. Rasmol).",
        "The unit of the potential is 1000 kJ/(mol e), the scaling be changed",
        "with the [TT]-scale[tt] option.[PAR]",
        "For larger ions, e.g. sulfate we recommended using [TT]genbox[tt]."
    };
    const char        *bugs[] = {
        "Calculation of the potential is not reliable, therefore the [TT]-random[tt] option is now turned on by default.",
        "If you specify a salt concentration existing ions are not taken into account. In effect you therefore specify the amount of salt to be added."
    };
    static int         p_num   = 0, n_num = 0, p_q = 1, n_q = -1;
    static const char *p_name  = "NA", *n_name = "CL";
    static real        rmin    = 0.6, scale = 0.001, conc = 0;
    static int         seed    = 1993;
    static gmx_bool    bRandom = TRUE, bNeutral = FALSE;
    static t_pargs     pa[]    = {
        { "-np",    FALSE, etINT,  {&p_num}, "Number of positive ions"       },
        { "-pname", FALSE, etSTR,  {&p_name}, "Name of the positive ion"      },
        { "-pq",    FALSE, etINT,  {&p_q},   "Charge of the positive ion"    },
        { "-nn",    FALSE, etINT,  {&n_num}, "Number of negative ions"       },
        { "-nname", FALSE, etSTR,  {&n_name}, "Name of the negative ion"      },
        { "-nq",    FALSE, etINT,  {&n_q},   "Charge of the negative ion"    },
        { "-rmin",  FALSE, etREAL, {&rmin},  "Minimum distance between ions" },
        { "-random", FALSE, etBOOL, {&bRandom}, "Use random placement of ions instead of based on potential. The rmin option should still work" },
        { "-seed",  FALSE, etINT,  {&seed},  "Seed for random number generator" },
        { "-scale", FALSE, etREAL, {&scale}, "Scaling factor for the potential for [TT]-pot[tt]" },
        { "-conc",  FALSE, etREAL, {&conc},
          "Specify salt concentration (mol/liter). This will add sufficient ions to reach up to the specified concentration as computed from the volume of the cell in the input [TT].tpr[tt] file. Overrides the [TT]-np[tt] and [TT]-nn[tt] options." },
        { "-neutral", FALSE, etBOOL, {&bNeutral}, "This option will add enough ions to neutralize the system. These ions are added on top of those specified with [TT]-np[tt]/[TT]-nn[tt] or [TT]-conc[tt]. "}
    };
    gmx_mtop_t        *mtop;
    gmx_localtop_t    *top;
    t_inputrec         inputrec;
    t_commrec         *cr;
    t_mdatoms         *mdatoms;
    gmx_enerdata_t     enerd;
    t_graph           *graph;
    t_forcerec        *fr;
    rvec              *x, *v;
    real              *pot, vol, qtot;
    matrix             box;
    t_atoms            atoms;
    t_pbc              pbc;
    int               *repl;
    atom_id           *index;
    char              *grpname;
    gmx_bool          *bSet, bPDB;
    int                i, nw, nwa, nsa, nsalt, iqtot;
    FILE              *fplog;
    output_env_t       oenv;
    t_filenm           fnm[] = {
        { efTPX, NULL,  NULL,      ffREAD  },
        { efXVG, "-table", "table", ffOPTRD },
        { efNDX, NULL,  NULL,      ffOPTRD },
        { efSTO, "-o",  NULL,      ffWRITE },
        { efLOG, "-g",  "genion",  ffWRITE },
        { efPDB, "-pot", "pot",    ffOPTWR },
        { efTOP, "-p",  "topol",   ffOPTRW }
    };
#define NFILE asize(fnm)

    parse_common_args(&argc, argv, PCA_BE_NICE, NFILE, fnm, asize(pa), pa,
                      asize(desc), desc, asize(bugs), bugs, &oenv);
    bPDB = ftp2bSet(efPDB, NFILE, fnm);
    if (bRandom && bPDB)
    {
        fprintf(stderr, "Not computing potential with random option!\n");
        bPDB = FALSE;
    }

    /* Check input for something sensible */
    if ((p_num < 0) || (n_num < 0))
    {
        gmx_fatal(FARGS, "Negative number of ions to add?");
    }

    snew(mtop, 1);
    snew(top, 1);
    fplog = init_calcpot(ftp2fn(efLOG, NFILE, fnm), ftp2fn(efTPX, NFILE, fnm),
                         opt2fn("-table", NFILE, fnm), mtop, top, &inputrec, &cr,
                         &graph, &mdatoms, &fr, &enerd, &pot, box, &x, oenv);

    atoms = gmx_mtop_global_atoms(mtop);

    qtot = 0;
    for (i = 0; (i < atoms.nr); i++)
    {
        qtot += atoms.atom[i].q;
    }
    iqtot = gmx_nint(qtot);

    
    if (conc > 0)
    {
        /* Compute number of ions to be added */
        vol = det(box);
        nsalt = gmx_nint(conc*vol*AVOGADRO/1e24);
        p_num = abs(nsalt*n_q);
        n_num = abs(nsalt*p_q);
    }
    if (bNeutral)
    {
        int qdelta = p_num*p_q + n_num*n_q + iqtot;

        /* Check if the system is neutralizable
         * is (qdelta == p_q*p_num + n_q*n_num) solvable for p_num and n_num? */
        int gcd = greatest_common_divisor(n_q, p_q);
        if ((qdelta % gcd) != 0)
        {
            gmx_fatal(FARGS, "Can't neutralize this system using -nq %d and"
                    " -pq %d.\n", n_q, p_q);
        }
        
        while (qdelta != 0)
        {
            while (qdelta < 0)
            {
                p_num++;
                qdelta += p_q;
            }
            while (qdelta > 0)
            {
                n_num++;
                qdelta += n_q;
            }
        }
    }

    if ((p_num == 0) && (n_num == 0))
    {
        if (!bPDB)
        {
            fprintf(stderr, "No ions to add and no potential to calculate.\n");
            exit(0);
        }
        nw  = 0;
        nsa = 0; /* to keep gcc happy */
    }
    else
    {
        printf("Will try to add %d %s ions and %d %s ions.\n",
               p_num, p_name, n_num, n_name);
        printf("Select a continuous group of solvent molecules\n");
        get_index(&atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &nwa, &index, &grpname);
        for (i = 1; i < nwa; i++)
        {
            if (index[i] != index[i-1]+1)
            {
                gmx_fatal(FARGS, "The solvent group %s is not continuous: "
                          "index[%d]=%d, index[%d]=%d",
                          grpname, i, index[i-1]+1, i+1, index[i]+1);
            }
        }
        nsa = 1;
        while ((nsa < nwa) &&
               (atoms.atom[index[nsa]].resind ==
                atoms.atom[index[nsa-1]].resind))
        {
            nsa++;
        }
        if (nwa % nsa)
        {
            gmx_fatal(FARGS, "Your solvent group size (%d) is not a multiple of %d",
                      nwa, nsa);
        }
        nw = nwa/nsa;
        fprintf(stderr, "Number of (%d-atomic) solvent molecules: %d\n", nsa, nw);
        if (p_num+n_num > nw)
        {
            gmx_fatal(FARGS, "Not enough solvent for adding ions");
        }
    }

    if (opt2bSet("-p", NFILE, fnm))
    {
        update_topol(opt2fn("-p", NFILE, fnm), p_num, n_num, p_name, n_name, grpname);
    }

    snew(bSet, nw);
    snew(repl, nw);

    snew(v, atoms.nr);
    snew(atoms.pdbinfo, atoms.nr);

    set_pbc(&pbc, inputrec.ePBC, box);

    /* Now loop over the ions that have to be placed */
    do
    {
        if (!bRandom)
        {
            calc_pot(fplog, cr, mtop, &inputrec, top, x, fr, &enerd, mdatoms, pot, box, graph);
            if (bPDB || debug)
            {
                char buf[STRLEN];

                if (debug)
                {
                    sprintf(buf, "%d_%s", p_num+n_num, ftp2fn(efPDB, NFILE, fnm));
                }
                else
                {
                    strcpy(buf, ftp2fn(efPDB, NFILE, fnm));
                }
                for (i = 0; (i < atoms.nr); i++)
                {
                    atoms.pdbinfo[i].bfac = pot[i]*scale;
                }
                write_sto_conf(buf, "Potential calculated by genion",
                               &atoms, x, v, inputrec.ePBC, box);
                bPDB = FALSE;
            }
        }
        if ((p_num > 0) && (p_num >= n_num))
        {
            insert_ion(nsa, &nw, bSet, repl, index, pot, x, &pbc,
                       1, p_q, p_name, mdatoms, rmin, bRandom, &seed);
            p_num--;
        }
        else if (n_num > 0)
        {
            insert_ion(nsa, &nw, bSet, repl, index, pot, x, &pbc,
                       -1, n_q, n_name, mdatoms, rmin, bRandom, &seed);
            n_num--;
        }
    }
    while (p_num+n_num > 0);
    fprintf(stderr, "\n");

    if (nw)
    {
        sort_ions(nsa, nw, repl, index, &atoms, x, p_name, n_name);
    }

    sfree(atoms.pdbinfo);
    atoms.pdbinfo = NULL;
    write_sto_conf(ftp2fn(efSTO, NFILE, fnm), *mtop->name, &atoms, x, NULL,
                   inputrec.ePBC, box);

    thanx(stderr);

    gmx_log_close(fplog);

    return 0;
}
T least_common_multiple(const T&a , const T& b)
{
    return (a / greatest_common_divisor(a, b)) * b;
}
Example #11
0
j_llint greatest_common_divisor_simp(j_llint i_val_one, j_llint i_val_two){
	return greatest_common_divisor(i_val_one, i_val_two).front();
}
Example #12
0
bool relative_prime(j_llint i_val_one, j_llint i_val_two){
	return 1 == greatest_common_divisor(i_val_one, i_val_two).front();
}