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 4568096
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:01:39+00:00 2026-05-21T19:01:39+00:00

I have these 2 values: decimal totalAmountDue = 1332.29m; short installmentCount = 3; I

  • 0

I have these 2 values:

decimal totalAmountDue = 1332.29m;
short installmentCount = 3;

I want to create 3 installments that have an even amount based on the totalAmountDue (extra pennies apply starting with the lowest installment number going to the highest installment number) using this class:

public class Installment
{
   public Installment( short installmentNumber, decimal amount )
   {
     InstallmentNumber = installmentNumber;
     Amount = amount;
   }

   public short InstallmentNumber { get; private set; }
   public decimal Amount { get; private set; }
}

The installments should be as follows:

{ InstallmentNumber = 1, Amount = 444.10m }
{ InstallmentNumber = 2, Amount = 444.10m }
{ InstallmentNumber = 3, Amount = 444.09m }

I am looking for an interesting way to create my 3 installments. Using a simple LINQ to objects method would be nice. I have been trying to understand more about functional programming lately and this seems like it could be a fairly good exercise in recursion. The only decent way I can think of doing this is with a traditional while or for loop at the moment…

  • 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-21T19:01:40+00:00Added an answer on May 21, 2026 at 7:01 pm

    There’s not a whole lot here that is “functional”. I would approach the problem like this:

    var pennies = (totalAmountDue * 100) % installmentCount;
    var monthlyPayment = totalAmountDue / installmentCount;
    var installments = from installment in Enumerable.Range(1, installmentCount)
                       let amount = monthlyPayment + (Math.Max(pennies--, 0m) / 100)
                       select new Installment(installment, amount);
    

    You might be able to work something out where you constantly subtract the previous payment from the total amount and do the division rounding up to the nearest penny. In F# (C# is too wordy for this) it might be something like:

    let calculatePayments totalAmountDue installmentCount =
      let rec getPayments l (amountLeft:decimal) = function
                | 0 -> l
                | count -> let paymentAmount = 
                             (truncate (amountLeft / (decimal)count * 100m)) / 100m
                           getPayments (new Installment(count, paymentAmount)::l)
                                       (amountLeft - paymentAmount)
                                       (count - 1)
      getPayments [] totalAmountDue installmentCount
    

    For those unfamiliar with F#, what that code is doing is setting up a recursive function (getPayments) and bootstrapping it with some initial values (empty list, starting values). Using match expressions it sets up a terminator (if installmentCount is 0) returning the list so far. Otherwise it calculates the payment amount and calls the recursive method adding the new installment to the front of the list, subtracting the payment amount from the amount left, and subtracting the count.

    This is actually building the list in reverse (adding on to the front each time), so we throw away the extra pennies (the truncate) and eventually it catches up with us so the penny rounding works as expected. This is obviously more math intensive than the add/subtract code above since we divide and multiply in every iteration. But it is fully recursive and takes advantage of tail recursion so we’ll never run out of stack.

    The trouble with C# here is that you want a sequence of installments and recursion and there’s no idiomatic built-in structure for doing that in C#. Here I’m using F#’s list which is immutable and O(1) operation to prepend.

    You could possibly build something using the Scan() method in the Reactive Extensions to pass state from once instance to another.

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

Sidebar

Related Questions

I have a textbox that accepts only these values: . , - $ m
I have a field in my database that contain comma separated values these values
I have an IDictionary, now I want to use these values in a selectlist.
I have a database in TSQL that contains columns in hexadecimal. However these values
I have a decimal column that contains percentages. I'm converting these so that they
I have these in a jsp file. But these values are not updated in
I have these tables and values: Table1 ------------------------ ID | Value ------------------------ 2 |
I have a list with these values: ['1', '10', '100.3', '1000.4', '1000.42', '150', '200',
I have a php form which saves these values to the database: id rand
I have these code in my views.py: array = [] p = Person.objects.filter(client=1,status='Complete').values('name', 'age',

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.