Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6018073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:13:16+00:00 2026-05-23T03:13:16+00:00

I have to write a program to find a rational number that has a

  • 0

I have to write a program to find a rational number that has a property. I wrote the code to check the property, but now I don’t know how to check all rational numbers. I tried with

float rat;
for (int i=1 ; i ; ++i) {
  for (int j=1 ; j ; ++j) {
    rat = (float)i/(float)j;
    if goodRat(rat) then return rat;
  }
}

but it never ends! And it misses too many. So then I tried this

float rat;
while {
  int i = random(1000) + 1;
  int j = random(1000) + 1;
  rat = (float)i/(float)j;
  if goodRat(rat) 
      return rat;
}

but this works only sometimes. How can I solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:13:17+00:00Added an answer on May 23, 2026 at 3:13 am

    The rational numbers are countable, which means that they can be put in one-to-one correspondence with the integers. If you do that, then you’ll have your solution.

    Instead of giving a one-to-one correspondence, an easier way to walk through the rationals is the following.

    Construct an (countably) infinite by (countably) infinite matrix Q so that Q_(i,j) = i/j where i and j range from 1 to infinity. The matrix looks like this:

     1  1/2 1/3 1/4 1/5 . . .
    2/1 2/2 2/3 2/4 2/5 . . .
    3/1 3/2 3/3 3/4 3/5 . . . 
    4/1 4/2 4/3 4/4 4/5 . . .
    5/1 5/2 5/3 5/4 5/5 . . .
     .   .   .   .   .
     .   .   .   .   .
     .   .   .   .   .
    

    Of course, there are many repeats (the entire diagonal is 1!), but I’m going for simplicity over speed.

    What you’re trying to do is walk down the columns, which are infinite, so you’ll miss lots of numbers. Instead, you should walk along the anti-diagonals, which are finite. That is, take the elements in the following order

     1  3  6 10 15  . 
     2  5  9 14  .  .
     4  8 13  .  .  .
     7 12  .  .  .
    11  .  .  .
     .  .  .
     .  .
     .
    

    So you’ll get 1, 2/1, 1/2, 3/1, 2/2, 1/3, 4/1, 3/2, 2/3, 1/4, .... Moreover, you know that you will encounter r/s at step (r+s)(r+s-1)/2 + s, so any given rational number will be encountered in finite time.

    One way to code this is to let i be the row index (outer for loop) and let j be the column index (inner for loop). Then i will range from 1 to infinity, but j will only range from 1 to i.

    If your goodRat function takes a fair amount of time, then you can speed this up by first testing that i and j are coprime, and if not skip them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write an assembly program that will find the minimum of array
I have to write a program that read from a file that contains the
I have to write a program that sniffs network packets (part1-the simple part). And
I have been assigned wit the task to write a program that takes a
I wanted to write a program that test if two files are duplicates (have
Imagine we have a program trying to write to a particular file, but failing.
Write a program to find the largest possible rectangle of letters such that every
EDIT: Sorted. See bottom for solution Okay, I have to write a program that
I have to write a program in which main calls other functions that test
Some information: i have to write a program that controls some devices (this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.