#include <iostream>
#include <cmath>
#include <iomanip>
#include <limits>
using namespace std;
const double FUEL_PER_MILE = 2.125;
// Function to check if a number is an odd prime
bool isOddPrime(int n) {
if (n < 2 || n % 2 == 0) return false; // Exclude even numbers and 2
for (int i = 3; i <= sqrt(n); i += 2) {
if (n % i == 0) return false;
}
return true;
}
// Function to simulate the trip and determine the stranded location
double simulateTrip(double initialFuel, bool printOutput = false) {
double fuel = initialFuel;
int location = 0;
while (fuel >= 0) {
if (printOutput) {
cout << "LOCATION: " << location << " FUEL: " << fixed << setprecision(6) << fuel << endl;
cout << flush; // Ensure output is printed immediately
}
if (isOddPrime(location)) {
fuel += location; // Refuel at odd prime mile markers
if (printOutput) {
cout << location << " FUEL ADDED AT ODD PRIME MILE MARKER" << endl;
cout << flush;
}
}
fuel -= FUEL_PER_MILE; // Consume fuel per mile
if (fuel < 0) {
return location + (fuel / FUEL_PER_MILE); // Return exact stranded location
}
location++;
}
return 9999999; // Use a large number instead of infinity for compatibility
}
int main() {
double minFuel = 6.375; // Based on your calculations
// Verify that 6.375 is indeed enough
double result = simulateTrip(minFuel);
if (result == 9999999) {
cout << "Minimum starting fuel: " << fixed << setprecision(6) << minFuel << endl;
} else {
cout << "ERROR: 6.375 is not enough fuel!" << endl;
}
// Part 2: Simulate with one drop less fuel
double strandedLocation = simulateTrip(minFuel - 0.0001, true);
cout << "Final stranded location with one drop less fuel: " << fixed << setprecision(6) << strandedLocation << endl;
return 0;
}