int main(int argc, const char * argv[]) { // prime the primes primeFactors(63); // approach 1 uses the common prime factorization technique { TimedBlock _("a1-10"); std::cout << "approach1(10) = " << approach1(10) << "\n"; } { TimedBlock _("a1-20"); std::cout << "approach1(20) = " << approach1(20) << "\n"; } std::cout << "---------------\n"; // approach 2 uses the lcd(a,b) = (a/gcd(a,b))*b principle { TimedBlock _("a2-10"); std::cout << "approach2(10) = " << approach2(10) << "\n"; } { TimedBlock _("a2-20"); std::cout << "approach2(20) = " << approach2(20) << "\n"; } }
int main(int argc, const char * argv[]) { // verification against example number std::cout << "approach1 = " << approach1(13195) << "\n"; { TimedBlock _("find factors"); std::cout << "approach1 = " << approach1(600851475143) << "\n"; } }
int main() { const int m[4][4] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}}; int r[4]; int count, sum = 0; for(count = 0; (count < REPETITIONS); count++) { int v[4] = {count, count, count, 0}; approach1(m, v, r); // <---- Only modify this line! sum += r[2]; } printf("Final sum = %d\n", sum); return 0; }
void SpaceStationType::OnSetupComplete() { // Since the model contains (almost) all of the docking information we have to extract that // and then generate any additional locators and information the station will need from it. // First we gather the MatrixTransforms that contain the location and orientation of the docking // locators/waypoints. We store some information within the name of these which needs parsing too. // Next we build the additional information required for docking ships with SPACE stations // on autopilot - this is the only option for docking with SPACE stations currently. // This mostly means offsetting from one locator to create the next in the sequence. // ground stations have a "special-f*****g-case" 0 stage launch process shipLaunchStage = ((SURFACE == dockMethod) ? 0 : 3); // gather the tags SceneGraph::Model::TVecMT entrance_mts; SceneGraph::Model::TVecMT locator_mts; SceneGraph::Model::TVecMT exit_mts; model->FindTagsByStartOfName("entrance_", entrance_mts); model->FindTagsByStartOfName("loc_", locator_mts); model->FindTagsByStartOfName("exit_", exit_mts); Output("%s has:\n %lu entrances,\n %lu pads,\n %lu exits\n", modelName.c_str(), entrance_mts.size(), locator_mts.size(), exit_mts.size()); // Add the partially initialised ports for (auto apprIter : entrance_mts) { int portId; PiVerify(1 == sscanf(apprIter->GetName().c_str(), "entrance_port%d", &portId)); PiVerify(portId > 0); SPort new_port; new_port.portId = portId; new_port.name = apprIter->GetName(); if (SURFACE == dockMethod) { const vector3f offDir = apprIter->GetTransform().Up().Normalized(); new_port.m_approach[1] = apprIter->GetTransform(); new_port.m_approach[1].SetTranslate(apprIter->GetTransform().GetTranslate() + (offDir * 500.0f)); } else { const vector3f offDir = -apprIter->GetTransform().Back().Normalized(); new_port.m_approach[1] = apprIter->GetTransform(); new_port.m_approach[1].SetTranslate(apprIter->GetTransform().GetTranslate() + (offDir * 1500.0f)); } new_port.m_approach[2] = apprIter->GetTransform(); m_ports.push_back(new_port); } int bay = 0; for (auto locIter : locator_mts) { int bayStr, portId; int minSize, maxSize; char padname[8]; const matrix4x4f &locTransform = locIter->GetTransform(); ++bay; // eg:loc_A001_p01_s0_500_b01 PiVerify(5 == sscanf(locIter->GetName().c_str(), "loc_%4s_p%d_s%d_%d_b%d", &padname[0], &portId, &minSize, &maxSize, &bayStr)); PiVerify(bay > 0 && portId > 0); // find the port and setup the rest of it's information bool bFoundPort = false; matrix4x4f approach1(0.0); matrix4x4f approach2(0.0); for (auto &rPort : m_ports) { if (rPort.portId == portId) { rPort.minShipSize = std::min(minSize, rPort.minShipSize); rPort.maxShipSize = std::max(maxSize, rPort.maxShipSize); rPort.bayIDs.push_back(std::make_pair(bay - 1, padname)); bFoundPort = true; approach1 = rPort.m_approach[1]; approach2 = rPort.m_approach[2]; break; } } assert(bFoundPort); // now build the docking/leaving waypoints if (SURFACE == dockMethod) { // ground stations don't have leaving waypoints. m_portPaths[bay].m_docking[2] = locTransform; // final (docked) numDockingStages = 2; numUndockStages = 1; } else { struct TPointLine { // for reference: http://paulbourke.net/geometry/pointlineplane/ static bool ClosestPointOnLine(const vector3f &Point, const vector3f &LineStart, const vector3f &LineEnd, vector3f &Intersection) { const float LineMag = (LineStart - LineEnd).Length(); const float U = (((Point.x - LineStart.x) * (LineEnd.x - LineStart.x)) + ((Point.y - LineStart.y) * (LineEnd.y - LineStart.y)) + ((Point.z - LineStart.z) * (LineEnd.z - LineStart.z))) / (LineMag * LineMag); if (U < 0.0f || U > 1.0f) return false; // closest point does not fall within the line segment Intersection.x = LineStart.x + U * (LineEnd.x - LineStart.x); Intersection.y = LineStart.y + U * (LineEnd.y - LineStart.y); Intersection.z = LineStart.z + U * (LineEnd.z - LineStart.z); return true; } }; // create the docking locators // start m_portPaths[bay].m_docking[2] = approach2; m_portPaths[bay].m_docking[2].SetRotationOnly(locTransform.GetOrient()); // above the pad vector3f intersectionPos(0.0f); const vector3f approach1Pos = approach1.GetTranslate(); const vector3f approach2Pos = approach2.GetTranslate(); { const vector3f p0 = locTransform.GetTranslate(); // plane position const vector3f l = (approach2Pos - approach1Pos).Normalized(); // ray direction const vector3f l0 = approach1Pos + (l * 10000.0f); if (!TPointLine::ClosestPointOnLine(p0, approach1Pos, l0, intersectionPos)) { Output("No point found on line segment"); } } m_portPaths[bay].m_docking[3] = locTransform; m_portPaths[bay].m_docking[3].SetTranslate(intersectionPos); // final (docked) m_portPaths[bay].m_docking[4] = locTransform; numDockingStages = 4; // leaving locators ... matrix4x4f orient = locTransform.GetOrient(), EndOrient; if (exit_mts.empty()) { // leaving locators need to face in the opposite direction const matrix4x4f rot = matrix3x3f::Rotate(DEG2RAD(180.0f), orient.Back()); orient = orient * rot; orient.SetTranslate(locTransform.GetTranslate()); EndOrient = approach2; EndOrient.SetRotationOnly(orient); } else { // leaving locators, use whatever orientation they have orient.SetTranslate(locTransform.GetTranslate()); int exitport = 0; for (auto &exitIt : exit_mts) { PiVerify(1 == sscanf(exitIt->GetName().c_str(), "exit_port%d", &exitport)); if (exitport == portId) { EndOrient = exitIt->GetTransform(); break; } } if (exitport == 0) { EndOrient = approach2; } } // create the leaving locators m_portPaths[bay].m_leaving[1] = locTransform; // start - maintain the same orientation and position as when docked. m_portPaths[bay].m_leaving[2] = orient; // above the pad - reorient... m_portPaths[bay].m_leaving[2].SetTranslate(intersectionPos); // ...and translate to new position m_portPaths[bay].m_leaving[3] = EndOrient; // end (on manual after here) numUndockStages = 3; } } numDockingPorts = m_portPaths.size(); // sanity assert(!m_portPaths.empty()); assert(numDockingStages > 0); assert(numUndockStages > 0); // insanity for (PortPathMap::const_iterator pIt = m_portPaths.begin(), pItEnd = m_portPaths.end(); pIt != pItEnd; ++pIt) { if (Uint32(numDockingStages - 1) < pIt->second.m_docking.size()) { Error( "(%s): numDockingStages (%d) vs number of docking stages (" SIZET_FMT ")\n" "Must have at least the same number of entries as the number of docking stages " "PLUS the docking timeout at the start of the array.", modelName.c_str(), (numDockingStages - 1), pIt->second.m_docking.size()); } else if (Uint32(numDockingStages - 1) != pIt->second.m_docking.size()) { Warning( "(%s): numDockingStages (%d) vs number of docking stages (" SIZET_FMT ")\n", modelName.c_str(), (numDockingStages - 1), pIt->second.m_docking.size()); } if (0 != pIt->second.m_leaving.size() && Uint32(numUndockStages) < pIt->second.m_leaving.size()) { Error( "(%s): numUndockStages (%d) vs number of leaving stages (" SIZET_FMT ")\n" "Must have at least the same number of entries as the number of leaving stages.", modelName.c_str(), (numDockingStages - 1), pIt->second.m_docking.size()); } else if (0 != pIt->second.m_leaving.size() && Uint32(numUndockStages) != pIt->second.m_leaving.size()) { Warning( "(%s): numUndockStages (%d) vs number of leaving stages (" SIZET_FMT ")\n", modelName.c_str(), numUndockStages, pIt->second.m_leaving.size()); } } }
int main(int argc, const char * argv[]) { std::cout << "a1 = " << approach1(2000000) << "\n"; std::cout << isqrt(10000); }